I want to store a php array as JSON in mysql. for that I have meeting_point_json column with type=’longtext’.
here is the array:
Array
(
[1] => Array
(
[date] => 23/4/2012
[meeting_time] => 23:04
[meeting_place] => town hall
[venue] => London
[opponents] => Tigers
[official_incharge] => Mr Putin
)
[2] => Array
(
[date] => 23/4/2050
[meeting_time] => 13:04
[meeting_place] => chief office
[venue] => Kenya
[opponents] => Peococks
[official_incharge] => Mr Black
)
[3] => Array
(
[date] => dsad
[meeting_time] => sadas
[meeting_place] => jjjjj
[venue] => jjjj
[opponents] => dasds
[official_incharge] => asad
)
)
and here is the php code:
$data = json_encode($_POST['team_meeting_pt']);
$sql = "UPDATE yami_sub_team set meeting_point_json = $data where id = $subteam_id";
if(mysql_query($sql)){
exit("Done!");
}else{
die('Something went wrong, changes not saved. Error details: ' . mysql_error());
}
it should work but Instead, I get an error:
Something went wrong, changes not saved. Error details: 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 '"1":{"date":"23\/4\/2012","meeting_time":"23:04","meeting_place":"town hall","ve' at line 1
Any idea what I am doing wrong here?
See at:
$datavariable change with".$data."and
$subteam_idvariable with".$subteam_id."remember the sql query only sent as strings, not variable inside there…
and do it to your all way to write PHP. Always wrap variable with “..” and ‘..’
“..” and ‘..’ are depending by your strip wrapper:
For example:
If look like this
$string = mysql_query("SELECT DATA FROM ".$variable."");So, you must use “..” wrapper !
and if like this
$string = mysql_query('SELECT DATA FROM '.$variable.'');So, you must use ‘..’ wrapper !
Good luck friend