【问题标题】:How do I loop through a 3 dimensional array to insert data into the database?如何循环通过 3 维数组将数据插入数据库?
【发布时间】:2018-10-16 15:22:24
【问题描述】:

这是我的 3 维数组。

我的 $_POST 数组。这些是价值观

$_POST = Array ( 
[9] => Array ( [student_id] => 9 [subject_id] => 6 [class_id] => 2 [mark_obtained] => 40 [mark_obtainable] => 100 ) 
[10] => Array ( [student_id] => 10 [subject_id] => 6 [class_id] => 2 [mark_obtained] => 52 [mark_obtainable] => 100 ) 
[11] => Array ( [student_id] => 11 [subject_id] => 6 [class_id] => 2 [mark_obtained] => 30 [mark_obtainable] => 100 ) 
[12] => Array ( [student_id] => 12 [subject_id] => 6 [class_id] => 2 [mark_obtained] => 68 [mark_obtainable] => 100 ) )

我如何遍历它并将每个数组提交到数据库中?

我的列名是关键。 “student_idsubject_idclass_idmark_obtainedmark_obtainable”。

我正在尝试这个,但我不断收到重复的条目。`foreach ($_POST as $i => $values1) {

foreach ($values1 as $key => $value) {
    $columns = implode(", ",array_keys($values1));
    $values  = implode(", ", $values1);
    $columns = $columns
    $sql = "INSERT INTO `daily_report`($columns) VALUES ($values)";

    if ($conn->multi_query($sql) === TRUE) {
        echo "New records created successfully <br>";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}

【问题讨论】:

  • 对我来说看起来像一个二维数组。主数组中的每个条目都会是数据库中的一行吗?如果是这样,那么您只需要一个 foreach 循环,每个循环都会执行一个 INSERT 语句。我们是否应该假设第一个 sn-p 中显示的数组实际上是 $_POST 数组?
  • 是的。第一个 sn-p 是 $_POST 数组。
  • 好吧,我怀疑我认为你只需要一个循环

标签: php mysql arrays


【解决方案1】:

你必须像这样在 foreach 循环中回显 sql 变量。

foreach ($_POST as $data => $values) {

echo $sql = "INSERT INTO `daily_report` (student_id, subject_id, class_id,mark_obtained,mark_obtainable,daily_report.date)
VALUES (".$values['student_id'].", ".$values['subject_id'].", ".$values['class_id'].", ".$values['mark_obtained'].",".$values['mark_obtainable'].", now())";

// echo $sqlupdate1 = "UPDATE table SET commodity_quantity=$qty WHERE user='".$rows['user']."' ";
if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

在回显时连接变量。

【讨论】:

  • 这是正确的并且会起作用,但请注意,这极易受到 SQL 注入攻击。实际上,应该使用参数化查询和准备好的语句。有关风险的说明,请参阅bobby-tables.com,以及使用带有 mysqli 或 PDO 的 PHP 安全编写查询的一些示例
【解决方案2】:

您不需要 3 维数组。您可以使用二维数组,并使用foreach 将它们一一插入。这是一个例子:

 $data_array = [
    [
        'student_id' => 9,
        'subject_id' => 6, 
        'class_id' => 2,
        'mark_obtained' => 40,
        'mark_obtainable' => 100    
    ],
        [
        'student_id' => 10,
        'subject_id' => 6, 
        'class_id' => 2,
        'mark_obtained' => 52,
        'mark_obtainable' => 100    
    ],
        [
        'student_id' => 11,
        'subject_id' => 6, 
        'class_id' => 2,
        'mark_obtained' => 30,
        'mark_obtainable' => 100    
    ],
        [
        'student_id' => 12,
        'subject_id' => 6, 
        'class_id' => 2,
        'mark_obtained' => 30,
        'mark_obtainable' => 100    
    ],
];

foreach($data_array as $data) {
$sql = "INSERT INTO `daily_report` (student_id, subject_id, class_id,mark_obtained,mark_obtainable)
VALUES ($data['student_id'], $data['subject_id'], $data['class_id'], $data['mark_obtained'],$data['mark_obtainable'])";
if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

【讨论】:

  • 我不相信在循环中执行查询是个好主意。并查看准备好的语句 - 有一些非常聪明的示例将它们与多行插入结合使用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-27
  • 1970-01-01
  • 2015-06-08
  • 1970-01-01
  • 2018-06-29
  • 2019-10-18
  • 2014-06-13
相关资源
最近更新 更多