我有一个javascript来创建json数组,它有一个ajax post方法,我必须知道这个json数组应该如何解码在PHP端?检索json数组从php json decode
我的JavaScript函数:
var invoices = {invoice: [{ "customerName" : "John" ,"reciptionName" : "Doe" ,"datee" : "2017-09-09" ,"total" : tot }], invoiceLine: []};
for(i=1; i<=count+arr.length-1; i++){
var ss = String(i);
if(arr.includes(ss)){
continue;
}
invoices.invoiceLine.push({
"itemName" : document.getElementById('item'+i).value ,
"qty" : document.getElementById('inputQty'+i).value ,
"value" : document.getElementById('value'+i).value
});
}
$.ajax({
type: "POST",
url: "saveInvoice.php",
data: {json : JSON.stringify(invoices)},
cache: false,
success: function(data) {
alert(data);
location.reload();
}
});
,这是我的PHP:。(它不会将数据保存到数据库中,我想先发票数据库中的数据保存现在然后我可以使用invoiveLine数据。另一个表中插入)
$dbhost = "localhost";
$dbuser = "root";
//$dbpass = "dbpassword";
$dbname = "inventory";
$jsonInvoice = json_decode($POST['invoices'],true);
$customerName = $jsonInvoice.["invoice"][0].["customerName"];
$reciptionName = $jsonInvoice.["invoice"][0].["reciptionName"];
$date = $jsonInvoice.["invoice"][0].["datee"];
$total = $jsonInvoice.["invoice"][0].["total"];
mysql_connect($dbhost, $dbuser, '');
mysql_select_db($dbname) or die(mysql_error());
$query = "insert into invoice (customerName,reciptionName,date,total) values ('$customerName','$reciptionName','$date',$total)";
$qry_result = mysql_query($query) or die(mysql_error());
$insertId = mysql_insert_id();
echo $insertId;
是什么'$ POST [“发票”]'是什么样子?另外,'mysql_'构造函数是[**从PHP 5.5开始不推荐使用](https://wiki.php.net/rfc/mysql_deprecation),并且彻底[**在PHP 7 **中被移除](https ://wiki.php.net/rfc/remove_deprecated_functionality_in_php7#extmysql)。请考虑切换到[** MySQLi **](http://php.net/manual/en/book.mysqli.php)或[** PDO **](http://php.net/manual/ en/book.pdo.php),确保您还使用[**参数化查询**](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)来防止SQL注入: ) –
首先,不要使用mysql_query,使用mysqli或者PDO并且参数化你的查询字符串,这样你就不会受到SQL注入的影响。 – Difster
我更正$ POST到$ _POST,但仍然得到错误 –