繁体   English   中英

如何使用Codeigniter将具有不同值的输入插入mysql数据库?

[英]How to insert an input with different values in to the mysql database using Codeigniter?

我有一些输入的表格,例如:

<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>

如何使用image_Id具有相同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,请在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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM