0

I have a table which selects the results from a database table, with each record there is a Button echoed (DeleteButton). I would like this delete button to delete THE SELECTED ROw (RECORD). My code is presented below, I have narrowed the issue down the 'id' not being POST/GET correctly. I think the form is not passing the id correctly. Can somebody help?

The Form In A Table Cell:

echo "<td>"
.'
<form id="DeleteUser" action="DeleteUser.php" method"post">
<input type"text" name="id" value=" '.$row['user_id'].' " hidden />
<input type="submit" value="Delete">
</form>
'.
"</td>";
echo '</td>';

The Delete Statement (DeleteUser) (DeleteUser.php) :

<?php


$stmt = $con->prepare("DELETE FROM users WHERE user_id = ?");
$stmt->bind_param('sissi',$_GET['id']);
$stmt->execute(); 
$stmt->close();


?>
3
  • please post the HTML generated by PHP code above Commented May 19, 2014 at 20:08
  • The php generates a blank html code. Commented May 19, 2014 at 20:14
  • You are specifying the form submission method as POST in the form, but trying to read the value from $_GET in DeleteUser.php; it should be $_POST Commented May 19, 2014 at 20:17

1 Answer 1

2

Your first parameter on the $stmt->bind_param statement has 5 different parameters. You're only passing one. Change it to this:

$stmt = $con->prepare("DELETE FROM users WHERE user_id = ?");
$stmt->bind_param('i',$_GET['id']);
$stmt->execute(); 
$stmt->close();

You have to make sure $_GET['id'] is int value also.

On your <input type"text" name="id" value=" '.$row['user_id'].' " hidden /> line, remove the spaces around value= for the concatenation. So you have value="'.$row['user_id'].'".

I would also suggest having some kind of check in place to make sure that I can't go in and delete a different user just by inputting a different number in there, random or otherwise.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.