[英]How to insert dynamic input values into the database using codeigniter?
[英]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.