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

u7up0aaq  于 2022-12-01  发布在  PHP
关注(0)|答案(1)|浏览(93)

此问题在此处已有答案

What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such(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>

表数据所有销售都应该添加这个表数据,我要发送到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 page i像这样接收数据

**$relative_list = $_POST['items'];
      $subtotal= $_POST["total"];**

销售.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();
}
?>

控制台日志检查汽车变量转储

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)
  }
}
6fe3ivhb

6fe3ivhb1#

您的bind param与您的prepare上的总数不符。请移除其中一个?

$stm = $conn->prepare("INSERT INTO sales_product(sales_id,item,price,qty,total)
          VALUES (?,?,?,?,?)");

相关问题