SQL语法错误。第1行的'WHERE post_id ='17''

时间:2019-06-08 02:08:12

标签: php content-management-system

我遇到以下错误:

  

查询失败:您的SQL语法有误;检查与您的MariaDB服务器版本相对应的手册以获取正确的语法,以在第1行的'WHERE post_id ='17'附近使用

这是我的代码:

$sql_query2 = "UPDATE posts SET post_title = '$post_title', post_category = '$post_category', post_tags = '$post_tags',  post_text = '$post_text',  post_image = '$post_image',  WHERE post_id = '$_POST[post_id]'";

$edit_post_query = mysqli_query($conn, $sql_query2);

   if(!$edit_post_query) {
    die("QUERY FAILED:" .mysqli_error($conn));
} else {

header("Location: posts.php");

我应该怎么做才能解决该SQL语法错误?

3 个答案:

答案 0 :(得分:1)

在WHERE之前删除逗号。更改

post_image = '$post_image',  WHERE post_id = '$_POST[post_id]'

post_image = '$post_image'  WHERE post_id = '$_POST[post_id]'

答案 1 :(得分:0)

List<Classes> myClassList = new List<Classes>();

myClassList.add(Class1);
myClassList.add(Class2);
//add more classes

foreach(Class currentClass in myClassList){
    initialize currentClass

    if(!currentClass.doSomething){
        break;
    }
    else
    {
        currentClass.dispose();
    }
}

注意:使用mysqli_real_escape_string保护免受黑客攻击。

答案 2 :(得分:0)

或尝试使用$ sql:

$sql_query2 = "UPDATE posts SET post_title = '.$post_title.', post_category = '.$post_category.', post_tags = '.$post_tags.',  post_text = '.$post_text.',  post_image = '.$post_image.',  WHERE post_id = '.$_POST[post_id].'";

或:

$sql_query2 = "UPDATE posts SET post_title = $post_title, post_category = $post_category, post_tags = $post_tags,  post_text = '$post_text',  post_image = $post_image,  WHERE post_id = '.$_POST[post_id].'";