将数组值插入数据库

时间:2022-09-09 15:39:52

I have this system where I store checkbox data into an array. So if it is checked it will put the value in the array. Let's say that there are 6 checkboxes, and I check the last 3, then array values [3][4] and [5] will have values, correct? Ok.

我有这个系统,我将复选框数据存储到一个数组中。因此,如果选中它,它会将值放入数组中。假设有6个复选框,我检查最后3个,然后数组值[3] [4]和[5]将有值,对吗?好。

Now if the array values [0][1] and [2] are 0 as they haven’t been checked, what is their value?

现在,如果数组值[0] [1]和[2]为0,因为它们尚未被检查,它们的值是多少?

The question is, that when I do a MySQL insert to a database and I use this code?

问题是,当我对数据库进行MySQL插入并使用此代码时?

mysqli_query($con,"INSERT INTO accounts (chk1, chk2, chk3, chk4, chk5, chk6) VALUES ('$checkbox[0]', '$checkbox[1]', '$checkbox[2]', '$checkbox[3]', '$checkbox[4]', '$checkbox[5]'");

Now, when that query executes, if the first arrays are nothing, it will skip them and pretend they are not there. Is there a way I can make them just put 0 if they haven’t been checked.

现在,当该查询执行时,如果第一个数组什么都没有,它将跳过它们并假装它们不在那里。有没有办法,如果他们没有被检查,我可以把它们只放0。

5 个解决方案

#1


0  

put this code before your query

将此代码放在查询之前

$checkbox[0]=(isset($checkbox[0]))?$checkbox[0]:0;
$checkbox[1]=(isset($checkbox[1]))?$checkbox[1]:0;
$checkbox[2]=(isset($checkbox[2]))?$checkbox[2]:0;
$checkbox[3]=(isset($checkbox[3]))?$checkbox[3]:0;
$checkbox[4]=(isset($checkbox[4]))?$checkbox[4]:0;
$checkbox[5]=(isset($checkbox[5]))?$checkbox[5]:0;

OR

// get $_POST value
function initPostValue($elementVar, $defVal=null) {
    if(!isset($_POST[$elementVar])){
        $_POST[$elementVar] = $defVal;
    }
    return $_POST[$elementVar];
}
// get $_REQUEST value
function initRequestValue($elementVar, $defVal=null) {
    if(!isset($_REQUEST[$elementVar])){
        $_REQUEST[$elementVar] = $defVal;
    }
    return $_REQUEST[$elementVar];
}
// get $_GET value
function initGetValue($elementVar, $defVal=null) {
    if(!isset($_GET[$elementVar])){
        $_GET[$elementVar] = $defVal;
    }
    return $_GET[$elementVar];
}

Example : $checkbox[0]  = initRequestValue('checkbox[0]',0);

use above function for get any value take from $_GET,$_POST,$_REQUEST so there are no need to for check empty

使用上面的函数从$ _GET,$ _ POST,$ _ REQUEST获取任何值,所以没有必要检查为空

#2


1  

Why not just apply a DEFAULT 0 constraint to every check{n} field, in the database?

为什么不在数据库中的每个check {n}字段中应用DEFAULT 0约束?

That way, (1) you will make your data protect themselves regardless of the interface and (2) there would be less code to be written, since less checks would be necessary.

这样,(1)无论接口如何,您都将自己保护数据;(2)编写的代码更少,因为需要更少的检查。

#3


0  

    if(!isset($checkbox[1]))
           $checkbox[1]=0;

#4


0  

They will not have values.

他们没有价值观。

You also want to sanity check them using mysqli_real_escape_string.

您还希望使用mysqli_real_escape_string进行健全性检查。

I'd do an isset on each checkbox item and set it to 0 if it's not there.

我会在每个复选框项上执行isset,如果不存在则将其设置为0。

Code example as requested:

请求的代码示例:

