MYSQL PHP: The update search causes a syntax error

I’m trying to update the content of a database (specifically the post content of one of the posts database’s records).

So here’s my PHP code :

$update_sql = "UPDATE wp_posts SET post_content='$new_content' WHERE ID=602";

if (mysqli_query($conn, $update_sql)) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . mysqli_error($conn);
}

This is what I get:

Error updating record: 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 'attention�sur
les plats eux-m�mes, le service est lui aussi tout en
simplicit' at line 1

I don’t know what I’m doing wrong. All I know is that $new_content contains a very long string (an entire wordpress post’s content, complete with various HTML tags).

What should I do for it to accept my update query ?

Thanks for your help

You need to escape the string before inserting it into the database

$update_sql = "UPDATE wp_posts SET post_content='" . mysqli_real_escape_string($new_content) . "' WHERE ID=602";

if (mysqli_query($conn, $update_sql)) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . mysqli_error($conn);
}
Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 后端存储 » MYSQL PHP: The update search causes a syntax error

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录