Forum Moderators: coopster

Message Too Old, No Replies

How to use radio buttons to ID a mysql row

Help with radio button form with mysql

         

twindad

3:11 am on Feb 27, 2004 (gmt 0)

10+ Year Member



I've written a script that displays a list of names and puts a radio button next to the name. The idea is that a user can select the name and then press one of two buttons: delete or edit. A third add button with a text field would be available as well.

I'm having difficulty figuring out how to use the radio button's value to send a delete query. Here's what I have so far, and it is the last section --if($submit)--that doesn't work for me:

<?php
function dbConnect()
{//define the database connection information
$dbHost = "localhost";
$dbUser = "root";
$dbPass = "******";
$dbName = "poker";
//connect to the db server
$link = @mysql_connect($dbHost, $dbUser, $dbPass);
}?>

<?php
function displaynames()
{//define variables
$result = mysql_query("SELECT * FROM hosts");
if ($myrow = mysql_fetch_array($result))
{
echo "<form method='post' action='/poker/schedule/learn.php'>";
echo "<table border=1>";
echo "<tr><td>NAME</td><td>DELETE</td></tr>";
do
{
printf("<tr><td>%s</td><td><input type='radio' name='hostid' value=%s></td></tr>", $myrow["name"], $myrow["hostid"]);
}while ($myrow =mysql_fetch_array ($result));
echo "</table>";
echo "<input type='Submit' name='submit' value='delete'>";
}else{
echo "Sorry, no records were found!";
}
}?>

<?php
dbConnect();
displaynames();
?>

<?php
if ($submit)
{
$sql = "DELETE FROM hosts WHERE hostid=$hostid";
$result = mysqlquery($sql);
displaynames();
}
?>

So, how do I reference the value of the radio button named hostid? When I do a view source on the page, each radio button has the appropriate value assigned, I just need to reference it so that the appropriate row is deleted, then re-display the list.

Thanks for your help!

-Chris

mykel79

9:35 am on Feb 27, 2004 (gmt 0)

10+ Year Member



Depending on your server setting, you might have to access all posted variables like this:
$HTTP_POST_VARS['hostid']

Try to print out hostid and see if it is shown correctly, then move on to the sql query.

Netizen

11:18 am on Feb 27, 2004 (gmt 0)

10+ Year Member



The last bit contains a typo, corrected below (mysqlquery should be mysql_query):

<?php
if ($submit)
{
$sql = "DELETE FROM hosts WHERE hostid=$hostid";
$result = mysql_query($sql);
displaynames();
}
?>

twindad

2:24 pm on Feb 27, 2004 (gmt 0)

10+ Year Member



Oh, wow...the typo was it. Thank you for pointing out the mistake!

Netizen

7:08 pm on Feb 27, 2004 (gmt 0)

10+ Year Member



All part of the service :-)