$checkbox[0] = isset($_REQUEST['checkboxName']) ? mysql_real_escape_string($_REQUEST[$_REQUEST['checkboxName']]) ? 0 ;

if the value is always 1, then use 1 instead of mysql_real_escape_string($_REQUEST[$_REQUEST['checkboxName']])

如果值始终为1,则使用1而不是mysql_real_escape_string($ _ REQUEST [$ _ REQUEST ['checkboxName']])

#5


0  

$data = array();
for($a=0; $a<max(array_keys($checkbox)); $a++) {
    $data["chk$a"] = $checkbox[$a] ?: '0';
}
$sql = 'INSERT INTO accounts (\''.implode('\',\'', array_keys($data)).'\') VALUES ('.implode(',',$data).')';

#1


0  

put this code before your query

将此代码放在查询之前

$checkbox[0]=(isset($checkbox[0]))?$checkbox[0]:0;
$checkbox[1]=(isset($checkbox[1]))?$checkbox[1]:0;
$checkbox[2]=(isset($checkbox[2]))?$checkbox[2]:0;
$checkbox[3]=(isset($checkbox[3]))?$checkbox[3]:0;
$checkbox[4]=(isset($checkbox[4]))?$checkbox[4]:0;
$checkbox[5]=(isset($checkbox[5]))?$checkbox[5]:0;

OR

// get $_POST value
function initPostValue($elementVar, $defVal=null) {
    if(!isset($_POST[$elementVar])){
        $_POST[$elementVar] = $defVal;
    }
    return $_POST[$elementVar];
}
// get $_REQUEST value
function initRequestValue($elementVar, $defVal=null) {
    if(!isset($_REQUEST[$elementVar])){
        $_REQUEST[$elementVar] = $defVal;
    }
    return $_REQUEST[$elementVar];
}
// get $_GET value
function initGetValue($elementVar, $defVal=null) {
    if(!isset($_GET[$elementVar])){
        $_GET[$elementVar] = $defVal;
    }
    return $_GET[$elementVar];
}

Example : $checkbox[0]  = initRequestValue('checkbox[0]',0);

use above function for get any value take from $_GET,$_POST,$_REQUEST so there are no need to for check empty

使用上面的函数从$ _GET,$ _ POST,$ _ REQUEST获取任何值,所以没有必要检查为空

#2


1  

Why not just apply a DEFAULT 0 constraint to every check{n} field, in the database?

为什么不在数据库中的每个check {n}字段中应用DEFAULT 0约束?

That way, (1) you will make your data protect themselves regardless of the interface and (2) there would be less code to be written, since less checks would be necessary.

这样,(1)无论接口如何,您都将自己保护数据;(2)编写的代码更少,因为需要更少的检查。

#3


0  

    if(!isset($checkbox[1]))
           $checkbox[1]=0;

#4


0  

They will not have values.

他们没有价值观。

You also want to sanity check them using mysqli_real_escape_string.

您还希望使用mysqli_real_escape_string进行健全性检查。

I'd do an isset on each checkbox item and set it to 0 if it's not there.

我会在每个复选框项上执行isset,如果不存在则将其设置为0。

Code example as requested:

请求的代码示例:

$checkbox[0] = isset($_REQUEST['checkboxName']) ? mysql_real_escape_string($_REQUEST[$_REQUEST['checkboxName']]) ? 0 ;

if the value is always 1, then use 1 instead of mysql_real_escape_string($_REQUEST[$_REQUEST['checkboxName']])

如果值始终为1,则使用1而不是mysql_real_escape_string($ _ REQUEST [$ _ REQUEST ['checkboxName']])

#5


0  

$data = array();
for($a=0; $a<max(array_keys($checkbox)); $a++) {
    $data["chk$a"] = $checkbox[$a] ?: '0';
}
$sql = 'INSERT INTO accounts (\''.implode('\',\'', array_keys($data)).'\') VALUES ('.implode(',',$data).')';