1

I want insert multiple checkbox values in Mysql using PDO.

HTML Form part checkbox

<form action="insert.php">
    <label>
        <input name="check[]" id="check[]" type="checkbox" value="<?=$row['id']?>" />
        <span class="lbl"></span>
    </label>
    <button type="submit" name="send" id="send" >
</form>

PHP Part insert

$checkbox = $_POST['check'];

for (i$=0; $i<sizeof($checkbox);$i++)

    $insert = $pdo->prepare("INSERT INTO table (check_value) VALUES (?)");
    $insert->execute(array(".$checkbox[$i]."));

More when i use ".$checkbox[$i]." give "Notice: Array to string conversion"

5
  • There's only one checkbox there. Could you post the whole code, maybe including the php part? I believe this is supposed to be in a loop coming from a database. Commented Jul 23, 2014 at 12:05
  • “HERE I WANT THE INSTRUCTION FOR INSERT EVERY CHECKBOX VALUE IN A ROW” – there is no such thing. Use a loop. Commented Jul 23, 2014 at 12:06
  • Beter to move that checkbox out of the label and start using the forattribute Commented Jul 23, 2014 at 12:07
  • Yeah, in truly the checkbox be inside a table, this could help? Commented Jul 23, 2014 at 12:07
  • I try this "for (i$=0; $i<sizeof($checkbox);$i++)" and 'code'".$checkbox[$i]." Commented Jul 23, 2014 at 12:17

1 Answer 1

1

One way to do this is build the SQL dynamicaly. Just add as many ? to the SQL as there are checkbox values.

<?php
if (isset($_POST['check'])) {
    $sql = 'INSERT INTO TABLE (check_value) VALUES ({foo})';

    $foo = '';
    foreach($_POST['check'] as $key => $value) $foo .= '?,';
    $foo = rtrim($foo, ',');

    $sql = str_replace('{foo}', $foo, $sql);

    $insert = $pdo->prepare($sql);
    $insert->execute($_POST['check']);
}
Sign up to request clarification or add additional context in comments.

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.