2016-09-26 57 views
1

在PHP中,用下面的代碼:php如何插入逗號分隔json格式的多個字符串作爲一個值到mysql表?

$j_id = $jData[$j]['itop_id']; 
$res = '"name":"' . $jData[$j]['label'] . '","x":'. $jData[$j]['x'] . ',"y":' . $jData[$j]['y'] . ',"url":"' . $jData[$j]['icon_url'] . '"'; 

我能得到的值:

$j_id = 1; 
$res = "name":"AOL network","x":1032.5,"y":180,"url":"http://localhost/pathfind/web/env-production/itop-config-mgmt/images/business-process.png" 

當我嘗試插入的$ j_id價值觀和$資源到MySQL表用下面的代碼:

$sql = 'INSERT INTO coordinate_info (id,coordinate_res) VALUES ('.$j_id.','. $res.')'; 

我得到了錯誤:

Error: INSERT INTO coordinate_info (id,coordinate_res) VALUES (1,"name":"AOL network","x":1032.5,"y":180,"url":"http://localhost/pathfind/web/env-production/itop-config-mgmt/images/business-process.png") You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ':"AOL network","x":1032.5,"y":180,"url":"http://localhost/pathfind/web/env-produ' at line 1 

我想插入逗號分隔多個JSON格式的字符串到MySQL表中作爲一個列值。 你能給我一些建議嗎? 在php中,如何將逗號分隔的值作爲一個值保存到mysql表中?

+2

預處理語句或逃避你的字符串將是不錯 – Naruto

+0

將幫助:'$ SQL =「INSERT INTO coordinate_info(ID,coordinate_res) VALUES(「。$ j_id。」,'$ res')「;' – devpro

回答

0

你應該真的使用準備好的語句和參數。如果安全不是一個問題,你可以改變你的SQL看起來像這樣:

$sql = 'INSERT INTO coordinate_info (id,coordinate_res) VALUES ('.$j_id.',"'. $res.'")'; 
+0

您的查詢類似於INSERT INTO mytable(test)VALUES(」「test」「)''不起作用,我想。 – devpro

+0

如果你回顯我的$ sql變量,你會得到這個:INSERT INTO coordinate_info(id,coordinate_res)VALUES(1,「string」) – CptMisery

+0

不,我沒有檢查過。 – devpro

相關問題