【问题标题】:MySQL syntax error when UPDATEing a record with JSON使用 JSON 更新记录时出现 MySQL 语法错误
【发布时间】:2011-05-29 21:10:20
【问题描述】:

我主要是一个 JavaScript 人,我正在为客户端构建一个库,该库根据 HTTP_REFERRER 在 MySQL 中缓存一些数据。如果数据与服务器上的数据不同,它会更新缓存。我不知道我到底做错了什么,但它说这个有语法错误:

if(mysql_query("UPDATE `cache-test` (cache) SET ('".addslashes(preg_replace('/\s\s+/', ' ', $referrer['cache']))."') WHERE url = '".$referrer['current']."'",$con)){ echo "saved"; }
else { echo mysql_error($con); }

我得到的错误是:

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以了解在 '(cache) SET ('[{\"LatLng\":{\"Ba\":45.531124,\"Ca\":-122.68374699999998} ,\"InfoW' 在第 1 行

发送的数据看起来像(当然是在被剥离和添加斜线之前):

[{"LatLng":{"Ba":45.531124,"Ca":-122.68374699999998},"InfoWindow":"\n        <address>1125 NW 12th Ave, Portland, OR</address>\n        <p>My first apartment</p>\n      ","originalAddress":"1125 NW 12th Ave, Portland, OR"},{"LatLng":{"Ba":45.5144501,"Ca":-122.67644239999998},"InfoWindow":"\n        <address>1230 SW 2nd Ave, Portland, OR</address>\n        <p>My 2nd apartment</p>\n      ","originalAddress":"1230 SW 2nd Ave, Portland, OR"},{"LatLng":{"Ba":45.748955,"Ca":-122.47959000000003},"InfoWindow":"\n        <address>17501 NE 188th Ct, Brush Prairie, WA</address>\n        <p>The first place I lived by my own</p>\n      ","originalAddress":"17501 NE 188th Ct, Brush Prairie, WA"},{"LatLng":{"Ba":45.756944,"Ca":-122.43575800000002},"InfoWindow":"\n        <address>18607 NE Erickson Rd, Brush Prairie, WA</address>\n        <p>Last place I lived with my parents</p>\n      ","originalAddress":"18607 NE Erickson Rd, Brush Prairie, WA"}

【问题讨论】:

    标签: php javascript mysql json syntax-error


    【解决方案1】:

    您没有要设置的字段:

    UPDATE `cache-test` SET field_name = 'your huge val'
    

    【讨论】:

    • 谢谢!就是这样。我认为它的工作方式类似于插入,您可以在其中定义 (a,b) ('a data','b data') 等字段。
    【解决方案2】:

    将添加斜杠替换为:

    mysql_real_escape_string()
    
    $cache = mysql_real_escape_string(preg_replace('/\s\s+/', ' ', $referrer['cache']));
    
    mysql_query("UPDATE `cache-test`  
                SET (cache = '$cache') 
              WHERE url = '".$referrer['current']."'",$con));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-22
      • 2018-09-27
      • 1970-01-01
      • 2019-11-09
      相关资源
      最近更新 更多