-1

I'm making a website for the first time using PHP with a MySQL database and getting a syntax error that I don't understand. I just want to delete everything from tblreparation with a certain ID and also everything from rblrepstat with that same ID. I am using this code:

<?php
    $con=mysqli_connect("localhost","root","MYPASS","repair");
    $ID = $_REQUEST['ID'];
    $sql = mysqli_query($con, "DELETE FROM tblreparation WHERE ID = {$ID}");
    if (!mysqli_query($con,$sql)) {
      die('Error: ' . mysqli_error($con));
    }
    $sql = mysqli_query($con, "DELETE FROM tblrepstat WHERE repID = {$ID}");
    if (!mysqli_query($con,$sql)) {
      die('Error2: ' . mysqli_error($con));
    }
    echo "1 record deleted";
    mysqli_close($con); 
?>

And this is the error I am getting:

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1

Near 1? I don't even see a '1'...

Air
  • 8,274
  • 2
  • 53
  • 88
ivancoene
  • 150
  • 4
  • 16

2 Answers2

1

You're calling mysqli_query twice for each query.

The second time you call it you're actually passing a resource as the query parameter, which causes the error you're getting.

Try changing your code to this:

<?php
    $con=mysqli_connect("localhost","root","MYPASS","repair");
    $ID = $_REQUEST['ID'];
    $sql = "DELETE FROM tblreparation WHERE ID = {$ID}";
    if (!mysqli_query($con,$sql)) {
      die('Error: ' . mysqli_error($con));
    }
    $sql = "DELETE FROM tblrepstat WHERE repID = {$ID}";
    if (!mysqli_query($con,$sql)) {
      die('Error2: ' . mysqli_error($con));
    }
    echo "1 record deleted";
    mysqli_close($con); 
?>
vitorsdcs
  • 682
  • 7
  • 32
-1

Your $ID might contain 1. Which also the location of the error in the query.

DELETE FROM tblreparation WHERE ID = $ID

Or if ID is containing a string or stored in another format which is string related (varchar) then wrap it in quotes:

DELETE FROM tblreparation WHERE ID = '$ID'
Daryl Gill
  • 5,464
  • 9
  • 36
  • 69