MySQL更新批处理删除一些记录
我有一个使用批处理存储一些财务值的程序。主表为财务预算和财务预算问题。finance\u budget.budget\u id=finance\u budget\u issue.budget\u id(1到多个联接) 我尝试使用以下代码编辑一些记录 控制器 看法MySQL更新批处理删除一些记录,mysql,codeigniter,Mysql,Codeigniter,我有一个使用批处理存储一些财务值的程序。主表为财务预算和财务预算问题。finance\u budget.budget\u id=finance\u budget\u issue.budget\u id(1到多个联接) 我尝试使用以下代码编辑一些记录 控制器 看法 $(文档).ready(函数(){ 变量i=$('#last').val(); $(“.add new”)。单击(函数(){ i++; //$('.tab_logic').append(); $('#addr'+i).html('+
$(文档).ready(函数(){
变量i=$('#last').val();
$(“.add new”)。单击(函数(){
i++;
//$('.tab_logic').append();
$('#addr'+i).html('+
"" +
"" +
"" +
"" +
"" +
"" +
"");
$('#tab_logic')。追加('');
$('i').val(i);
//警报($('i').val());
$(“#项”+i)。选择2();
});
$(文档)。在(“单击”、“.remove”上,函数(){
var count=$('#tab_logic>tbody').children().length;
如果(计数>0){
$(this).closest('tr').remove();
}
});
});
编辑已发布的分配
您可以修改模型以检查$details
是否为空,从而使财务预算问题
表保持不变:
函数editIssuedAllocations($ustock,$details,$id)
{
如果(!空($id)){
$this->db->trans_start();
$this->db->update('finance_budget',$ustock,array('budget_id'=>$id));
如果($id!=1&&!为空($details[0])){
$this->db->where('budget_id',$id);
$this->db->delete(‘财务预算问题’);
$this->db->insert_batch('finance_-budget_-issue',$details);
}
$this->db->trans_complete();
返回$this->db->trans_status();
}
返回false;
}
我在控制器中修改了for循环,如下所示
$count = count($this->input->post('office'));
$offices = $this->input->post('office');
$votes = $this->input->post('vote_id');
$amount = $this->input->post('amount');
$notes = $this->input->post('notes');
$bdate = $this->input->post('budget_date');
for ($x = 0; $x < $count; $x++) {
$details[$x]['budget_id'] = $id;
$details[$x]['office'] = $offices[$x];
$details[$x]['vote_id'] = $votes[$x];
$details[$x]['amount'] = (-1)*$amount[$x];
$details[$x]['notes'] = $notes[$x];
$details[$x]['budget_date'] = $bdate[$x];
$details[$x]['status'] = 1;
}
$count=count($this->input->post('office'));
$offices=$this->input->post('office');
$votes=$this->input->post('vote_id');
$amount=$this->input->post('amount');
$notes=$this->input->post('notes');
$bdate=$this->input->post('budget_date');
对于($x=0;$x<$count;$x++){
$details[$x]['budget_id']=$id;
$details[$x]['office']=$offices[$x];
$details[$x]['vote_id']=$voces[$x];
$details[$x]['amount']=(-1)*$amount[$x];
$details[$x]['notes']=$notes[$x];
$details[$x]['budget_date']=$b日期[$x];
$details[$x]['status']=1;
}
现在这个问题已经部分解决了。但是财务预算问题表的保存投票号未经编辑是不正确的。您能分享您的意见吗View@ascoftw. 补充说,这是一个太多的代码分析。我认为问题在于,当您保存而不做任何更改时,$details
为空,并且它正在删除模型方法中的所有内容。在这两种情况下,尝试检查并比较$details
或$\u POST
本身的值。这是正确的。但结果是一样的。如果我编辑同一条记录两次,该记录将被完全删除。我认为$this->db->delete('finance_-budget_-issue')行的问题;。如果我删除这一行,财务预算问题表的记录将加倍。如果我第一次在视图中按“更新”按钮(无论是否编辑条目),财务预算问题表的“投票id”和“状态”列将变为“0”。我想这可能就是原因。我们必须找到解决办法。
function editIssuedAllocations($ustock,$details,$id)
{
if(!empty($id)){
$this->db->trans_start();
$this->db->update('finance_budget',$ustock,array('budget_id'=>$id));
if($id!=1){
$this->db->where('budget_id', $id);
$this->db->delete('finance_budget_issue');
$this->db->insert_batch('finance_budget_issue', $details);
}
$this->db->trans_complete();
return $this->db->trans_status();
}
return false;
}
<?php
if (!empty($purchase)) {
$details = $purchase;
$purchase = $purchase[0];
}
?>
<script type="text/javascript">
$(document).ready(function () {
var i = $('#last').val();
$(".add-new").click(function () {
i++;
// $('.tab_logic').append();
$('#addr' + i).html("<td><a href='javascript:void(0);' id='add-post' class='external add-new'><i style='color: #00C853 !important;' class='fa fa-2x fa-plus-circle'></i></a></td>" +
"<td><select name='office[]' id='office" + i + "' class='select2 form-control' required>" +
"<option value=''></option>" +
<?php
if (!empty($office)) {
foreach ($office as $row) {
?>
"<option value='<?= $row->office_id ?>'><?= addslashes($row->office_name) ?></option>" +
<?php
}
}
?>
"</select></td>" +
"<td><input name='amount[]' type='number' step='any' placeholder='Amount' class='form-control input-md' required></td>" +
"<td><input name='notes[]' type='text' placeholder='Notes' class='form-control input-md'></td>" +
"<td><a href='javascript:void(0);' class='external remove'><i style='color: #dd4b39 !important;' class='fa fa-2x fa-minus-circle' id='addIcon'></i></a></td>");
$('#tab_logic').append('<tr id="addr' + (i + 1) + '"></tr>');
$('#i').val(i);
// alert($('#i').val());
$('#item' + i).select2();
});
$(document).on("click", ".remove", function () {
var count = $('#tab_logic>tbody').children().length;
if (count > 0) {
$(this).closest('tr').remove();
}
});
});
</script>
<div class="box box-info">
<div class="box box-info collapsed-box">
<div class="box-header with-border">
<h3 class="box-title">Edit Issued Allocations</h3>
<div class="box-tools pull-right">
<button type="button" class="btn btn-box-tool" style="font-size: 16px;" data-widget="collapse"><i
class="fa fa-plus"></i>
</button>
</div>
<!-- /.box-tools -->
</div>
<!-- /.box-header -->
<div class="box-body" style="display: block">
<form action="<?= site_url('budget/editPurchase/' . $purchase->budget_id) ?>" method="post">
<div class="row">
<div class="col-md-2">
<div class="form-group"><label>Budget ID</label>
<input type="text" name="budget_id" id="budget_id" class="form-control" disabled
value="<?= $purchase->budget_id ?> ">
</div>
</div>
<div class="col-md-4">
<div class="form-group"><label>වැය ශීර්ෂය</label>
<select name="vote_id" id="vote_id" class="form-control select2" required>
<option value="<?= $purchase->vote_id ?>"><?= $purchase->vote ?></option>
<?php
if (!empty($vote)) {
foreach ($vote as $row) {
?>
<option value="<?= $row->vote_id ?>"><?= $row->vote ?></option>
<?php
}
}
?>
</select>
</div>
</div>
<div class="col-md-4">
<div class="form-group"><label>යොමු / ගොණු අංකය</label>
<input type="text" name="ref_no" id="ref_no" class="form-control"
value="<?= $purchase->ref_no ?>">
</div>
</div>
<div class="col-md-2">
<div class="form-group"><label>දිනය</label>
<input type="date" name="budget_date" id="budget_date" class="form-control"
value="<?= $purchase->budget_date ?>">
</div>
</div>
</div>
<div class="col-md-12 column">
<table class="table table-bordered table-hover" id="tab_logic">
<thead>
<tr>
<th class="text-center">
</th>
<th class="text-center">
Office
</th>
<th class="text-center">
Amount
</th>
<th class="text-center">
Notes
</th>
<th class="text-center">
</th>
</tr>
</thead>
<tbody>
<?php
//dd($details);
$x = 0;
foreach ($details as $i => $values) {
// foreach ($values as $key => $value) {
$x = $i;
?>
<tr id='addr0'>
<td>
<a href="javascript:void(0);" id="add-post" class="external add-new"><i
style="color: #00C853 !important;"
class="fa fa-2x fa-plus-circle"></i></a>
</td>
<td style="width: 50%">
<input type="hidden" name="i" id="i" value="<?= $i ?>">
<select name="office[]" id="<?= "office" . $i ?>" class="form-control select2" required>
<option value="<?= $details[$i]->office_id ?>"><?= $details[$i]->office_name ?></option>
<?php
if (!empty($office)) {
foreach ($office as $row) {
?>
<option value="<?= $row->office_id ?>"><?= $row->office_name ?></option>
<?php
}
}
?>
</select>
</td>
<td>
<input type="number" name='amount[]' placeholder='Amount' class="form-control"
value="<?= (-1) * $details[$i]->amount ?>" required/>
</td>
<td>
<input type="text" name="notes[]" placeholder='Notes'
value="<?= $details[$i]->notes ?>" class="form-control"/>
<input type="hidden" name="table_id"
value="<?= $details[$i]->notes ?>">
</td>
<td><a href='javascript:void(0)' class='external remove'><i
style='color: #dd4b39 !important;'
class='fa fa-2x fa-minus-circle ' id='addIcon'></i></a>
</td>
</tr>
<tr id='addr1'></tr>
<?php
//print " $key => $value\n";
// }
//print "}\n";
}
?>
</tbody>
</table>
</div>
<!--table-->
<div class="row">
<div class="col-md-8"></div>
<div class="col-md-4">
<input type="hidden" value="<?= $x ?>" name="last" id="last">
<input type="submit" value="Update" class="btn btn-primary btn-block">
</div>
</div>
</form>
<!--end of table-->
</div>
</div>
</div>
$count = count($this->input->post('office'));
$offices = $this->input->post('office');
$votes = $this->input->post('vote_id');
$amount = $this->input->post('amount');
$notes = $this->input->post('notes');
$bdate = $this->input->post('budget_date');
for ($x = 0; $x < $count; $x++) {
$details[$x]['budget_id'] = $id;
$details[$x]['office'] = $offices[$x];
$details[$x]['vote_id'] = $votes[$x];
$details[$x]['amount'] = (-1)*$amount[$x];
$details[$x]['notes'] = $notes[$x];
$details[$x]['budget_date'] = $bdate[$x];
$details[$x]['status'] = 1;
}