image button for deleting users from mysql
so I've got a problem
$query = "SELECT * FROM users;";
$result = mysql_query($query) or die(mysql_error());
while ($row=mysql_fetch_array($result)) {
?>
<tr>
<td><?php echo $row['fio']; ?></td>
<td><?p开发者_Go百科hp echo $row['born_date']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['number']; ?></td>
<td><?php echo $row['work_post']; ?></td>
<td><?php echo $row['inwork_date']; ?></td>
<td><center><form action="delete.php" method="POST"><input type="hidden" name="user_id" value="'.$row['user_id'].'"><input type="Submit" class="deleteButton" value="Delete"></form></center></td>
</tr>
<?php
and in delete.php I have
<?php
if(isset($_POST['Submit'])) {
$query = "DELETE FROM users WHERE user_id='$user_id'";
$result = mysql_query($query) or die(mysql_error());
header("Location: edit.php");
exit;
}
?>
I see list of users - it works! But I can not delete anybody.
You're not correctly inserting the ID due to the fact that the PHP interpreter isn't "active" where you seem to think it is.
As such, change...
<input type="hidden" name="user_id" value="'.$row['user_id'].'">
...to...
<input type="hidden" name="user_id" value="<?php echo $row['user_id']; ?>">
...and all will be well. (It happens to us all occasionally.)
Incidentally, wrapping each delete link in a form is perhaps a bit excessive - is there a reason you don't want to use a "normal" link?
UPDATE
You'll also most likely need to explicitly use the HTTP post variable on your delete page as follows:
$query = "DELETE FROM users WHERE user_id='" . intval($_POST['user_id']) . "'";
<input type="Submit" class="deleteButton" value="Delete">
You need a name on this field to pass your if(isset($_POST['Submit'])) {
<input type="Submit" class="deleteButton" value="Delete" name="Submit">
On a slightly different note, I hope you are doing some sort of check that the user executing the delete call has permissions.
精彩评论