How to insert one item from PHP drop down?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • matt sheeyd
    New Member
    • Jan 2011
    • 7

    How to insert one item from PHP drop down?

    Hey,
    I need to insert a an item from my drop down into MySQL. However at the moment i am getting all the values and i need to know if theres a way to submit the values individually.
    Code:
    <?php 
    $sql="SELECT name, no FROM test";
     echo "<select name=\"name\">";  
     echo "<option size=75>name | no</option>"; 
      if ($result=mysql_query($sql)) {  
      while ($data=mysql_fetch_assoc($result))  
     {  
     echo "<option size=75>$data[name]&nbsp; $data[no]</option>";  
     }  
     } 
     else 
     {
     echo "<!-- SQL Error ".mysql_error()." -->";
     }
     echo "</select>";
    ?>
    So basically i want to insert into test.name the name in the drop down and insert into test.no the no next to the name. It works at the moment but everything goes into the name column echo "<select name=\"name\">" ; and no is empty. Any help would be appreciated

    Thanks matt.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    - size is an invalid attribute for <option> (it belongs into <select>)
    - array keys must be quoted ($array['key'])

    you mean the submitted value doesn’t go in the right database field? then there’s something wrong with your insert query.

    Comment

    • matt sheeyd
      New Member
      • Jan 2011
      • 7

      #3
      Hi dormilich,
      Sorry i didnt look at the size att when i typed it. The problem is occuring becuase alot of the data has similiar names. eg id 4242 & id 4342. So i need to SHOW a NAME and an ID in the drop down but ONLY submit the ID to the database. Its all very messy at this stage. They were previously using ms access, absolute nightmare. Thanks for the reply tho. I have taken a different route now and instead i have supplied a drop down with just the id and a simple ajax table that displays the MySQL row details for that id below the drop down.

      thanks.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        if you only need the ID to pass, put that into the value attribute.
        Code:
        <option value="id">name</option>
        // or
        <option value="id">name id</option>

        Comment

        Working...