想請教我在ajax傳值進php
在使用foreach insert進sql 卻進不了sql
我有測試過不使用foreach 直接在php下 sql 是可行的 代表ajax有成功傳值過去php裡
但我在使用foreach時 就會失敗
所以我在想是不是我在ajax與foreach上哪邊少做了什麼動作了?
請求幫忙 以下是code
<script>
var summaryData = $("#addcontent").jqxGrid('getcolumnaggregateddata',
'total', ['sum']);
var totalamount = "&total_deductions=" + summaryData.sum;
var rows = $('#addcontent').jqxGrid('getrows');
var result = "";
for(var i = 0; i < rows.length; i++)
{
var row = rows[i];
result += "&name=" + row.id + "&quantity=" + row.quantity +
"&purchase_price=" + row.price + "&total=" + row.total + "\n";
}
$.ajax({
url:"edit.php",
data: 'id='+select.id + '&total_balance='+select.total_amount +
totalamount + result,
type: "POST",
success: function(msg){
alert("成功");
},
error:function(xhr, ajaxOptions, thrownError){
alert("錯誤");
}
});
</script>
<?php
require_once("dbtools.inc.php");
//入商品
$product_id = $_POST['name'];
$quantity = $_POST['quantity'];
$purchase_price = $_POST['purchase_price'];
$total= $_POST['total'];
//建立資料連接
$link = create_connection();
//釋放 $result 佔用的記憶體
mysql_free_result($result);
//執行 SQL 命令,新增此帳號
foreach($product_id as $v){
$sql = "INSERT INTO pre_item ( product_id ) VALUES ( '$v' )";
$result = execute_sql("pos", $sql, $link);
}
//關閉資料連接
mysql_close($link);
?>