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!
I have a query that selects stuff from a database and then places each item in a new row in a table using a while loop. I also made it add 2 extra columns per row with a "delete" and "edit" button. Ultimately what needs to happen is if I click the delete button it must delete that item from my database. What puzzles me is how can i make sure each delete button has a unique name so I can make sure that the correct entry is deleted and how would I call it? I hope that I have made sense lol. How is my code
Isn't your "cid" column unique? You should update and delete based on unique keys where ever possible. I would store the cid in a hidden field and then reference it that way.
I've solved the same problem recently. Like the previous poster says, make each row a form. The form action should be the same page, with the code in the head of the page. You can use as many querys, deletes, inserts as you like in that function. Here is my form handling function, and the table with the form-per-row.
if (isset($_POST['submit'])) { // Form has been submitted improperly.
$date = trim($_POST['date']);
$time = trim($_POST['time']);
$gym = trim($_POST['gym']);
$id = trim($_POST['id']);
if ($date == NULL || $time == NULL )
{
$message = "Your Games IS NOT edited. <br />You left a feild blank or entered too many characters";
} else {
$query = "UPDATE `games` SET `date`='".$date."',`time`='".$time."',`gym`='".$gym."' WHERE `coach`='".$_SESSION['display_name']."' AND `id`='".$id."'";
mysql_query($query, $connection) or die(mysql_error());
$message = 'Game edited successfully' ;
}
When I click my edit button it also deletes. I tried changing the name of the button but no luck. I added an if statement redirecting to the index.php page if the edit button is clicked which works. So basically I get redirected and that line on my page gets deleted. Here is the code. Thanks
The php page needs to determine which submit button was clicked. If you give each submit input a name then there will be a $_POST value associated with it.
pass your unique/primary key field value with delete button
you can check in your Database for primary key if it not exist please make new one and send this id(i am giving my dummy name for field) with delete button