反正有没有将json数组发送到服务器端php并将其值插入表中? [英] Is there anyway to send a json array to server side php and insert it's values in a table?
问题描述
我在客户端使用 Angular 8,在服务器端使用 PHP 7.我在使用该数组的值通过查询插入它们时遇到问题.
i am using ANgular 8 in client side and PHP 7 in server side . i had problem to use the values of that array to insert them by query .
我通过 print_r 显示数组,它显示如下内容:
i displayed the array by print_r and it show something like this:
Array
(
[0] => stdClass Object
(
[idprod] => 8
[prix] => 2
[qte] => 1
[refCmd] => 35
)
[1] => stdClass Object
(
[idprod] => 9
[prix] => 2.4
[qte] => 5
[refCmd] => 35
)
)
问题是如何将该数组的每个对象插入名为 regrouper 的表中?
the question is how to insert every object of that array in table called regrouper ?
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Credentials: true ");
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header("Access-Control-Allow-Headers: X-Custom-Header, Origin, Content-
Type , Authorisation , X-Requested-With");
header("Content-Type: application/json; charset=UTF-8 ");
$json = file_get_contents('php://input');
$decoded = json_decode($json);
$tab = $decoded->tab;
function conn()
{
$dbhost = "localhost";
$user = "root";
$pass = "";
$db = "smart";
$conn = new PDO('mysql:host=localhost;dbname=smart', $user, $pass);
return $conn;
}
$db = conn();
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$p = $db->prepare("INSERT INTO regrouper (refCommande, refProduit, prixP,
qteP) VALUES(:refCmd,:refProduit,:prix,qte)");
foreach ($tab as $item) {
$p->execute([json_decode($item)]);
}
echo json_encode(true);
?>
我希望表格重组器将第一个对象放在一行中,将第二个对象放在另一行中
i expect the table regrouper to have the first object on a row and the second object in another row
推荐答案
你不需要调用 json_decode()
两次.你已经解码了
You don't need to call json_decode()
twice. You already decoded it when you did
$decoded = json_decode($json);
因此您在插入时不需要使用 json_decode($item)
.
so you don't need to use json_decode($item)
when inserting.
对 json_decode()
使用 true
第二个参数,以便它为每个项目创建一个关联数组而不是一个对象.然后你可以将该数组直接传递给 $p->execute()
.您还需要使用 $decoded['tab']
而不是 $decoded->tab
.
Use the true
second argument to json_decode()
so that it creates an associative array instead of an object for each item. Then you can pass that array to $p->execute()
directly. You also need to use $decoded['tab']
instead of $decoded->tab
.
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Credentials: true ");
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header("Access-Control-Allow-Headers: X-Custom-Header, Origin, Content-
Type , Authorisation , X-Requested-With");
header("Content-Type: application/json; charset=UTF-8 ");
$json = file_get_contents('php://input');
$decoded = json_decode($json, true);
$tab = $decoded['tab'];
function conn()
{
$dbhost = "localhost";
$user = "root";
$pass = "";
$db = "smart";
$conn = new PDO('mysql:host=localhost;dbname=smart', $user, $pass);
return $conn;
}
$db = conn();
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$p = $db->prepare("INSERT INTO regrouper (refCommande, refProduit, prixP, qteP)
VALUES(:refCmd,:refProduit,:prix,qte)");
foreach ($tab as $item) {
$p->execute($item);
}
echo json_encode(true);
这篇关于反正有没有将json数组发送到服务器端php并将其值插入表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!