使用 JSON 更新记录时 MySQL 语法错误

发布于 2024-11-10 12:29:47 字数 1661 浏览 7 评论 0原文

我主要是一名 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"}

Im mainly a JavaScript guy and im building a library for a client and the library caches some data in MySQL per HTTP_REFERRER. If the data is different than it is on the server it updates the cache. I don't know what exactly I'm doing wrong but its saying there is a syntax error with this:

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); }

The error Im getting is:

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 '(cache) SET ('[{\"LatLng\":{\"Ba\":45.531124,\"Ca\":-122.68374699999998},\"InfoW' at line 1

The data being sent looks like (before being stripped and slashes added, of course):

[{"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"}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幻想少年梦 2024-11-17 12:29:47

您没有要设置的字段:

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

You don't have the field you wish to set:

UPDATE `cache-test` SET field_name = 'your huge val'
硬不硬你别怂 2024-11-17 12:29:47

将 addslash 替换为:

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));

Replace addslashes with:

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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文