我正在尝试创建一个表单,在单个SQL记录中写出多个复选框值 到目前为止我有一些代码,但我不知道我做错了什么。
以下是我所拥有的:(我的表格中包含复选框)
<table>
<tr>
<td><input id="vlaams-brabant" type="checkbox" name="Regio[]" value="Vlaams-Brabant"/> Vlaams-Brabant</td>
<td><input id="waals-brabant" type="checkbox" name="Regio[]" value="Waals-Brabant"/> Waals-Brabant </td>
</tr>
<tr>
<td><input id="oost-vlaanderen" type="checkbox" name="Regio[]" value="Oost-Vlaanderen"/> Oost-Vlaanderen </td>
<td><input id="west-vlaanderen" type="checkbox" name="Regio[]" value="West-Vlaanderen"/> West-Vlaanderen </td>
</tr>
<tr>
<td><input id="Limburg" type="checkbox" name="Regio[]" value="Limburg"/> Limburg </td>
<td><input id="Antwerpen" type="checkbox" name="Regio[]" value="Antwerpen"/> Antwerpen</td>
</tr>
<tr>
<td><input id="Luik" type="checkbox" name="Regio[]" value="Luik"/> Luik </td>
<td><input id="Henegouwen" type="checkbox" name="Regio[]" value="Henegouwen"/> Henegouwen </td>
</tr>
<tr>
<td><input id="Luxemburg" type="checkbox" name="Regio[]" value="Luxemburg"/> Luxemburg </td>
<td><input id="Namen" type="checkbox" name="Regio[]" value="Namen"/> Namen </td>
</tr>
<tr>
<td><input id="België" type="checkbox" name="Regio[]" value="Heel België"/> Heel België </td>
<td><input id="Internationaal" type="checkbox" name="Regio[]" value="Internationaal"/> Internationaal </td>
</tr>
<tr>
<td><input id="Brussel" type="checkbox" name="Regio[]" value="Brussel Hoofdstedelijk Gewest"/> Brussel Hoofdstedelijk Gewest </td>
</tr>
</table>
这是我的PHP代码(请记住,这只是更大代码的一部分,我有其他文本字段等......已经正常运行):
$adds['nameCom'] = $conn->real_escape_string($_POST['nameCom']);
$adds['name'] = $conn->real_escape_string($_POST['name']);
$adds['number'] = $conn->real_escape_string($_POST['number']);
$adds['email'] = $conn->real_escape_string($_POST['email']);
$adds['activiteit'] = $conn->real_escape_string($_POST['activiteit']);
$adds['Regio'] = $conn->real_escape_string($_POST['Regio']);
// query voor INSERT INTO
$sql = "INSERT INTO `data` (`nameCom`, `name`, `number`, `email`, `activiteit`, `Regio`)
VALUES ('". $adds['nameCom']. "', '". $adds['name']. "', '". $adds['number']. "', '". $adds['email']. "', '". $adds['activiteit']. "', '" . implode(',', $adds['Regio']) ."')";
// Performs the $sql query on the server to insert the values
if ($conn->query($sql) === TRUE) {
echo 'Uw gegevens werden opgeslagen, bedankt!';
内爆发出以下错误: 警告:implode()[function.implode]:在第38行的C:\ xampplite \ htdocs \ LPtest \ insert.php中传递的参数无效
如果有人能帮我解决我在这里做错的事情,那将会非常感激! 提前谢谢!
答案 0 :(得分:2)
正如函数名称所说real_escape_string
仅适用于字符串。因此,您必须为$_POST['Regio']
数组的每个值调用此函数:
$Regio = array();
$adds['Regio'] = "";
if(count($_POST['Regio']) > 0) {
foreach($_POST['Regio'] as $key=>$value)
$Regio[] = $conn->real_escape_string($value);
}
$adds['Regio'] = implode(',', $Regio);
这应该有用。
答案 1 :(得分:1)
$ _ POST ['regio']是一个多维数组。您应该使用foreach
从中获取单个值。
答案 2 :(得分:0)
你应该使用foreach ..
HTML code:
<input id="id1" type="checkbox" name="Regio[id1]"/>
<input id="id2" type="checkbox" name="Regio[id2]"/>
<input id="id3" type="checkbox" name="Regio[id3]"/>
PHP代码:
$chkBoxes = $_POST['Regio'];
foreach ($chkBoxes as $key => $val) {
// you code here
}