Note that there are some explanatory texts on larger screens.

plurals
  1. POhow can I access my json encoded data from PHP into a mysql query
    text
    copied!<p>I have been on a long mission here, I have gotten to my last step in my program and I can't seem to figure out how to crack open a JSON string from my Ajax request sent to my PHP.</p> <p>My Jquery code looks like this;</p> <pre><code>$('#formElem').submit(function(){ var data = {}; $("input[name=scene]").each(function() { data[$(this).val()] = { scene: $(this).val(), int_text: $(this).next("input[name=int_ext]").val(), desc: $(this).next().next("input[name=scene_desc]").val(), day_night: $(this).next().next().next("input[name=day_night]").val() }; }); $.post("script.import.php", { action: 'save-import', data: data // as-is //data: JSON.stringify(data) // with stringify }, function(resp) { console.log(eval('('+resp.scene+')')); console.log(eval(resp.int_ext)); console.log(eval(resp.desc)); console.log(resp.day_night); }); return false; }); </code></pre> <p>and my PHP is:</p> <pre><code>if($_POST['save-import']){ $scenes = json_decode($_POST['data']); echo($scenes); } </code></pre> <p>A snippet of what my JSON response looks like:</p> <pre><code>WITHOUT stringify() action save-import data[100][day_night] NIGHT data[100][desc] SAPNA'S BEDROOM data[100][int_text] INT data[100][scene] 100 data[101][day_night] NIGHT data[101][desc] LIVING ROOM data[101][int_text] INT data[101][scene] 101 data[102][day_night] NIGHT data[102][desc] SHAH HOUSE, FRONT YARD data[102][int_text] EXT data[102][scene] 102 data[103][day_night] NIGHT data[103][desc] SHAH HOUSE, FRONT PORCH data[103][int_text] EXT WITH stringify() {"2":{"scene":"2","int_text":"INT","desc":"SAPNA'S BEDROOM","day_night":"MORNING"},"9":{"scene":"9","int_text":"INT","desc":"BMW","day_night":"NIGHT"},"19":{"scene":"19","int_text":"INT","desc":"SAPNA'S BEDROOM","day_night":"DAY"},"21":{"scene":"21","int_text":"INT","desc":"KITCHEN","day_night":"DAY"},"22":{"scene":"22","int_text":"INT","desc":"HALLWAY","day_night":"DAY"},"23":{"scene":"23","int_text":"INT","desc":"TEMPLE ROOM","day_night":"DAY"},"24":{"scene":"24","int_text":"INT","desc":"LIVING ROOM","day_night":"NIGHT"} </code></pre> <p>when the ajax is sent to my php to get processed i need to be able to break apart the json response to accomplish this query</p> <pre><code>mysql_query("INSERT INTO (`project_id`, `scene`, `int_ext`, `scene_desc`, `day_night`) VALUES ('10', 'data.scene', 'data.int_ext', 'data.desc', 'data.day_night')"); </code></pre> <p>what the JSON should read in english is</p> <pre><code>scene: 24 int_ext: "INT" desc: "LIVING ROOM" day_night: "NIGHT" </code></pre> <p>as one query, and do to that for every scene in the JSON string.</p> <p>EDIT:</p> <pre><code>with var_dump string(11463) ""{\\\"2\\\":{\\\"scene\\\":\\\"2\\\",\\\"int_text\\\":\\\"INT\\\",\\\"desc\\\":\\\"SAPNA\\'S BEDROOM\\\",\\\"day_night\\\":\\\"MORNING\\\"},\\\"9\\\":{\\\"scene\\\":\\\"9\\\",\\\"int_text\\\":\\\"INT\\\",\\\"desc\\\":\\\"BMW\\\",\\\"day_night\\\":\\\"NIGHT\\\"},\\\"19\\\":{\\\"scene\\\":\\\"19\\\",\\\"int_text\\\":\\\"INT\\\",\\\"desc\\\":\\\"SAPNA\\'S BEDROOM\\\",\\\"day_night\\\":\\\"DAY\\\"},\\\"21\\\":{\\\"scene\\\":\\\"21\\\",\\\"int_text\\\":\\\"INT\\\",\\\"desc\\\":\\\"KITCHEN\\\",\\\"day_night\\\":\\\"DAY\\\"},\\\"22\\\":{\\\"scene\\\":\\\"22\\\",\\\"int_text\\\":\\\"INT\\\",\\\"desc\\\":\\\"HALLWAY\\\",\\\"day_night\\\":\\\"DAY\\\"},\\\"23\\\":{\\\"scene\\\":\\\"23\\\",\\\"int_text\\\":\\\"INT\\\",\\\"desc\\\":\\\"TEMPLE ROOM\\\",\\\"day_night\\\":\\\"DAY\\\"},\\\"24\\\":{\\\"scene\\\":\\\"24\\\",\\\"int_text\\\":\\\"INT\\\",\\\"desc\\\":\\\"LIVING ROOM\\\",\\\"day_night\\\":\\\"NIGHT\\\"},\\\"26\\\":{\\\"scene\\\":\\\"26\\\",\\\"int_text\\\":\\\"INT\\\",\\\"desc\\\":\\\"TREE HOUSE\\\",\\\"day_night\\\":\\\"NIGHT\\\"} </code></pre> <p>EDIT: with stripslashes</p> <pre><code>Catchable fatal error: Object of class stdClass could not be converted to string in /home/content/06/5679006/html/v3/test.php on line 4 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload