【问题标题】:Record is not added into the mysqldatabase using Php Ajax Json [duplicate]使用 Php Ajax Json 未将记录添加到 mysql 数据库中 [重复]
【发布时间】: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() + "&amp;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()

标签: php mysql json ajax


【解决方案1】:

您的 bind param 与您的 prepare 上的总数不匹配。删除一个?就可以了:

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

【讨论】:

  • 我做到了。但不工作。这样我得到了 json 数据 $relative_list = $_POST['items'];对吗?
  • 致命错误:未捕获错误:调用 C:\xampp\htdocs\icepos\sales_add.php:32 中布尔值的成员函数 bind_param() 堆栈跟踪:# 0 {main} 在 C:\xampp\htdocs\icepos\sales_add.php32
    这行 $stm->bind_param("iiiiii" ,$last_id,$item,$price,$qty,$total);
  • 尝试将$stm-&gt;bind_param("iiiii",$last_id,$item,$price,$qty,$total);移到$total= $relative_list[$x]['tot'];下方
  • 警告C:\xampp\htdocs\icepos\sales_add.php 中的非法字符串偏移 'item' 在行 33

    警告C:\xampp\htdocs\icepos\sales_add.php 中的非法字符串偏移 'price' 在第 34

    警告:在 C:\xampp\htdocs\icepos\sales_add.php 中存在非法字符串偏移量 'qty'第 35

    警告C:\xampp\htdocs\icepos\sales_add.php 中的非法字符串偏移 'tot' b> 在第 36 行

    致命错误:未捕获的错误:调用成员函数 bind_param() on
  • 你能var_dump$relative_list
猜你喜欢
  • 2012-08-15
  • 1970-01-01
  • 2015-05-21
  • 2012-03-10
  • 1970-01-01
  • 2020-11-28
  • 2014-10-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多