简体   繁体   English

在php中编辑复选框数据并在mysql中更新行

[英]Editing checkboxes data in php and updating the rows in mysql

I have a group of checkboxes as below, as part of a larger form, in the view. 在视图中,我有一组复选框,作为较大表单的一部分,如下所示。

<input type = "checkbox" value = "1" name = "checkbox_array[]" /> Checkbox 1
<input type = "checkbox" value = "2" name = "checkbox_array[]" /> Checkbox 2
<input type = "checkbox" value = "3" name = "checkbox_array[]" /> Checkbox 3
<input type = "checkbox" value = "3" name = "checkbox_array[]" /> Checkbox 3

Say I check Checkbox 2 and Checkbox 4 and click submit. 假设我选中复选框2和复选框4,然后单击提交。 To save I loop through the checkbox_array[] as below, in the controller. 为了保存,我在控制器中通过如下所示的checkbox_array[]循环。

$checkbox_array = $this -> input -> post('checkbox_array', TRUE)

<?php for($i=0;$i<count($checkbox_array);$i++){
$users = new User();
$users -> role_id = $checkbox_array[$i];
//Amongst other form data
$users -> save();
}?>

This saves the data in the db as: 这会将数据库中的数据保存为:

id role_id 
1    2
2    4

Using the same view, I intend to edit the data saved. 使用相同的视图,我打算编辑保存的数据。 So say now I want to check Checkbox 1, uncheck Checkbox 2, check Checkbox 3, retain Checkbox 4. such that I intend to have below in the db. 所以说现在我要选中“复选框1”,取消选中“复选框2”,选中“复选框3”,保留复选框4。这样,我打算在数据库中包含以下内容。

id role_id 
1    1
2    3
3    4

I am at a loss however how to do the update. 但是我不知所措,如何进行更新。 I had been thinking of getting the saved array from the db, doing an inarray() search, and inserting the data that is not in the saved array, computationally I do not know about how effective this is. 我一直在想从数据库中获取保存的数组,执行inarray()搜索,然后插入不在保存的数组中的数据,从计算上我不知道这样做有多有效。

Is there a mysql function I could use to achieve this, say like replace() , or how else would I be able to achieve the edit? 有没有可以用来实现此目的的mysql函数,例如replace() ,或者还有其他方法可以实现编辑?

Thank you. 谢谢。

What I usually end up doing in this situation is first delete all data related to that form in the database (for the specific user, etc) and then insert the data for the checkboxes. 在这种情况下,我通常要做的是首先删除数据库中与该表单有关的所有数据(针对特定用户等),然后为复选框插入数据。 It is just far cheaper to delete the data matching the criteria (especially if the WHERE of the delete is on an indexed column) than trying to select and selectively delete them. 删除符合条件的数据(尤其是如果删除的WHERE位于索引列中)要比尝试选择并有选择地删除它们便宜得多。 Not to mention it keeps the code a lot cleaner which over the long-haul for maintenance reasons has its benefits. 更不用说它使代码更加整洁,从长远来看,出于维护原因,它有其好处。

Just to be clearer, a DELETE that has WHERE clauses on an index is far cheaper than a SELECT and then a DELETE for those that should not be there and then an INSERT. 为了更清楚一点,在索引上具有WHERE子句的DELETE比SELECT便宜,对于不应该存在的DELETE比对INSERT更便宜。

$checkbox1 = $_POST['checkbox_array'];
$selected_checkbox = "";
foreach ($checkbox1 as $checkbox1) 
{
   $selected_checkbox .= $checkbox1 . ", ";
}
$selected_checkbox = substr($selected_checkbox, 0, -2);

// your insert query here.. 

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

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