使用foreach

时间:2016-04-11 17:28:59

标签: php mysql arrays

我有三个数组,我想将它们写入数据库,我面临的问题是每当将值写入特定列时,列的其余部分都为空。

$name_array = array(3) { [0]"Name1" [1]=>"Name2" [2]=> "Name3" } 
$roll_array = array(3) { [0]=>"1" [1]=>"2" [2]=>"3" } 
$att_array =  array(3) { [0]=>"Present" [1]=>"Present" [2]=>"absent" }

我在DB中有三列“NAME”“ROLL”“ATTENDANCE” 我想同时将所有数组数据存储到数据库中。 所以看起来应该是这样的

NAME      ROLL     ATTENDANCE
Name1     1         present
Name2     2         present
Name3     3         absent

这是我尝试的代码,但它只是将每个值添加到列中,并将另一列留空。所以前三行只有ROLLNO,接下来的三行只有NAME,最后三行只有ATTENDANCE。

$name_values = array();
$roll_values = array();
$att_values = array();


foreach ($name_array as $key => $name_values) {

    $name_values = mysqli_real_escape_string($connection,$name_values);
    $sql= "INSERT INTO `aclass12` (Name) VALUES ('$name_values')";
    mysqli_query($connection,$sql);

}

foreach ($roll_array as $key => $roll_values) {
    $roll_values = mysqli_real_escape_string($connection,$roll_values); 
    $sql= "INSERT INTO `aclass12` (RollNo) VALUES ('$roll_values')";
}

foreach ($att_array as $key => $att_values) {
    $att_values = mysqli_real_escape_string($connection,$att_values);   
    $sql= "INSERT INTO `aclass12` (attendance) VALUES ('$att_values')";
}

我知道这不是正确的做法。什么是这样做的?

5 个答案:

答案 0 :(得分:3)

只需使用一个数组作为主数据,并使用该数组的键来访问其他2个数组数据。

然后将所有数据插入单个INSERT

检查INSERT实际工作也是个好主意,所以我添加了一些错误检查

foreach ($name_array as $key => $value) {

    $name = mysqli_real_escape_string($connection,$value);
    $roll = mysqli_real_escape_string($connection,$roll_values[$key]); 
    $att  = mysqli_real_escape_string($connection,$att_array[$key]); 

    $sql = "INSERT INTO `aclass12` 
                   (Name, RollNo, attendance) 
            VALUES ('$value', '$roll', '$att')";

    $res = mysqli_query($connection,$sql);
    if ( $res === FALSE ) {
        echo mysqli_error();
        exit;
    }

}

答案 1 :(得分:0)

foreach($name_array as $n_k=>$name) {
    $roll = (isset($roll_array[$n_k])) ? $roll_array[$n_k] : '';
    $att = (isset($att_array[$n_k])) ? $att_array[$n_k] : '';
    $name = mysqli_real_escape_string($connection,$name);
    $roll = mysqli_real_escape_string($connection,$roll);
    $att = mysqli_real_escape_string($connection,$att);
    $sql= "INSERT INTO `aclass12` (Name, RollNo, attendance) VALUES ('$name','$roll','$att')";
    mysqli_query($connection,$sql);
}

答案 2 :(得分:0)

只使用一个foreach并访问那里的数组元素。像这样:

foreach ($name_array as $key => $name_values) {
    $name_values = mysqli_real_escape_string($connection,$name_values);
    $roll_values = mysqli_real_escape_string($connection,$roll_array[$key]);
    $att_values = mysqli_real_escape_string($connection,$att_array[$key]);
    $sql= "INSERT INTO `aclass12` (Name, RollNo, attendance) VALUES ('$name_values', '$roll_values', '$att_values')";
    mysqli_query($connection,$sql);
}

此外,建议使用预准备语句,因为它们可以防止SQL注入攻击。更多信息here

答案 3 :(得分:0)

尝试这种方式

for($i = 0; $i < count($name_array);$i++) {
    $name_values = mysqli_real_escape_string($connection,$name_array[$i]);
    $roll_values = mysqli_real_escape_string($connection,$roll_array[$i]);
    $att_values = mysqli_real_escape_string($connection,$att_array[$i]); 
    $sql= "INSERT INTO `aclass12` (Name, RollNo, attendance) VALUES ('$name_values', '$roll_values','$att_values')";
}

其他选项是使用带有foreach的多维数组。

答案 4 :(得分:0)

我认为最好使用自mysql查询以来注入它,并在此之前简单地连接所有内容。这是这样的:

$query = "INSERT INTO tbl_name (col1, col2, col3) VALUES ";

for ($i = 0; $i < count($name_array); $i++) {
    $name = mysqli_real_escape_string($conn, $name_array[$i]);
    $roll = mysqli_real_escape_string($conn, $roll_array[$i]);
    $att = mysqli_real_escape_string($conn, $att_array[$i]);
    $query .= "('{$name}', '{$roll}', '{$att}'),";       
}

$query = trim($query, ',');
$query = $query . ';';
mysqli_query($connection,$sql);

在那里添加一些伤害控制(检查错误)就是这样。