Working with values from a drop-down box

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Working with values from a drop-down box

Post by evilmonkey »

Hello.

I have a drop-down box on my form. To populate it, I use the foreach() commnd, like so:

Code: Select all

//connected to db earlier...
$sql = "SELECT title FROM news";
$result = mysql_query ($sql, $db);
echo "<FORM ACTION="newsprocess.php" method=POST>";
echo "<select name="title">";
while ($row=mysql_fetch_assoc($result))
{
foreach ($row as $key=>$val) {
echo "<option value="$key=$val">$val</option>"; }}
echo "</select>";
echo "<br>";
echo "<INPUT TYPE=SUBMIT name=delete>";
echo "</FORM>";
This works perfectly (as someone I know would say "Runs like a champ" :lol: ), but the problem is in newsproccess.php, the action script:

Code: Select all

$sql="DELETE FROM news WHERE title='$val'";
$result=mysql_query ($sql, $db);
echo "Deleted! <br>";
echo "Please wait one second...";
echo $sql;
$sql="";
$result="";
As you can see, I am "echoing" out the query, but the all I get is title=''. No value. How can I make this so it works?

Thanks!
User avatar
Sevengraff
Forum Contributor
Posts: 232
Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:

Post by Sevengraff »

Try this:

Code: Select all

$sql='DELETE FROM news WHERE title=' . $_POST['title']; 
$result=mysql_query ($sql, $db); 
echo "Deleted! <br>"; 
echo "Please wait one second..."; 
echo $sql; 
$sql=""; 
$result="";
See, in your first file, you have <select name=\"title\">, and so any <option>'s will be sent to php with the name title, and their value of whatever was selected.
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Carelessness gets the best of everyone...I'm sorry for such a stupid error. Thanks!

Cheers!
Post Reply