i am creating a simple inventory system in php ajax.all sales calculation will be done it well. after add the data into the database data is not added in to the database.i didn't get any error on console. what i tried so far i attached below . Form data Subtotal
<form class="form-horizontal" role="form" id="frmSummery">
<div>
<label>Total</label>
<input type="text" style="color: yellow; background: black; font-size: 30px;" id="total" name="total" placeholder="Total" class="form-control" required>
</div>
<Form>
Table data all sales should added this table data i am going to send in to sales_add.php page . i checked like this console.log(JSON.stringify(items)); full code i write below.
function add_product_to_array(item, price,qty, tot)
{
var item = [item, price,qty, tot];
items.push(item);
console.log(JSON.stringify(items));
}
Table i checked through Console.log it display successfully like this
**[["Chocolate",32,"1",32]]
(index):237 [["Chocolate",32,"1",32],["Mango",10,"1",10]]**
i sending this way **var data = $('#frmSummery').serialize() + "&items=" + JSON.stringify((items))** to sales.add.php page
function addProject()
{
var data = $('#frmSummery').serialize() + "&items=" + JSON.stringify((items));
$.ajax({
type: "POST",
url: "sales_add.php",
dataType: 'JSON',
data: data,
success: function (data) {
console.log(_data);
alert("Success");
},
error: function (xhr, status, error) {
alert(xhr);
console.log(xhr.responseText);
}
});
}
Sales.php page i Receiving data like this way
**$relative_list = $_POST['items'];
$subtotal= $_POST["total"];**
Sales.php page
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "icepos";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$relative_list = $_POST['items'];
$stmt = $conn->prepare("INSERT INTO sales(subtotal)VALUES (?)");
$stmt->bind_param("s",$subtotal);
$subtotal= $_POST["total"];
if ($stmt->execute()) {
$last_id = $conn->insert_id;
} else {
}
for($x = 0; $x < count($relative_list); $x++)
{
$stm = $conn->prepare("INSERT INTO sales_product(sales_id,item,price,qty,total)
VALUES (?,?,?,?,?,?)");
$stm->bind_param("iiiii",$last_id,$item,$price,$qty,$total);
$item= $relative_list[$x]['item'];
$price= $relative_list[$x]['price'];
$qty= $relative_list[$x]['qty'];
$total= $relative_list[$x]['tot'];
if ($stm->execute()) {
}
else {
echo $conn->error;
}
$stm->close();
$stmt2->close();
}
$stmt->close();
}
?>
Console.log i check car var dump
array(3) {
[0]=>
array(4) {
[0]=>
string(9) "Chocolate"
[1]=>
int(32)
[2]=>
string(1) "1"
[3]=>
int(32)
}
[1]=>
array(4) {
[0]=>
string(5) "Mango"
[1]=>
int(10)
[2]=>
string(1) "1"
[3]=>
int(10)
}
[2]=>
array(4) {
[0]=>
string(6) "Venila"
[1]=>
int(22)
[2]=>
string(1) "1"
[3]=>
int(22)
}
}