简体   繁体   中英

Insert array value into database

I have an array of checkboxes whose value I want to insert into a database. The checkbox declaration is like this:

echo '<td><input type="checkbox" name="check_list[]"       
onClick="toggle(this, '.$x.')" /> All</td>';

And here I try to insert it (together with other variables, that doesnt really matter). So, the other variables are inserted, while this one isnt.

<?php
    ob_start(); //eliminates buffer collisions
    require_once('connect_db.php'); 
    $id = time(); //creates a unique id using the unix time
    $check =  $_POST['check_list[0]']; 
    $result = pg_query(connect(), "INSERT INTO lecturer VALUES ($id, '$_POST[name]','$_POST[surname]','$_POST[department]', $check)");  


?>

What is wrong with my syntax? Cause all the values are inserted, besides that of the checkbox.

First of all you should access the value in such way:

$_POST['check_list'][0]

Also, do not forget that non-selected checkboxes do not produces $_POST values, so if you did not select any checkbox, then $_POST['check_list'] will be empty.

Please, take a look here post checkbox value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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