Я создаю простую систему инвентаризации, используя php json ajax
. Отображаются данные этого формата. Я проверил через консоль. Я хочу передать значения ключей на страницу php.
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) "2"
[3]=>
int(20)
}
[2]=>
array(4) {
[0]=>
string(6) "Venila"
[1]=>
int(22)
[2]=>
string(1) "3"
[3]=>
int(66)
}
}
это моя php-страница
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$relative_list = json_decode($_POST['items']);
var_dump($relative_list);
for($x = 0; $x < count($relative_list); $x++)
{
$stm = $conn->prepare("INSERT INTO sale_product(item,price,qty,total);
VALUES (?,?,?,?)");
$stm->bind_param("ssss",$item,$price,$qty,$total);
//this how i passed the keyvalues
$item= $relative_list[$x]['0'];
$price= $relative_list[$x][1];
$qty= $relative_list[$x][2];
$total= $relative_list[$x][3];
if ($stm->execute()) {
}
else {
echo $conn->error;
}
$stm->close();
}
}
это код, который я пытался добавить в базу данных, я получил ошибку Uncaught Error: вызов функции-члена bind_param () для логического значения в C: \ xampp \ htdocs \ icepos \ sales_add.php: 40 Трассировка стека: эта строка $ stm-> bind_param ("ssss", $ item, $ price, $ qty, $ total);отображается ошибка
полный код Jquery
var items = [];
var price = 0;
var count = 0;
function add_row(del,inc, item, price,qty, tot, )
{
var row = '<tr><td>' + del + '</td> <td>' + inc + '</td><td>' + item + '</td><td>' + price + '</td><td>' + qty + '</td><td>' + tot + '</td></tr>'
$('#product_list tbody').append(row);
$("table tbody").append(row);
}
function add() {
if(a==="Chocolate")
{
item = "Chocolate";
price =32;
}
else if(a==="Mango")
{
item = "Mango";
price =10;
}
else if(a==="Venila")
{
item = "Venila";
price =22;
}
var qty = $('#qty').val();
tot = qty * price;
var btndelete = '<button type="button" class="btn btn-success btn-xs" data-dismiss="modal" onclick="delete_items(this)">delete</button>';
count = count + 1;
add_row(btndelete,count, item, price,qty, tot );
add_product_to_array(item, price,qty, tot);
}
function delete_items(e)
{
total_cost = parseInt($(e).parent().parent().find('td:last').text(),10);
total -= total_cost;
$('#total').val(total);
$(e).parent().parent().remove();
}
$('#reset').click(function()
{
location.reload();
});
function add_product_to_array(item, price,qty, tot)
{
var item = [item, price,qty, tot];
items.push(item);
console.log(JSON.stringify(items));
}
function addProject()
{
var data = "&items=" + JSON.stringify((items));
$.ajax({
type: "POST",
url: "sales_add.php",
dataType: 'JSON',
data: data,
success: function (data)
{
alert("Success");
},
error: function (xhr, status, error) {
alert(xhr);
console.log(xhr.responseText);
}
});
}