Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I am using the following code to delete a record. It works fine, but MySQL always returns a blank error message. PHP returns "Error: " instead of "1 Record Dropped".

$eventid = intval(mysqli_real_escape_string($dbconnect, $_POST['eventid']));
mysqli_query($dbconnect,"DELETE FROM Events WHERE EventID='$eventid' LIMIT 1");
if (!mysqli_query($dbconnect,$sql)) {
  die('Error: ' . mysqli_error($dbconnect));
}
echo "1 Record Dropped";

When I use the same PHP code and change the SQL to a SELECT or INSERT INTO command instead of a DELETE command, I receive the correct response from PHP.

share|improve this question

closed as off-topic by ypercube, Paul White, Max Vernon, Mark Storey-Smith, RolandoMySQLDBA Jul 25 at 15:21

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Too localized - this could be because your code has a typo, basic error, or is not relevant to most of our audience. Consider revising your question so that it appeals to a broader audience. As it stands, the question is unlikely to help other users (regarding typo questions, see this meta question for background)." – Paul White, Max Vernon, Mark Storey-Smith
If this question can be reworded to fit the rules in the help center, please edit the question.

1  
There is something wrong on that code: you are querying mysql twice, only check for errors once, and you do not show the actual query text of the second one ($sql). You can also print the number of records dropped with mysqli_affected_rows(). –  jynus Jul 23 at 8:34

1 Answer 1

up vote 1 down vote accepted

Doh! @jynus was exactly correct. Here is the fix:

$eventid = intval(mysqli_real_escape_string($dbconnect, $_POST['eventid']));
$sql="DELETE FROM Events WHERE EventID='$eventid' LIMIT 1";
if (!mysqli_query($dbconnect,$sql)) {
  die('Error: ' . mysqli_error($dbconnect));
}
echo mysqli_affected_rows() . " Record(s) Dropped";
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.