0

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

<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_Id相同的行插入一行?!image_Title我使用了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();
}
4

1 回答 1

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();
}
于 2013-08-20T07:07:59.097 回答