[請益] ajax傳到foreach insert sql 問題求救

看板PHP作者 (Deicide)時間11年前 (2015/01/04 19:18), 編輯推噓2(207)
留言9則, 6人參與, 最新討論串1/1
想請教我在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); ?> -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 49.158.69.251 ※ 文章網址: http://www.ptt.cc/bbs/PHP/M.1420370320.A.DD3.html

01/04 19:23, , 1F
錯誤訊息是?
01/04 19:23, 1F

01/04 19:41, , 2F
name=xxx -> name[]=xxx 試試?
01/04 19:41, 2F

01/04 19:55, , 3F
mecumi 好像是foreach的部份出問題 但沒console
01/04 19:55, 3F

01/04 19:56, , 4F
Darkland 不好意思 請問name[]=xxx是要在php裡做動作嗎?
01/04 19:56, 4F

01/04 23:47, , 5F
http://goo.gl/LQwhSj 你可以參考一下最下面的解法
01/04 23:47, 5F

01/04 23:50, , 6F
post參數建議改用json格式
01/04 23:50, 6F

01/05 13:00, , 7F
php傳陣列參數名後要加[]就你js徊圈組result那邊
01/05 13:00, 7F

01/06 08:47, , 8F
print_r($product_id);
01/06 08:47, 8F

01/07 23:11, , 9F
一直重覆name=xxx,php只會採用最後一個
01/07 23:11, 9F
文章代碼(AID): #1KgI6GtJ (PHP)
文章代碼(AID): #1KgI6GtJ (PHP)