Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将我的json编码数据从PHP访问到mysql查询中_Php_Jquery_Mysql_Json - Fatal编程技术网

如何将我的json编码数据从PHP访问到mysql查询中

如何将我的json编码数据从PHP访问到mysql查询中,php,jquery,mysql,json,Php,Jquery,Mysql,Json,我在这里执行了很长的任务,我已经完成了程序的最后一步,我似乎不知道如何从发送到PHP的Ajax请求中打开JSON字符串 我的Jquery代码如下所示 $('#formElem').submit(function(){ var data = {}; $("input[name=scene]").each(function() { data[$(this).val()] = { scene: $(this).val(),

我在这里执行了很长的任务,我已经完成了程序的最后一步,我似乎不知道如何从发送到PHP的Ajax请求中打开JSON字符串

我的Jquery代码如下所示

$('#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;
});
我的PHP是:

if($_POST['save-import']){
    $scenes = json_decode($_POST['data']);
    echo($scenes);
}
我的JSON响应的一个片段:

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"}
当ajax被发送到我的php进行处理时,我需要能够分解json响应来完成这个查询

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')");
JSON应该用英语读的是

scene: 24
int_ext: "INT"
desc: "LIVING ROOM"
day_night: "NIGHT"
作为一个查询,并对JSON字符串中的每个场景执行该操作

编辑:

编辑:带斜杠

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

根据您发布的var_转储,尝试以下操作:

if($_POST['save-import']){
    $scenes = json_decode(stripslashes($_POST['data']));
    echo($scenes);
}

在JSON对象被传输之前对其进行转义似乎有点疯狂。

以下内容对我很有用。显然,那里的斜杠确实出了问题:

$json = <<<JSON
    {\\\"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\\\"}}
JSON;

$json = stripslashes(stripslashes($json));

$data = json_decode($json);

foreach($data as $item) {
    /*
    Item looks like this:
    stdClass Object
            (
                [scene] => 2
                [int_text] => INT
                [desc] => SAPNA'S BEDROOM
                [day_night] => MORNING
            )
    */
}
$json=SAPNA的卧室
[白天/晚上]=>早上
)
*/
}

在php中,替换echo($scenes);使用var_dump($scenes);我用var_dump编辑数据当你将数据转换成自然数组而不是对象时,数据是什么样子的?e、 g.json_解码($_POST['data'],true);我已经试过了,我无法访问关联的arrayOH,我怎么能只使用我的$\u POST['data']变量呢?比如说$json=$\u POST['data'],我把它添加到了原始帖子的底部
$json = <<<JSON
    {\\\"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\\\"}}
JSON;

$json = stripslashes(stripslashes($json));

$data = json_decode($json);

foreach($data as $item) {
    /*
    Item looks like this:
    stdClass Object
            (
                [scene] => 2
                [int_text] => INT
                [desc] => SAPNA'S BEDROOM
                [day_night] => MORNING
            )
    */
}