【发布时间】:2020-02-29 00:07:00
【问题描述】:
我正在用 php ajax 创建一个简单的库存系统。所有的销售计算都会很好地完成。将数据添加到数据库后,数据未添加到数据库中。我在控制台上没有收到任何错误。 到目前为止我尝试了什么,我附在下面。 表格数据小计
<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>
表格数据所有销售都应该添加这个表格数据,我将发送到 sales_add.php 页面。我像这样检查console.log(JSON.stringify(items)); 我在下面写的完整代码。
function add_product_to_array(item, price,qty, tot)
{
var item = [item, price,qty, tot];
items.push(item);
console.log(JSON.stringify(items));
}
我通过 Console.log 检查了表,它像这样成功显示
**[["Chocolate",32,"1",32]]
(index):237 [["Chocolate",32,"1",32],["Mango",10,"1",10]]**
我以这种方式将**var data = $('#frmSummery').serialize() + "&items=" + JSON.stringify((items))** 发送到sales.add.php 页面
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 页面 i 以这种方式接收数据
**$relative_list = $_POST['items'];
$subtotal= $_POST["total"];**
Sales.php 页面
<?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 我检查汽车 var 转储
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)
}
}
【问题讨论】:
-
欢迎来到 SO。你遇到了什么错误?
-
我在控制台上没有收到任何错误。传递数据的任何问题 var data = $('#frmSummery').serialize() + "&items=" + JSON.stringify((items));这边
-
您可以通过将其添加到您的
php文件ini_set('display_errors', 1);和error_reporting(E_ALL);来检查错误 -
我检查了现在显示的错误 Sales.php 页面这一行有问题 $stm->bind_param("iiiiii",$last_id,$item,$price,$qty,$total);未捕获的错误:调用成员函数 bind_param()