简体   繁体   English

PHP:在SQL中保存多个复选框值

[英]PHP: Saving multiple checkbox values in SQL

I'm trying to make a form that writes away multiple checkbox values in a single SQL record I have some code so far, but i have no idea what i'm doing wrong. 我正在尝试制作一种形式,可以在单个SQL记录中写出多个复选框值,到目前为止,我有一些代码,但是我不知道自己在做什么错。

Here's what i have: (my table that contains the checkboxes) 这是我所拥有的:(我的包含复选框的表)

<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>

Here's my PHP-code (keep in mind, this is just part of a bigger code, i have other textfields etc.. already functioning) : 这是我的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!';

The implode gives the following error: Warning: implode() [function.implode]: Invalid arguments passed in C:\\xampplite\\htdocs\\LPtest\\insert.php on line 38 爆破产生以下错误: 警告:implode()[function.implode]:在C:\\ xampplite \\ htdocs \\ LPtest \\ insert.php的第38行传递了无效的参数

IF ANYONE CAN HELP ME ON WHAT I'M DOING WRONG HERE, IT WOULD BE GREATLY APPRECIATED! 如果有人在我这里做错了什么,可以对我有帮助,那么我们将不胜感激! Thank you in advance! 先感谢您!

As the function name says real_escape_string only works for strings. 正如函数名称所说, real_escape_string仅适用于字符串。 So you must call this function for each value of $_POST['Regio'] array: 因此,您必须为$_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);

This should work. 这应该工作。

$_POST['regio'] is a multi-dimensional array. $ _POST ['regio']是一个多维数组。 You should use foreach to get the individual values out of it. 您应该使用foreach从中获取各个值。

You should use foreach.. 您应该使用foreach。

HTML code : HTML代码:

<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 code : PHP代码:

$chkBoxes = $_POST['Regio'];
foreach ($chkBoxes as $key => $val) {
     // you code here
}

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

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