As shown here if there is a numeric id that matches $member_id that record is deleted and $res evaluates to TRUE. If the table contains a ‘member_id’ that’s a string such as abc1234 it will only delete if I make $member_id a string by enclosing it in quotes ‘$member_id’ that will delete all matches but when then $res always evaluates to TRUE so if there is a mismatch nothing gets deleted(as it should) but the user gets a “$member_id.” has been removed from members table” message. I hope that was clear.
<?php
$member_id = "";
require("connect.php");
if (isset($_POST['member_id']))$member_id = fix_string($_POST['member_id']);
$sql=("DELETE FROM members WHERE member_id = $member_id");
$res = mysqli_query($con,$sql);
**if($res) {
echo "member with ID of ".$member_id." has been removed from members table";
} else {
echo "member was not deleted";
}**
function fix_string($string) {
if (get_magic_quotes_gpc()) $string = stripslashes($string);
return htmlentities ($string);
}
?>
Your logic is in the right place, but not executed correctly. You’re trying to see if a user/member has been deleted by doing:
However,
mysqli_query()will returntrueif the statement executed properly for aDELETE– it doesn’t matter if it deletes rows or not.What you’ll want to do is utilize
mysqli::$affected_rows:This will check if there was a single record affected by your
DELETEstatement (assuming your member-IDs are unique; if not, you could use>= 1instead). If there was one, well, it was removed!Side Note (not answer specific)
You should drop your
fix_string()method and opt formysqli‘s prepared statements which will auto-sanitize the input for you. You can try the following for your existing code: