-2

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)
  }
}
deva ram
  • 65
  • 7
  • Welcome to SO. What error did you encounter? – Jordan Lipana Nov 03 '19 at 13:16
  • i didn't get any error on console. any problem passing the data var data = $('#frmSummery').serialize() + "&items=" + JSON.stringify((items)); this way – deva ram Nov 03 '19 at 13:18
  • You can check the error by adding this on your `php` file `ini_set('display_errors', 1);` and `error_reporting(E_ALL);` – Jordan Lipana Nov 03 '19 at 13:20
  • i checked error displayed now Sales.php page this line had a problem $stm->bind_param("iiiii",$last_id,$item,$price,$qty,$total); Uncaught Error: Call to a member function bind_param() – deva ram Nov 03 '19 at 13:29

1 Answers1

0

Your bind param doesn't match the total on your prepare. Remove one ? on it:

$stm = $conn->prepare("INSERT INTO sales_product(sales_id,item,price,qty,total)
          VALUES (?,?,?,?,?)");
Jordan Lipana
  • 379
  • 3
  • 16
  • i done it. but not working . this way i got the json data $relative_list = $_POST['items']; is it correct. – deva ram Nov 03 '19 at 13:37
  • Fatal error: Uncaught Error: Call to a member function bind_param() on boolean in C:\xampp\htdocs\icepos\sales_add.php:32 Stack trace: #0 {main} thrown in C:\xampp\htdocs\icepos\sales_add.php on line 32
    this line $stm->bind_param("iiiii",$last_id,$item,$price,$qty,$total);
    – deva ram Nov 03 '19 at 13:41
  • Try moving `$stm->bind_param("iiiii",$last_id,$item,$price,$qty,$total);` below `$total= $relative_list[$x]['tot'];` – Jordan Lipana Nov 03 '19 at 13:44
  • Warning: Illegal string offset 'item' in C:\xampp\htdocs\icepos\sales_add.php on line 33

    Warning: Illegal string offset 'price' in C:\xampp\htdocs\icepos\sales_add.php on line 34

    Warning: Illegal string offset 'qty' in C:\xampp\htdocs\icepos\sales_add.php on line 35

    Warning: Illegal string offset 'tot' in C:\xampp\htdocs\icepos\sales_add.php on line 36

    Fatal error: Uncaught Error: Call to a member function bind_param() on
    – deva ram Nov 03 '19 at 13:46
  • Can you you `var_dump` the `$relative_list` – Jordan Lipana Nov 03 '19 at 13:49
  • how to var_dump sir var_dump = $relative_list like this – deva ram Nov 03 '19 at 13:53
  • string(45) "[["Chocolate",32,"1",32],["Mango",10,"1",10]]" got this result var_dump – deva ram Nov 03 '19 at 13:56
  • Change `$relative_list = $_POST['items'];` to `$relative_list = json_decode($_POST['items']);` – Jordan Lipana Nov 03 '19 at 14:01
  • Notice: Undefined index: item in C:\xampp\htdocs\icepos\sales_add.php on line 39

    Notice: Undefined index: price in C:\xampp\htdocs\icepos\sales_add.php on line 40

    Notice: Undefined index: qty in C:\xampp\htdocs\icepos\sales_add.php on line 41

    Error displayed
    – deva ram Nov 03 '19 at 14:06
  • Change also `$relative_list[$x]['item']` to `$relative_list[$x][0];` and `$relative_list[$x]['price']` to `$relative_list[$x][1];` and `$relative_list[$x]['qty']` to `$relative_list[$x][2];` and `$relative_list[$x]['tot']` to `$relative_list[$x][3];` – Jordan Lipana Nov 03 '19 at 14:08
  • i attached the result on console var_dump above please check question editedddd – deva ram Nov 03 '19 at 14:09
  • Uncaught Error: Call to a member function bind_param() of this line again $stm->bind_param("iiiii",$last_id,$item,$price,$qty,$total); – deva ram Nov 03 '19 at 14:14
  • Since `$item` is a string, update the param to this `$stm->bind_param("isiii",$last_id,$item,$price,$qty,$total)` – Jordan Lipana Nov 04 '19 at 05:04