View Full Version : How to populat MySQL table with an option or select list?

12-11-2008, 09:44 AM
How would I go about populating a MySQL table with an option or select list?

table = items
id = (primary key INT 3)
items = (VARCHAR 50)How do I get the values below into the table above?

<select name="select" id="select">
<option value="item1">item1</option>
<option value="item2">item2</option>

Read the file;

$row = 1;
$handle = fopen("items.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
fclose($handle);Import the file;

mysql_connect("localhost", "user", "pass");

# first get a mysql connection as per the FAQ
$fcontents = file ('./items.csv');
# expects the csv file to be in the same dir as this script

for($i=0; $i<sizeof($fcontents); $i++) {
//$line = trim($fcontents[$i]);
$line = trim($fcontents[$i], '');
$arr = explode("\t", $line);
#if your data is comma separated
# instead of tab separated,
# change the '\t' above to ','

$sql = "insert into items values ('".implode("','", $arr) ."')";
echo $sql ."<br>\n";
if(mysql_error()) {
echo mysql_error() ."<br>\n";

Sorry I know - I answered my own question; I am always in a rush (it's my downfall)