从多个相同的输入插入数据?

Inserting Data From Multiple Inputs That Are The Same?

如果我有多个相同的输入,我希望仍然能够将它们插入数据库而不会覆盖第一个 lot.I 已经试过了,但没有成功。我的框架使用 codeigniter。

我如何能够拥有多个相同的输入,但仍然在 table.

上为两者创建一个新行

尽管如此,我需要为所有人保留相同的横幅 ID。

如果您对问题投反对票,请解释为什么干杯

型号

public function add_banners_image($banner_id = 0) {

$data = array(
'banner_id' => $banner_id,
'link' => $this->input->post('link'),
);

$this->db->set($data);
$this->db->insert_id();
$this->db->insert($this->db->dbprefix . 'banner_image');

//return $this->db->insert_id();

} 

控制器

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Banner_add extends Admin_Controller {

public function __construct() {
parent::__construct();
$this->load->model('admin/design/model_add_banners');
}

public function index() {
$this->load->library('form_validation');

$this->form_validation->set_rules('name', 'Banner Name');
$this->form_validation->set_rules('status', 'Banner Status');

if ($this->form_validation->run($this) == FALSE) {

return $this->load->view('design/banner_form.tpl', $data);

} else {

$banner_id = $this->model_add_banners->add_banners();

$banner_image_id = $this->model_add_banners->add_banners_image($banner_id);

$this->model_add_banners->admin_banners_banner_image_description($banner_image_id, $banner_id);

redirect('admin/design/banners');

}

}

}

查看

<?php echo Modules::run('admin/common/header/index');?><?php echo Modules::run('admin/common/column_left/index');?>

<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">

<?php echo $this->load->view('flashdata/flashdata.tpl');?>
<?php echo validation_errors('<div class="alert alert-danger">', '</div>'); ?>
<?php echo form_open_multipart('admin/design/banners/add', array('id' => 'banner_add', 'class' => 'form-horizontal'));?>

<div class="form-group">
<label class="col-sm-2 control-label" for="input-banner-name">Banner Name</label>
<div class="col-sm-10">
<input type="text" name="name" value="" placeholder="Banner Name" id="input-banner-name" class="form-control" />
</div>
</div>

<div class="form-group">
<label class="col-sm-2 control-label" for="input-status">Banner Status</label>
<div class="col-sm-10">
<select name="status" id="input-status" class="form-control">
<option value="1">Enable</option>
<option value="0">Disabled</option>
</select>
</div>
</div>

<table id="banners" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<td class="text-center">Title</td>
<td class="text-center">Url Link</td>
</tr>
</thead>
<tbody>

<tr>
  <td>
    <input type="text" name="title" value="" placeholder="Banner Title" id="" class="form-control" />
  </td>
  <td>
    <input type="text" name="link" value="" placeholder="Banner Link" id="" class="form-control" />
  </td>
</tr>

<tr>
  <td>
    <input type="text" name="title" value="" placeholder="Banner Title" id="" class="form-control" />
  </td>
  <td>
    <input type="text" name="link" value="" placeholder="Banner Link" id="" class="form-control" />
  </td>
</tr>

</tbody>
</table>
<?php echo form_close();?>

</div>
</div>

<?php echo Modules::run('admin/common/footer/index');?>

首先,您应该将title 和link 的name 属性更改为title[]link[]。这允许在提交表单时发送同名的多个值。

然后,您必须分别读取所有值并进行相应处理。这是您模型的更改代码:

public function add_banners_image($banner_id = 0) {

$links = $this->input->post('link');
foreach ($links as $link) {
    $data = array(
        'banner_id' => $banner_id,
        'link' => $link,
    );

    $this->db->set($data);
    $this->db->insert_id();
    $this->db->insert($this->db->dbprefix . 'banner_image');

    //return $this->db->insert_id();
}

} 

这里是对在 codeigniter 中 "POST" 中接收到的处理数组值的引用: Getting data from post array in CodeIgniter