0

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.

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

$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

IF ANYONE CAN HELP ME ON WHAT I'M DOING WRONG HERE, IT WOULD BE GREATLY APPRECIATED! Thank you in advance!

CodeSigns
  • 53
  • 1
  • 2
  • 10

3 Answers3

2

As the function name says real_escape_string only works for strings. So you must call this function for each value of $_POST['Regio'] array:

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

Mihai Matei
  • 24,166
  • 5
  • 32
  • 50
  • best practice is to `$adds['Regio']=array();` before the loop – Waygood Mar 27 '13 at 09:16
  • +1 You could also do it in one line `$adds['Regio'] =$conn->real_escape_string(implode(',', $_POST['Regio']));` BUT your code shows the processing involved – Waygood Mar 27 '13 at 09:17
  • I see whatr you're getting at, however: what should my code be in the insert statement if i use Matei Mihai's code? `$sql = "INSERT INTO `data` (`nameCom`, `name`, `number`, `email`, `activiteit`, `Regio`) VALUES ('". $adds['nameCom']. "', '". $adds['name']. "', '". $adds['number']. "', '". $adds['email']. "', '". $adds['activiteit']. "', '". $my_implode['Regio'] ."')"; ` Like this? – CodeSigns Mar 27 '13 at 09:38
  • If you're using exactly my code.. you have to add just `$adds['Regio']` in your insert statement – Mihai Matei Mar 27 '13 at 09:39
  • i'm still getting an error in your foreach statement: **Warning: Invalid argument supplied for foreach() in C:\xampplite\htdocs\LPtest\insert.php on line 37** i do appreciate your help though, i'm several steps further in my mission! – CodeSigns Mar 27 '13 at 09:44
  • That's because you didn't select any checkbox.. To prevent this you should verify if your array contains any value. See my new edited answer! – Mihai Matei Mar 27 '13 at 09:47
  • That's not the case, i already edited the code with a count-function. same error still occurs. I've asked for some external help and i'm going to solve my problem by using dropdownboxes, but i thank you for your time, you have been most helpful, thank you! – CodeSigns Mar 27 '13 at 10:05
1

$_POST['regio'] is a multi-dimensional array. You should use foreach to get the individual values out of it.

CaptainCarl
  • 3,411
  • 6
  • 38
  • 71
0

You should use 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 code :

$chkBoxes = $_POST['Regio'];
foreach ($chkBoxes as $key => $val) {
     // you code here
}
mehdi
  • 1,755
  • 2
  • 15
  • 21