【问题标题】:Decoding JSON to save in mysql using PHP and AJAX使用 PHP 和 AJAX 解码 JSON 以保存在 mysql 中
【发布时间】:2017-01-29 12:58:49
【问题描述】:

我有这样的 div 容器:

<div class="container">
    <div class="step" id="1">
        <h2 class="title">Step 1</h2>
        <div class="image" id="1">Item 1</div>
        <div class="image" id="2">Item 2</div>
        <div class="image" id="3">Item 3</div>
    </div>
    <div class="step" id="2">
        <h2 class="title">Step 2</h2>
        <div class="image" id="4">Item 4</div>
        <div class="image" id="5">Item 5</div>
        <div class="image" id="6">Item 6</div>
    </div>
    <div class="step" id="3">
        <h2 class="title">Step 3</h2>   
        <div class="image" id="7">Item 7</div>
        <div class="image" id="8">Item 8</div>
        <div class="image" id="9">Item 9</div>
    </div>
</div>

基本上,我有脚本记录拖放并记录 div.step 类中的更改。查看图片以更好地理解

$.ajax({
        type: 'POST',
        url: 'process.php',
        data: {json: JSON.stringify(myArguments)},
        dataType: 'json'            
    });

我不知道如何在 process.php 中更进一步

如上图所示分离步骤 id 和类 id 的任何方式,以便我可以插入数据库中

谢谢。

编辑:更新 process.php (感谢 Ofir Baruch 正在工作)

<?php 
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testdb";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 


$json = $_POST['json'];
$organize = json_decode($json);

foreach($organize->{1} as $pos => $div){

$pos1 = 1;
    $sql = "INSERT INTO process VALUES (DEFAULT,'".mysqli_real_escape_string($conn,$pos1)."','".mysqli_real_escape_string($conn,$div)."')";
    if ($conn->query($sql) === TRUE) {

    } 
//Insert to the Database:
// Step: 1
// image_id: $div
// position: $pos

}

foreach($organize->{2} as $pos => $div){
//Insert to the Database:
// Step: 2
// image_id: $div
// position: $pos
    $pos1 = 2;
    $sql = "INSERT INTO process VALUES (DEFAULT,'".mysqli_real_escape_string($conn,$pos1)."','".mysqli_real_escape_string($conn,$div)."')";
    if ($conn->query($sql) === TRUE) {

    } 
}

foreach($organize->{3} as $pos => $div){
//Insert to the Database:
// Step: 3
// image_id: $div
// position: $pos
    $pos1 = 3;
    $sql = "INSERT INTO process VALUES (DEFAULT,'".mysqli_real_escape_string($conn,$pos1)."','".mysqli_real_escape_string($conn,$div)."')";
    if ($conn->query($sql) === TRUE) {

    } 
}

?>

【问题讨论】:

    标签: php jquery mysql json ajax


    【解决方案1】:

    从解码 JSON 字符串开始

    $json = $_POST['json'];
    $organize = json_decode($json);
    

    $组织结构: (如果是:'{"1":["1","2"], "2":["3","4"]}';)

    > object(stdClass)#1 (2) {
    > 
    > ["1"]=>   array(2) {
    >     [0]=> string(1) "1"
    >     [1]=> string(1) "2"   }
    > ["2"]=>   array(2) {
    >     [0]=> string(1) "3"
    >     [1]=> string(1) "4"   }
    > }
    

    现在,$organize 类的属性是步骤,也是数字,所以为了访问它们,你应该使用:

    //$organize->{1} //Access step 1 arrays of "divs"
    
    foreach($organize->{1} as $pos => $div){
    //Insert to the Database:
    // Step: 1
    // image_id: $div
    // position: $pos
    }
    
    foreach($organize->{2} as $pos => $div){
    //Insert to the Database:
    // Step: 2
    // image_id: $div
    // position: $pos
    }
    
    foreach($organize->{3} as $pos => $div){
    //Insert to the Database:
    // Step: 3
    // image_id: $div
    // position: $pos
    }
    

    请注意,您调用的变量在 2,3 循环中不存在。

    foreach($organize->{2} as $pos => $div){
        $pos1 = 2; //change to $pos2 = 2;
        $sql = "INSERT INTO process VALUES (DEFAULT,'".mysqli_real_escape_string($conn,$pos2)
    
    
    ....
    ....
    
    foreach($organize->{3} as $pos => $div){
    
        $pos1 = 3; //change to $pos3 =3 ;
        $sql = "INSERT INTO process VALUES (DEFAULT,'".mysqli_real_escape_string($conn,$pos3)
    

    【讨论】:

    • 我会尝试,但有没有办法自动访问所有步骤,而不是指定 $organize->(1)。并创建“n”个foreach。谢谢。
    • 当然,就像你提到的那样使用 foreach 循环。
    • 它几乎似乎工作了你能告诉我如何访问每个$sql = "INSERT INTO process VALUES (DEFAULT,'".mysqli_real_escape_string($conn,HOW TO GET STEP ID)."','".mysqli_real_escape_string($conn,$div)."')";里面的step_id吗?谢谢!
    • step_id 基本上是“{$n}”。
    • 好的,所以它部分工作为(在以下情况下:'{"1":["1","2"], "2":["3","4"]} ';) 它只运行第一个 foreach 循环并跳过休息,所以我的数据库正在保存 1.[1,2] 任何想法?非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多