Codeigniter多维数组到数据库
我试图将数组作为单独的行插入到数据库表中。该数组是从用户可以添加其他行的表中生成的。无法解决问题!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
)
)
回答:
你正在构建你的阵列的方式是错误的,你想什么要做什么,因为每个数据库列都有一个数组,并且您想要的是每个数据库行的数组。
取而代之的是,你的部分描述添加到一个数组
<td><input type="text" name="parts_description[]" class="form-control" /></td>
你需要的东西是这样的:第一行
<td><input type="text" name="job[$i][parts_description]" class="form-control" /></td>
凡$i=0
,然后通过增加它1,每个新行由你的jQuery函数添加。
这样,您将结束与包含与对应的单排每一个你的桌子上,然后可以循环到插入到数据库
回答:
这虽然这种解决方案可能会来方便多阵列$_POST['job']
变量。这不是理想的解决方案。
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')
);
$partsArr = [];
$totalCnt = count($parts['job_number']);
for($key=0; $key < $totalCnt ;$key++)
{
$partsArr[] = [
'job_number' => $parts['job_number'][$key] ,
'quantity' => $parts['quantity'][$key] ,
'parts' => $parts['parts'][$key] ,
'fitted' => $parts['fitted'][$key] ,
'required' => $parts['required'][$key] ,
'cost' => $parts['cost'][$key]];
}
//just make sure that your service_model uses `insert_batch` method to insert data
$this->service_model->add_parts_db($partsArr);
redirect('service', 'refresh');
}
欲了解更多信息,请阅读documentation
以上是 Codeigniter多维数组到数据库 的全部内容, 来源链接: utcz.com/qa/258753.html