Page 1 of 1

PHP Delete button help!

Posted: Wed Mar 26, 2014 7:52 am
by stanlex
Hello,

I recently started learning PHP on my own and started to use mysql and apache as well. I made a basic table in mysql and using some php code i displayed the table in a html table in a browser. Now I'd like to add a delete button beside each row and when clicked, it would delete that row. I am very new to this and I'm just practicing. Could anyone please help me? This is the code I have so far:

Code: Select all

<?php

ini_set('display_errors',1); 
 error_reporting(E_ALL);

mysql_connect('localhost', 'root', '');

mysql_select_db('testdb');

$result = mysql_query('select * from products');

$numrows = mysql_numrows($result);

//****************************************************************
print "<table border = 3 style = width:400px>";
	

for($i = 0; $i < $numrows; $i++)
{
	$row = mysql_fetch_row($result);
	
	print "<tr>";
	

	foreach($row as $cell)
	{

		print "<td>";
		print $cell;
		print "</td>";
		
		
	}
	print "</tr>";

	
}
	

print "</table>";


mysql_close();

?>
I also have a delete.php page but I really don't know where to start. I've looked online tutorials and many say different ways.

Thanks.

Re: PHP Delete button help!

Posted: Wed Mar 26, 2014 9:03 am
by Celauran
First, don't use mysql_ functions. Seriously. These functions have been deprecated and will be removed from the language. Especially if you're just starting, do yourself a favour and become familiar with PDO before bad habits become ingrained. Also, don't use the root account for MySQL. Or anything, really.

That said, if you're looking to create a single delete link, delete.php?id=foo will suffice. In your delete script, check for $_GET['id'], make sure it's a valid ID, and then pass that in to your DELETE FROM query. If you'd like to delete multiple entries with a single submission, create a form with a checkbox array containing the IDs and delete the selected IDs on post.