Connecting Tech Pros Worldwide Forums | Help | Site Map

Problem With Php Insertion From List! Urgent

TKB TKB is offline
Newbie
 
Join Date: Nov 2006
Posts: 8
#1: Dec 1 '06
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 categorydescription, categoryname FROM categories ORDER BY categoryname ASC";
$result = mysql_query($query,$conn) or die('Query failed: ' . mysql_error());
?>
<select name="categoryname" id="categoryname">
<?php
while ($line = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<option value= \"$line[0]\"> $line[1] </option>" ;

}
mysql_free_result($result);



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

Newbie
 
Join Date: Nov 2006
Posts: 17
#2: Dec 1 '06

re: Problem With Php Insertion From List! Urgent


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 !
Newbie
 
Join Date: Dec 2006
Location: West Linn, OR
Posts: 19
#3: Dec 1 '06

re: Problem With Php Insertion From List! Urgent


Quote:

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 categorydescription, categoryname FROM categories ORDER BY categoryname ASC";
$result = mysql_query($query,$conn) or die('Query failed: ' . mysql_error());
?>
<select name="categoryname" id="categoryname">
<?php
while ($line = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<option value= \"$line[0]\"> $line[1] </option>" ;

}
mysql_free_result($result);



$query ="INSERT INTO orderdetails (CategoryName)
VALUES('$line[0]\')"; mysql_query($query) 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($query) or die(mysql_error());[/PHP]
Hope that helps.

Sean
Reply