简体   繁体   English

将数组值插入数据库

[英]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. 另外,不要忘记未选中的复选框不会产生$ _POST值,因此,如果您未选择任何复选框,则$_POST['check_list']将为空。

Please, take a look here post checkbox value 请在此处查看发布复选框值

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

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