我对Codeigniter还是新手,对PHP也很新手。我想从数据库和目录中删除一条记录。数据是从数据库和目录中删除。当我提交时,删除是成功的,但是删除总是从表数据库中选择最后一项,而不是从ID中删除,因为我选择删除。这是我的代码
<?php foreach ($data as $row) : ?>
<div class="modal fade" id="deleteModal<?= $row['id']; ?>" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="deleteModalLabel">Ready to delete?</h5>
<button class="close" type="button" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<?php echo form_open('admin/slider_delete'); ?>
<input type="text" name="id" id="id" value="<?= $row['id']; ?>">
<div class="form-group row">
<label for="slider_name" class="col-sm-3 col-form-label">Slider Name</label>
<div class="col-sm-9"><input type="text" class="form-control" name="slider_name" id="slider_name" value="<?= $row['slider_name']; ?>" readonly>
</div>
</div>
<div class="form-group row">
<label for="slider_image" class="col-sm-3 col-form-label">Image</label>
<div class="col-sm-9">
<div class="custom-file">
<div class="row">
<div class="col-sm-12">
<input type="hidden" class="form-control" id="slider_image" name="slider_image" value="<?= $row['slider_image']; ?>" readonly>
<img src="<?= base_url('assets/img/slider/') . $row['slider_image']; ?>" id="image-preview" alt="image preview" class="img-thumbnail" style="width : 100px;height:60px;">
</div>
</div>
</div>
</div>
</div>
<br><br><br>Are you sure to delete this data?
</div>
<div class="modal-footer">
<button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Update</button> </div>
<?= var_dump($row); ?>
</div>
</div>
</div>
<?php endforeach ?>
public function slider_delete()
{
$id = $this->input->post('id');
$slider = $this->input->post('slider_image');
$data = [
'slider_name' => htmlspecialchars($this->input->post('slider_name', true)),
'slider_link' => htmlspecialchars($this->input->post('slider_link', true)),
'text_active' => 1,
'title_text' => htmlspecialchars_decode($this->input->post('title_text', true)),
'description' => htmlspecialchars_decode($this->input->post('description', true)),
'is_active' => 1
];
echo var_dump($id, $data, $slider);
die;
$this->slider_model->delete($id, $data, $slider);
$this->session->set_flashdata('massage', '<div class="alert alert-success" role="alert"> Slider has been delete!</div>');
redirect('admin/slider');
}
public function delete($id, $data, $slider)
{
$this->db->set($data);
$this->db->where('id', $id);
unlink(FCPATH . 'assets/img/slider/' . $slider);
$this->db->delete('slider');
}
答案 0 :(得分:1)
根据您的代码,您正在为每一行创建一个新表单,但您没有关闭该表单。因此,ID变量始终是推断的封闭表单标记之前的最后一个集合。在行之后
<br><br><br>Are you sure to delete this data?
添加
</form>
然后检查显示在浏览器中的代码。您应该具有与结束表单标签等效的开始表单标签。