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