I have a table of checkboxes and values, if a user selects a checkbox they select the value of the id in an array called checkedHW for simplicity sake this is what code looks like:
我有一个复选框和值的表,如果用户选择一个复选框,他们在名为checkedHW的数组中选择id的值,为简单起见,这是代码的样子:
$ids = implode(',',arrayofids);
$sql = "insert into table(id, type) values($ids,type);
$db->query($sql);
echo query for testing:
echo测试的查询:
"insert into table('id1,id2','type')
I figured that if I loop through this query I could hypothetically do this:
我想如果我循环这个查询我可以假设这样做:
"insert into table('id1','type');"
"insert into table('id2','type');"
but I'm not exactly quite sure how to do, any help would be wonderful :)
但我不太确定该怎么办,任何帮助都会很精彩:)
I actually solved it using:
我实际上用以下方法解决了
for($i=0;$i<count(arrayofids); $i++){
$sql = "insert into table(id,type) values(array[$i], 'type'";
$db->query($sql);}
I hope that helps someone and thank you guys for the help!
我希望能帮助别人并感谢你们的帮助!
3 个解决方案
#1
6
You could do something like this:
你可以这样做:
$base = 'INSERT INTO table (id, type) VALUES (';
$array = array(1, 2, 3, 4);
$values = implode(", 'type'), (", $array);
$query = $base . $values . ", 'type')";
$db->query($query);
This is what would be getting submitted:
这是将要提交的内容:
INSERT INTO table (id, type) VALUES (1, 'type'), (2, 'type'), (3, 'type'), (4, 'type')
#2
0
Both query are completely different
两种查询都完全不同
insert into table('id1,id2','type')
will insert single row
插入表('id1,id2','type')将插入单行
id1,id2 | type
id1,id2 |类型
whereas
而
insert into table('id1','type');"
insert into table('id2','type');"
will insert two rows
将插入两行
id1 | type
id2 | typeid1 |输入id2 |类型
so if your id column is int
type then you cant run first query
所以,如果您的id列是int类型,那么您无法运行第一个查询
#3
0
If you have a series of checkboxes, and wanting to insert the values into your table, you could loop through them like this:
如果您有一系列复选框,并希望将值插入表中,则可以像这样循环遍历它们:
<?php
$values = array();
foreach ($_POST['field_name'] as $i => $value) {
$values[] = sprintf('(%d)', $value);
}
$values = implode(',', $values);
$sql = "INSERT INTO `table_name` (`column_name`) VALUES $values";
This will give you a SQL query similar to:
这将为您提供类似于以下内容的SQL查询:
INSERT INTO `table_name` (`column_name`) VALUES (1),(2),(3),(4)
Hope this help.
希望这有帮助。
#1
6
You could do something like this:
你可以这样做:
$base = 'INSERT INTO table (id, type) VALUES (';
$array = array(1, 2, 3, 4);
$values = implode(", 'type'), (", $array);
$query = $base . $values . ", 'type')";
$db->query($query);
This is what would be getting submitted:
这是将要提交的内容:
INSERT INTO table (id, type) VALUES (1, 'type'), (2, 'type'), (3, 'type'), (4, 'type')
#2
0
Both query are completely different
两种查询都完全不同
insert into table('id1,id2','type')
will insert single row
插入表('id1,id2','type')将插入单行
id1,id2 | type
id1,id2 |类型
whereas
而
insert into table('id1','type');"
insert into table('id2','type');"
will insert two rows
将插入两行
id1 | type
id2 | typeid1 |输入id2 |类型
so if your id column is int
type then you cant run first query
所以,如果您的id列是int类型,那么您无法运行第一个查询
#3
0
If you have a series of checkboxes, and wanting to insert the values into your table, you could loop through them like this:
如果您有一系列复选框,并希望将值插入表中,则可以像这样循环遍历它们:
<?php
$values = array();
foreach ($_POST['field_name'] as $i => $value) {
$values[] = sprintf('(%d)', $value);
}
$values = implode(',', $values);
$sql = "INSERT INTO `table_name` (`column_name`) VALUES $values";
This will give you a SQL query similar to:
这将为您提供类似于以下内容的SQL查询:
INSERT INTO `table_name` (`column_name`) VALUES (1),(2),(3),(4)
Hope this help.
希望这有帮助。