gpt4 book ai didi

php - 在ajax post mysql上形成序列化非法字符串偏移量 ''

转载 作者:行者123 更新时间:2023-11-29 06:51:12 24 4
gpt4 key购买 nike

我正在尝试通过表单序列化方法插入数据。这是我的ajax。

$('#AddInventory').on('click', function () {
var data = $("#validated_form").serialize();
alert(data);
$.ajax({
data: {
'AddInventory': data
},
type: "POST",
url: "Product_DB.php",
dataType: 'json',
success: function (data) {
alert(data);
}
});
});

在警报中,我收到了类似 this 的发送数据.

这是我的数据库文件,我根据建议进行了更改

if (isset($_POST["AddInventory"])) {

$AddInventory = $_POST["AddInventory"];

$sql = "insert into `tblinventory` ( Bill_No, SKU_No, Purchase_Price, Discount_Price, Discount, Qty, , Deal, Created_Date, Modified_Date, IsDeleted)values ('" . $AddInventory["Bill_No"] . "','1','" . $AddInventory["Purchase_Price"] . "' ,'" . $AddInventory["Discount_Price"] . "','" . $AddInventory["Discount"] . "','" . $AddInventory["Qty"] . "',,'" . $AddInventory["Deal"] . "',now() ,now() ,'0' )";

if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "" . mysqli_error($conn);
}
exit();
}

编辑

现在我得到了

illegal string offset 'Bill_No'

错误;

我在输出中没有得到任何响应,在控制台中也没有显示脚本错误

我尝试搜索并实现我认为相关的代码,尽管如此,问题仍然存在,任何帮助将是可观的

编辑2

我测试了var_dump,我得到了这样的响应

enter image description here

最佳答案

您将 AddInventory 中的整个data 作为 URL 编码格式传递,使用 parse_str() 您可以将其作为数组获取。所以在 PHP 中像这样访问

  if(isset($_POST["AddInventory"])){

parse_str(urldecode($_POST["AddInventory"]),$AddInventory);


$sql = "insert into `tblinventory` ( Bill_No)values ('".$AddInventory["Bill_No"]."' )";

if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
}
}

重要提示:您的代码容易受到 SQL 注入(inject)攻击,请准备好您的语句(使用 PDO)

关于php - 在ajax post mysql上形成序列化非法字符串偏移量 '',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47387427/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com