Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    Regular Coder student101's Avatar
    Join Date
    Nov 2007
    Posts
    634
    Thanks
    80
    Thanked 15 Times in 15 Posts

    Question How to populat MySQL table with an option or select list?

    How would I go about populating a MySQL table with an option or select list?

    Code:
     table = items
    id = (primary key INT 3)
    items = (VARCHAR 50)
    How do I get the values below into the table above?
    Code:
    <select name="select" id="select">
       <option value="item1">item1</option>
       <option value="item2">item2</option>
    </select>
    Cheers

    Edit: Read the file;
    PHP Code:
     $row 1;
    $handle fopen("items.csv""r");
    while ((
    $data fgetcsv($handle1000",")) !== FALSE) {
        
    $num count($data);
        echo 
    "<p> $num fields in line $row: <br /></p>\n";
        
    $row++;
        for (
    $c=0$c $num$c++) {
            echo 
    $data[$c] . "<br />\n";
        }
    }
    fclose($handle); 
    Import the file;
    PHP Code:
    mysql_connect("localhost""user""pass");
    mysql_select_db("database");

    # 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) ."')"
          
    mysql_query($sql);
          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)
    Last edited by student101; 12-11-2008 at 10:20 AM.
    Thanks for your support!
    Update MySQL with checkboxes | Tell A Friend | Delete MySQL with checkboxes

    Give thanks & resolve when done :thumbsup:


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •