我試圖將數組作爲單獨的行插入到數據庫表中。該數組是從用戶可以添加其他行的表中生成的。無法解決問題!Codeigniter多維數組到數據庫
下面是這個視圖
<table class="table table-bordered" id="parts">
<thead class="table-success">
<tr>
<th class="table-success">Quantity</th>
<th class="table-success">Parts</th>
<th class="table-success">Fitted</th>
<th class="table-success">Required</th>
<th class="table-success">Cost</th>
</tr>
</thead>
<tbody>
<tr>
<input type="hidden" name="job_no[]" value="<?php echo $job_number; ?>" />
<td><input type="number" name="parts_qty[]" class="form-control" /></td>
<td><input type="text" name="parts_description[]" class="form-control" /></td>
<td><input type="text" name="parts_fitted[]" class="form-control" /></td>
<td><input type="text" name="parts_required[]" class="form-control" /></td>
<td><input type="text" name="parts_cost[]" class="form-control" /></td>
</tr>
</tbody>
</table>
<button type="button" class="btn btn-primary btn-xs" id="addRow"> <span class="glyphicon glyphicon-plus-sign" aria-hidden="true"> </span> Add Row </button>
<script>
$(document).ready(function() {
$("#addRow").click(function(){
$("#parts").append("<tr><input type='hidden' name='job_no[]' value='<?php echo $job_number; ?>' /><td><input type='number' name='parts_qty[]' class='form-control' /></td><td><input type='text' name='parts_description[]' class='form-control' /></td><td><input type='text' name='parts_fitted[]' class='form-control' /></td><td><input type='text' name='parts_required[]' class='form-control' /></td><td><input type='text' name='parts_cost[]' class='form-control' /></td></tr>");
});
});
</script>
請注意,這是一個更大形式的一部分,因此沒有顯示提交按鈕等
我的控制器功能看起來像這樣
function add_parts_db()
{
$parts = array (
'job_number' => $this->input->post('job_no'),
'quantity' => $this->input->post('parts_qty'),
'parts' => $this->input->post('parts_description'),
'fitted' => $this->input->post('parts_fitted'),
'required' => $this->input->post('parts_required'),
'cost' => $this->input->post('parts_cost')
);
$this->service_model->add_parts_db($parts);
redirect('service', 'refresh');
}
這裏該陣列通過表格中的一些樣本輸入生成
Array
(
[job_number] => Array
(
[0] => 2008
[1] => 2008
[2] => 2008
)
[quantity] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[parts] => Array
(
[0] => Part 1
[1] => Part 2
[2] => Parts 3
)
[fitted] => Array
(
[0] => Fitted 1
[1] => Fitted 1
[2] => Fitted 3
)
[required] => Array
(
[0] => Required 1
[1] => Required 2
[2] => Required 3
)
[cost] => Array
(
[0] => 1.00
[1] => 2.00
[2] => 3.00
)
)
最後我希望將數組的每個項插入到數據庫表中作爲單獨的行。
function add_parts_db($parts)
{
$this->db->insert_batch('parts', $parts);
return $this->db->insert_id();
}
我想上面的陣列被插入到數據庫中作爲單獨的行所以應該是這個樣子
提交表單
的時候,我發現了以下錯誤Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0, 1, 2) VALUES ('2008','2008','2008'), ('1','2','3'), ('Part 1','Part 2','Parts' at line 1
INSERT INTO `parts` (0, 1, 2) VALUES ('2008','2008','2008'), ('1','2','3'), ('Part 1','Part 2','Parts 3'), ('Fitted 1','Fitted 1','Fitted 3'), ('Required 1','Required 2','Required 3'), ('1.00','2.00','3.00')
Filename: models/Service_model.php
Line Number: 33
我認爲這個問題可能出在我的jQuery/Javascript中,我不太明白。我可以使用名稱設置初始密鑰爲0 =「配件[0] [jobnumber可以]」但後來在我的jQuery的,我不認爲這是識別變量正確
var $i = 1;
$("#addRow").click(function(){
$("#parts").append("<tr><input type='hidden' name='parts[$i][jobnumber]' value='<?php echo $job_number; ?>' /><td><input type='number' name='parts[$i][qty]' class='form-control' /></td><td><input type='text' name='parts[$i][description]' class='form-control' /></td><td><input type='text' name='parts[i][fitted]' class='form-control' /></td><td><input type='text' name='parts[$i][required]' class='form-control' /></td><td><input type='text' name='parts[$i][cost]' class='form-control' /></td></tr>");
i++;
});
我認爲傾銷陣列看起來像它的去拉正確的數組格式,但它沒有得到它,因爲我的無能的jQuery。
["parts"]=>
array(2) {
[0]=>
array(5) {
["jobnumber"]=>
string(4) "2002"
["qty"]=>
string(1) "3"
["description"]=>
string(7) "parts 3"
["required"]=>
string(10) "required 3"
["cost"]=>
string(4) "3.00"
}
["i"]=>
array(1) {
["fitted"]=>
string(8) "fitted 3"
}
}
@Bhaumik梅塔
Array
(
[0] => Array
(
[job_number] => 2002
[quantity] => 1
[parts] => parts 1
[fitted] => Fitted 1
[required] => Required 1
[cost] => 1.00
)
[1] => Array
(
[job_number] => 2002
[quantity] => 2
[parts] => Part 2
[fitted] => Fitted 2
[required] => Required 2
[cost] => 2.00
)
[2] => Array
(
[job_number] => 2002
[quantity] => 3
[parts] => Parts 3
[fitted] => Fitted 3
[required] => Required 3
[cost] => 3.00
)
)
感謝您抽出寶貴時間來回答。我仍然在努力。我嘗試過使用上述方法,但無法將數組轉換爲添加到數據庫的格式。 –
'var_dump($ _ POST);'如果提交表單,返回?你是否明白你的問題中的這個陳述有什麼問題,或者如果我解釋了它會有幫助嗎? (0,1,2)VALUES('2008','2008','2008'),('1','2','3'),(第1部分,'第2部分',' ('必填1','必填2','必填3'),('1.00','2.00','3.00' ')' – miknik
我認爲這個問題可能在我不太瞭解的jQuery/Javascript中。我可以設置初始密鑰爲0使用名稱=「parts [0] [jobnumber]」 –