1

in php , with the following code:

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

I can get the values :

$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"

When I try to insert the values of $j_id and $res into mysql table with the following code:

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

I got the error:

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

I want to insert comma separated multiple strings in json format into mysql table as one column value . Could you give me some suggestions ? In php , how can I save the comma separated values into mysql table as one value ?

Anwar Ahmat
  • 233
  • 5
  • 14

1 Answers1

0

You should really use prepared statements and parameters. If security isn't an issue you can just change your SQL to look like this:

$sql = 'INSERT INTO coordinate_info (id,coordinate_res) VALUES ('.$j_id.',"'. $res.'")';
CptMisery
  • 600
  • 4
  • 15
  • your query something like `INSERT INTO mytable (test) VALUES (""test"")` will not work, i think. – devpro Sep 26 '16 at 14:40
  • If you echo my $sql variable you get this: INSERT INTO coordinate_info (id,coordinate_res) VALUES (1, "string") – CptMisery Sep 26 '16 at 16:40
  • no, its not, i have checked. – devpro Sep 26 '16 at 16:49
  • Dude, if you have this code `$j_id = 1; $res = 'string'; $sql = 'INSERT INTO coordinate_info (id,coordinate_res) VALUES ('.$j_id.',"'. $res.'")'; echo $sql;` You get the following output: `INSERT INTO coordinate_info (id,coordinate_res) VALUES (1, "string")` – CptMisery Sep 26 '16 at 18:33
  • Note that OP not having string value in single quote – devpro Sep 26 '16 at 19:07