Problem With Php Insertion From List! Urgent

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TKB
    New Member
    • Nov 2006
    • 8

    Problem With Php Insertion From List! Urgent

    The following code takes the values from the db and echos them inside a list. Then when the user selects a value from the list, the value must be stored to the db, but this doesn't work. Could anyone help?

    Thank you in advance for your effort.

    <?php
    $query = "SELECT categorydescrip tion, categoryname FROM categories ORDER BY categoryname ASC";
    $result = mysql_query($qu ery,$conn) or die('Query failed: ' . mysql_error());
    ?>
    <select name="categoryn ame" id="categorynam e">
    <?php
    while ($line = mysql_fetch_arr ay($result, MYSQL_NUM)) {
    echo "<option value= \"$line[0]\"> $line[1] </option>" ;

    }
    mysql_free_resu lt($result);



    $query ="INSERT INTO orderdetails (CategoryName)
    VALUES('$line[0]\')"; mysql_query($qu ery) or die(mysql_error ());
    ?>
  • scriptee
    New Member
    • Nov 2006
    • 17

    #2
    Hi ...

    How do you expect it know ...what the user selected ...
    Capture the selected value using form "post" method then insert it into the database.

    Try it !

    Comment

    • seangates
      New Member
      • Dec 2006
      • 19

      #3
      Originally posted by TKB
      The following code takes the values from the db and echos them inside a list. Then when the user selects a value from the list, the value must be stored to the db, but this doesn't work. Could anyone help?

      Thank you in advance for your effort.

      <?php
      $query = "SELECT categorydescrip tion, categoryname FROM categories ORDER BY categoryname ASC";
      $result = mysql_query($qu ery,$conn) or die('Query failed: ' . mysql_error());
      ?>
      <select name="categoryn ame" id="categorynam e">
      <?php
      while ($line = mysql_fetch_arr ay($result, MYSQL_NUM)) {
      echo "<option value= \"$line[0]\"> $line[1] </option>" ;

      }
      mysql_free_resu lt($result);



      $query ="INSERT INTO orderdetails (CategoryName)
      VALUES('$line[0]\')"; mysql_query($qu ery) or die(mysql_error ());
      ?>
      TKB,

      You need to reference the posted selection. Thus, here is your fixed code:
      [PHP]$query ="INSERT INTO orderdetails (CategoryName)
      VALUES('$_POST[categoryname]')";
      mysql_query($qu ery) or die(mysql_error ());[/PHP]
      Hope that helps.

      Sean

      Comment

      Working...