简体   繁体   English

PHP复选框更新,插入,删除MySQL记录

[英]php checkbox update, insert, delete mysql records

I've searched quite a bit for this but nothing has seem to to come up which is strange because I think it would be something that's something quite easy to implement. 我已经搜索了很多,但是似乎没有出现什么奇怪的事情,因为我认为这很容易实现。 Basically I have a list of items which the user selects, what I want to do is update the values in the database according to the checkboxes selected basically the following scenarios: 基本上,我有一个用户选择的项目列表,我要做的是根据基本上在以下情况下选中的复选框来更新数据库中的值:

The id assigned in the checkbox array is already in the DB and nothing needs to happen, or can be updated with the same value. 复选框数组中分配的ID已存在于数据库中,无需进行任何操作,也可以使用相同的值进行更新。

The id assigned in the checkbox array needs to be added to the database. 需要将复选框数组中分配的ID添加到数据库中。

The id ISN't assigned in the checkbox array and therefor must be deleted from the DB. 未在复选框数组中分配的ID ISN,因此必须从数据库中删除。

has anyone got any code they worked with that does this? 有没有人得到与他们一起工作的任何代码,这样做吗?

EDIT: 编辑:

the table is simple with two things being updated: 该表很简单,其中有两处要更新:

id_text and id_product id_text和id_product

array coming from $_POST is: 来自$ _POST的数组是:

Array ( [text] => 
         Array ( [0] => 8 [1] => 1 [2] => 2 ) // the values that need to be inserted, updated, or deleted if they don't exist here.
         [textValue] => ALL OF THEM
         [id] => 33 // the 'id_product value'
         [update] => update ) 

True, without any code or database squema is pretty hard to help, but if I understand correctly you can just: 没错,没有任何代码或数据库的拥挤很难帮助,但是如果我理解正确的话,您可以:

  1. Delete all values on DB. 删除数据库上的所有值。
  2. Loop through the ids passed in POST or GET and insert them on DB. 循环遍历POST或GET中传递的ID,并将其插入DB中。
  1. Do an replace for every id passed. 对传递的每个ID进行替换。 Replace behaves like insert i the entry does not exist and like update if it exists. 替换的行为类似于插入i,该条目不存在,并且像更新(如果存在)。 I can not give you more details becuase I do not know your table structure 我无法提供更多详细信息,因为我不知道您的表结构
  2. Delete all items that have a id that is not in your array. 删除所有ID不在您的数组中的项目。 you can select those with "in": 您可以选择带有“中”的那些:

    ...WHERE id_product NOT IN (1,3,5,8) AND...... ... id_product不在(1,3,5,8)中的位置...

    you can easily generate this by using 您可以通过使用轻松生成此

    $idCondition = 'id_product NOT IN (' . implode(',',$yourArrayName) . ')'; $ idCondition ='id_product NOT IN('。implode(',',$ yourArrayName)。')';

About REPLACE: 关于REPLACE:

http://dev.mysql.com/doc/refman/5.1/de/replace.html http://dev.mysql.com/doc/refman/5.1/de/replace.html

About IN: 关于IN:

http://www.webdevelopersnotes.com/tutorials/sql/tutorial_mysql_in_and_between.php3 http://www.webdevelopersnotes.com/tutorials/sql/tutorial_mysql_in_and_between.php3

EDIT: I just realized that some people might have objections on my deinition of REPLACE. 编辑:我只是意识到,有些人可能对我对REPLACE的定义有异议。 What REPLACE actually does is deleting the row if it already exists and then do an INSERT. REPLACE实际执行的操作是删除该行(如果已存在),然后执行INSERT。

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

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