开发者

checkbox group multiple insert with MYSQL achieveable?

开发者 https://www.devze.com 2023-04-10 22:36 出处:网络
how is it possible to output my rows into a checkbox group and insert them into a database at based on the ID of the items checked?

how is it possible to output my rows into a checkbox group and insert them into a database at based on the ID of the items checked?

So user checks 1,2,3,4,5 and I insert the ID of 1,2,3,4,5 into myTable.

开发者_如何学JAVA

I've only ever dealt with 1 x $myVariable INSERTS so far..


MySQL has an extended insert syntax. Assuming your checkbox values are stored in a normalized table, you'd use something like

INSERT INTO yourtable (checkboxID) VALUES ($id1), ($id2), ($id3), etc...

which would produce a new record in yourtable for each value you specify. Note that you can insert sets of values in the same manner:

INSERT .... VALUES ($x, $y), ($a, $b), ($c, $d) etc...

You'd have to build up that list of values yourself, as the brackets around them are required for the extended insert to work. You can't simply do INSERT .... implode($_POST['checkboxes'] type of thing.

Now, if you want to insert a comma-separated list of IDs into a single field/record in the DB, then I strongly suggest you don't. Normalize your database and create a sub-table for those values.


Name your checkbox as an array and then check on submission if each index is checked or not.

For example:

<input type='checkbox' name=content[] value='1'>1
<input type='checkbox' name=content[] value='2'>2
<input type='checkbox' name=content[] value='3'>3

PHP:

foreach($_POST['content'] as $record)
{
    // insert query
} 
0

精彩评论

暂无评论...
验证码 换一张
取 消