Radio buttons for ID...deleting record from database.

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
TheDumbNerd
Forum Newbie
Posts: 13
Joined: Sun Nov 13, 2011 4:01 pm

Radio buttons for ID...deleting record from database.

Post by TheDumbNerd »

Okay, so instead of the ID's of each record showing up in the form that is displayed for the user, there are radio buttons. I want to create it as when the user clicks on one of the radio buttons, that record is selected. Then the user, clicks on the delete button and gets redirected to another page in which it displayes the content of that record.
For example:

First Name: (value of first name in record)
Last Name: (value of last name in record)

And then there will be a button on the bottom that says, Confirm delete. And then when the user clicks that button, the record will get deleted.

I just don't know how to use the radio buttons that contain the ID of each record to do this..

Code: Select all

echo "<tr><td><input type='radio' name='idselect' value=".$row["ID"]." /></td>
That's how i put the radio buttons, as the value being the ID of the record from my database. I also created a variable:

Code: Select all

@$id_select=$_POST['idselect'];
But i don't think this is the right way, then I passed that variable to the 'confirm' page.

Code: Select all

@$id_select=$_POST['idselect'];

If ($record_action=='Delete')
echo "<h1> Delete Record </h1>";
 $db=mysql_connect("localhost","root") or die(mysql_error());

		 mysql_select_db("project6",$db) or die (mysql_error());

		 $SQL="SELECT * FROM 'contacts' WHERE ID='$id_select'";

		 $result=mysql_query($SQL) or die(mysql_error());
		 
		 $num_results=mysql_num_rows($result);
		 
		  $row=mysql_fetch_array($result);

			 echo "".$row["ID"]."".$row["First_Name"]."".$row["Last_Name"]."".$row["Address"]"";
			 
		 mysql_close($db)
Don't mind the record_action stuff. But I don't know how to get the value of the record from the user just pressing the radio button that contains the ID. As you can see, I was trying to display the record that the user had selected.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Radio buttons for ID...deleting record from database.

Post by social_experiment »

$_POST['idselect'] is the correct way to access the value from the radio button; what happens if you echo $id_select to the browser?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
TheDumbNerd
Forum Newbie
Posts: 13
Joined: Sun Nov 13, 2011 4:01 pm

Re: Radio buttons for ID...deleting record from database.

Post by TheDumbNerd »

It doesn't show up when i go

echo $id_select;
or
echo ".$id_select.";
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Radio buttons for ID...deleting record from database.

Post by social_experiment »

Code: Select all

echo "<tr><td><input type='radio' name='idselect' value='".$row["ID"]."' /></td>
What does the source for the page containing the radio button look like; try the sample of code above because it was missing single quotes you used to encase the values of the attributes in the input tag
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
TheDumbNerd
Forum Newbie
Posts: 13
Joined: Sun Nov 13, 2011 4:01 pm

Re: Radio buttons for ID...deleting record from database.

Post by TheDumbNerd »

Code: Select all

<table border="1" />
<th>ID</th> <th> First Name </th> <th>Last Name </th> <th> Address </th> <th> Postal code </th> <th>Phone</th> <th>E-mail Address</th><th> City </th><th>Province</th> 

<?php
@$record_action=$_POST['select'];
@$id_select=$_POST['idselect'];

$db=mysql_connect("localhost","root") or die(mysql_error());
					
mysql_select_db("project6",$db) or die (mysql_error());
$SQL="SELECT * 

FROM  `contacts` 

INNER JOIN cities ON contacts.citiesID = cities.citiesID

ORDER BY contacts.Last_Name";
$result=mysql_query($SQL) or die(mysql_error());
$num_results=mysql_num_rows($result);

					
for ($i=0;$i<$num_results;$i++)

{

$row=mysql_fetch_array($result);

 echo "<tr><td><input type='radio' name='idselect' value='".$row["ID"]."' /></td><td>".$row["First_Name"]."</td><td>".$row["Last_Name"]."</td><td>".$row["Address"]."</td><td>".$row["Postal_Code"]."</td><td>".$row["Phone"]."</td><td>".$row["Email_Address"]."</td><td>".$row["Name"]."</td><td>".$row["Province"]."</td></tr>";

}
 mysql_close($db);

?>
</table>
<form name="addform" action="p06_form.php" method="POST">

<input type='submit' name='select' value='Add' /><br/>

</form>

<form name="deleteform" action="p06_confirm.php" method="POST">
<input type='submit' name='select' value='Delete' /><br/>

<?php echo "&nbsp"; ?><br/>

<input type='submit' name='select' value='Edit' /><br/>
</form>

Code: Select all

<?php
@$record_action=$_POST['select'];
@$id_select=$_POST['idselect'];

If ($record_action=='Delete')
echo "<h1> Delete Record </h1>";
 echo $id_select;
 
If ($record_action=='Edit')
echo "<h1> Edit Record </h1>";

?>

That's my code for both of my documents. First one is named p06_main.php and second one is p06_confirm.php
I can't see the problem!
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Radio buttons for ID...deleting record from database.

Post by social_experiment »

The source i'm referring to is if you select the view the source from the browser, not the php source :)
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply