简体   繁体   English

PHP foreach循环插入数据库

[英]PHP foreach loop inserting into database

I am trying to insert the selected values of checkboxes into a mysql database using php. 我正在尝试使用php将复选框的选定值插入到mysql数据库中。 I just can't understand why it's not working because it echoes out all the values fine, but it will only insert the first selected one into the database. 我只是不明白为什么它不起作用,因为它会很好地回显所有值,但是只会将第一个选定的值插入数据库中。

HTML: HTML:

<b>Injury type:</b>
Bruise <input type='checkbox' name='InjuryType[]' value='1'><br>
Cut <input type='checkbox' name='InjuryType[]' value='2'><br>
Graze <input type='checkbox' name='InjuryType[]' value='3'><br>
Break <input type='checkbox' name='InjuryType[]' value='4'><br>
Bump <input type='checkbox' name='InjuryType[]' value='5'><br>

PHP: PHP:

foreach($_POST['InjuryType'] as $value) {
$insert = mysql_query("INSERT INTO AccidentInjuryLink(InjuryID) VALUES ('$value')");
echo $value; 
}

也许该列具有主索引,这意味着所有数字都必须是唯一的,因此只能插入第一个值。

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

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