gpt4 book ai didi

php - 使用 PHP 和 AJAX 解码 JSON 以保存在 mysql 中

转载 作者:行者123 更新时间:2023-11-29 07:05:17 25 4
gpt4 key购买 nike

我有这样的 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 类中的更改。查看图片以更好地理解

enter image description here

$.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) {

}
}

?>

最佳答案

首先解码 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)

关于php - 使用 PHP 和 AJAX 解码 JSON 以保存在 mysql 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41921155/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com