[未使用Php Ajax Json将记录添加到mysql数据库中

问题描述 投票:-1回答:1

我正在用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>

[Table data所有销售都应添加此表数据,我将发送到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页面我以这种方式接收数据

   **$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();
}
?>
php mysql json ajax
1个回答
0
投票

您的bind param与您的prepare的总数不符。删除一个?

$stm = $conn->prepare("INSERT INTO sales_product(sales_id,item,price,qty,total)
          VALUES (?,?,?,?,?)");
© www.soinside.com 2019 - 2024. All rights reserved.