Reputation: 881
Sorry for the badly phrased title, I don't know what else to call this situation.
Here's my dilemma, for fun I'm making a little doodad so people can submit the name, platform, and location of Battlefield 3 servers so they no longer need to rely on word of mouth.
This older code works fine, it deletes records and does it's job.
<?php
mysql_connect('localhost', 'root', 'pass');
mysql_select_db('database');
$server_secret = mysql_real_escape_string($_POST['secret_initial']);
$server_confirm = mysql_real_escape_string($_POST['secret_verify']);
if ($server_secret == $server_confirm) {
echo "Both matched.";
if ((strlen($server_secret) < 6) and (strlen($server_verify) < 6)) {
echo "Password too short.";
} else {
echo "Password is of proper length.";
$sql = "DELETE FROM `servers` WHERE secret='" . sha1($server_secret) . "'";
mysql_query($sql) or die("Couldn't delete record. " . mysql_error());
}
} else {
echo "Mismatch.";
}
?>
However, if I use this:
<?php
mysql_connect('localhost', 'root', 'pass');
mysql_select_db('database');
// Create variables from form
$server_secret = mysql_real_escape_string($_POST['secret_initial']);
$server_confirm = mysql_real_escape_string($_POST['secret_verify']);
// Check for verification
if ($server_secret == $server_confirm) {
// Delete from database
$sql = "DELETE FROM `servers` WHERE secret='" . sha1($server_secret) . "'";
if (mysql_query($sql) or die("Couldn't delete record. " . mysql_error()) {
$success = true;
} else {
$success = false;
}
}
?>
It always reports $success as true, even when it is false, and what also confuses me is even when both the secret and verification are correct the entry isn't removed from the MySQL database.
The older version works fine though. That's what baffles me.
The same method I used for checking success (if (mysql_query($sql)
) worked fine for insertion and actually gives me feedback if it fails as it should, but for some reason it always reports success when I attempt to delete a record.
If anyone can help me out here I'd really appreciate it.
Please note: I'm very much a novice with PHP/MySQL, so please forgive me.
Upvotes: 0
Views: 1863
Reputation: 1054
From This post
mysql_query will return TRUE even if the query did not actually remove anything.
So you will always get true from Delete query..
Credit goes to Paolo
Upvotes: 2
Reputation: 7953
This is because the query is syntactically correct, so there'll never be an error. Instead, you want to find the number of rows that were actually affected:
<?php
mysql_connect('localhost', 'root', 'pass');
mysql_select_db('database');
// Create variables from form
$server_secret = mysql_real_escape_string($_POST['secret_initial']);
$server_confirm = mysql_real_escape_string($_POST['secret_verify']);
// Check for verification
if ($server_secret == $server_confirm) {
// Delete from database
$sql = "DELETE FROM `servers` WHERE secret='" . sha1($server_secret) . "'";
$result = mysql_query($sql);
if (mysql_affected_rows($result)) {
$success = true;
} else {
$success = false;
}
}
?>
Upvotes: 1