phpmysqlmysqli

MySQL syntax error: near '1' at line 1


I'm getting the error '1' at line 1, I'm a bit stumped with this one; I'm a complete noob and usually try to correct things without asking for help

It inputs all the data correctly into the db, but offers up this error.

There are a couple of other threads with the same question, the solution to one was: "That was the reason error appeared: you tried to call mysql_query with result of the last update query. Which was, as TRUE converted to String, just '1'."

$title = mysqli_real_escape_string($con, $_POST['title']);
$firstname = mysqli_real_escape_string($con, $_POST['firstname']);
$lastname = mysqli_real_escape_string($con, $_POST['lastname']);
$jobtitle = mysqli_real_escape_string($con, $_POST['jobtitle']);
$address = mysqli_real_escape_string($con, $_POST['address']);
$address2 = mysqli_real_escape_string($con, $_POST['address2']);
$address3 = mysqli_real_escape_string($con, $_POST['address3']);
$postcode = mysqli_real_escape_string($con, $_POST['postcode']);
$telephone = mysqli_real_escape_string($con, $_POST['telephone']);
$email = mysqli_real_escape_string($con, $_POST['email']);

(I know I should really make a prepared statement)

$sql = mysqli_query($con, "UPDATE users SET title='$title', firstname='$firstname', lastname='$lastname', jobtitle='$jobtitle', address='$address', address2='$address2', address3='$address3', postcode='$postcode', telephone='$telephone', email='$email' WHERE username='$user'");

if (!mysqli_query($con,$sql)) {
$result_array = mysql_fetch_assoc($qStuff);
die('Error: ' . mysqli_error($con));
}

Solution

  • the following line stores the value "1" in $sql when the insertion is successful and 0 when failed.

    $sql = mysqli_query($con, "UPDATE users SET title='$title', firstname='$firstname', lastname='$lastname', jobtitle='$jobtitle', address='$address', address2='$address2', address3='$address3', postcode='$postcode', telephone='$telephone', email='$email' WHERE username='$user'");
    

    And you are trying to execute this

    if (!mysqli_query($con,$sql)) 
    

    here, the value of $sql is "1" which is not a valid query. That results in syntax error. Probably if you are trying to achieve this:

    $sql = "UPDATE users SET title='$title', firstname='$firstname', lastname='$lastname', jobtitle='$jobtitle', address='$address', address2='$address2', address3='$address3', postcode='$postcode', telephone='$telephone', email='$email' WHERE username='$user'";
    if(mysqli_query($con,$sql)){
        //do something if the operation is successful
    }