keep getting a syntax error (php / mysql)

后端 未结 1 389
别那么骄傲
别那么骄傲 2021-01-29 12:51

php/mysql

I keep getting this 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 n

相关标签:
1条回答
  • 2021-01-29 13:02

    The error is coming from this line:

    if (!mysqli_query($dbCon,$sql)){
    

    $sql contains the result of

    $dbCon->query($add_query);
    

    Since that query was successful, $sql contains TRUE. mysqli_query() requires the second argument to be a string, so TRUE becomes "1", so you're effectively doing:

    if (!mysqli_query($dbCon, "1")) {
    

    That's not a valid query, so you get an error.

    I think what you really meant to do was:

    if (!$sql) {
        die('Error: ' . $dbCon->error);
    } else {
        echo "dados atualizados!";
    }
    

    You don't need to keep calling mysqli_query() repeatedly.

    You should also learn to code using prepared statements instead of substituting variables into the query, to prevent SQL injection.

    0 讨论(0)
提交回复
热议问题