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!

2
  • 2
    what is the result of real_escape_string over Regio array? I think, it is defined only for strings.... and it returns string, which is not an array which can be imploded... Commented Mar 27, 2013 at 9:08
  • What code is it on line 38? Commented Mar 27, 2013 at 9:15

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

7 Comments

best practice is to $adds['Regio']=array(); before the loop
+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
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?
If you're using exactly my code.. you have to add just $adds['Regio'] in your insert statement
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!
|
1

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

Comments

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.