【问题标题】:How to insert an input with different values in to the mysql database using Codeigniter?如何使用 Codeigniter 将具有不同值的输入插入到 mysql 数据库中?
【发布时间】:2013-08-22 03:19:08
【问题描述】:

我有一个包含一些输入的表单,例如:

<form action="http://site.com" method="post" accept-charset="utf-8">
<input type="text" name="image_Title" value="Image" />
<input type="text" name="image_Basename" value="0000001" />
<input type="text" name="image_basename" value="0000002" />
<input type="text" name="image_basename" value="0000003" />
</form>

如何使用 Codeigniter 将具有相同 image_Title 的每个 image_Id 插入一行?!我用了foreach,但我不能让它工作?!

例如:

table-row-name:image_Title              table-row-name:image_Basename
       'Image'                                '00000001'
       'Image'                                '00000002'
       'Image'                                '00000003'
       'Image2'                               '00000258'
       'Image2'                               '00000102'

我该怎么做?!

这是我的 php 代码(来自 Codeigniter):

foreach($_POST['image_Basename'] as $image_Basename) 
{
    $image_Id = $this->gallery_model->add(array('image_Title' => $_POST['image_Title'], 'image_Basename' => $image_Basename)); 
}     

我的模特:

function add($options = array())
{

    $options = array(

    'image_Title' => $_Post['image_Title'],

    'image_Basename' => $image_Basename

    );

    $this->db->insert('mg_gallery', $options);

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

【问题讨论】:

  • 你是如何插入的?你用的是什么编程语言?
  • 我正在使用 PHP。我会测试你的解决方案并通知你:)

标签: php mysql codeigniter input insert


【解决方案1】:

如果您使用的是 PHP,请将 [] 添加到 input 名称中,您将收到一个值数组。

在 HTML 中

<input type="text" name="image_Id[]" value="0000003" />

使用MySQLi(无错误检查,无连接):

foreach ($_POST['image_Id'] as $id) {
    $stmt = $mysqli->prepare("INSERT INTO table (id) VALUES (?)");
    $stmt->bin_param("s", $id);
    $stmt->execute();
}

【讨论】:

  • 那么,我该如何插入它们呢?!
  • 显示一些你尝试过的代码。基本上,循环遍历从$_POST 接收的数组并插入值。
  • 我应该如何将name="name[]" 插入到数据库中?!
  • @Afshin 我更新了我的答案。我不知道 CodeIgniter,所以我无法为您提供帮助。这就是它在普通 PHP 中的工作方式。
猜你喜欢
  • 2017-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多