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