如果我们将查询存储到变量中,则MySQL REPLACE不起作用

时间:2017-08-18 10:09:32

标签: php mysql database

我的查询是这样的:

<?php
$sql = "UPDATE macs_temp SET home_page_heading='$home_page_heading', home_page_sub_heading='$home_page_sub_heading', home_page_description='$home_page_description', about_us_heading='$about_us_heading', about_us_description='$about_us_description', business_description='$business_description', we_do_description='$we_do_description', video_description='$video_description', photography_description='$photography_description', music_description='$music_description', web_and_app_description=REPLACE($web_and_app_description, '\'', '') WHERE id=1";

if ($conn->query($sql) === TRUE) {
//    echo "Record updated successfully";
header("Location: index.php");
   exit;
} else {
    echo "Error updating record(Contact System Admin): " . $conn->error;
    echo "Inside else error..!";
}
?>

给出错误: 更新记录时出错(联系系统管理员):您的SQL语法有错误;查看与您的MySQL服务器版本相对应的手册,以便在''j','\'',''附近使用正确的语法。)第1行的WHERE id = 1'其他错误..!

注意:我的表单包含11个字段,该字段中的任何一个/多个可能包含撇号,所以使用所有撇号我想更新mysql数据库中的所有数据。

1 个答案:

答案 0 :(得分:0)

尝试此操作,您需要将$web_and_app_description括在''中,以便将其视为字符串

$sql = "UPDATE macs_temp SET home_page_heading='$home_page_heading', 
home_page_sub_heading='$home_page_sub_heading', 
home_page_description='$home_page_description', 
about_us_heading='$about_us_heading', about_us_description='$about_us_description', 
business_description='$business_description', 
we_do_description='$we_do_description', video_description='$video_description', 
photography_description='$photography_description', 
music_description='$music_description', 
web_and_app_description=REPLACE('$web_and_app_description', '\'', '') WHERE 
id=1";