dsw7547
2018-05-06 19:29
浏览 301
已采纳

如何循环三维数组以将数据插入数据库?

This is my 3 dimensional array.

My $_POST array. These are the values

$_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 ) )

How do i loop through it and submit each of the array into the database?

My column names are the keys. "student_id, subject_id, class_id, mark_obtained, mark_obtainable".

I am trying this but i keep getting duplicate entries.`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;
    }
}

图片转代码服务由CSDN问答提供 功能建议

这是我的3维数组。

My $ _POST数组。 这些是值

  $ _ POST = Array(
 [9] =&gt; Array([student_id] =&gt; 9 [subject_id] =&gt; 6 [class_id]  =&gt; 2 [mark_obtained] =&gt; 40 [mark_obtainable] =&gt; 100)
 [10] =&gt;数组([student_id] =&gt; 10 [subject_id] =&gt; 6 [class_id] =&gt; 2  [mark_obtained] =&gt; 52 [mark_obtainable] =&gt; 100)
 [11] =&gt;数组([student_id] =&gt; 11 [subject_id] =&gt; 6 [class_id] =&gt; 2 [mark_obtained] =  &gt; 30 [mark_obtainable] =&gt; 100)
 [12] =&gt;数组([student_id] =&gt; 12 [subject_id] =&gt; 6 [class_id] =&gt; 2 [mark_obtained] =&gt; 68 [  mark_obtainable] =&gt; 100))
   
 
 

如何循环并将每个数组提交到数据库? < p>我的列名是键。 “ student_id subject_id class_id mark_obtained mark_obtainable ”。

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

  foreach(  $ values1 as $ key =&gt; $ value){
 $ columns = implode(“,”,array_keys($ values1)); 
 $ values = implode(“,”,$ values1); 
 $ columns =  $ columns 
 $ sql =“INSERT INTO`daily_report`($ columns)VALUES($ values)”; 
 
 if($ conn-&gt; multi_query($ sql)=== TRUE){
 echo“ 成功创建新记录&lt; br&gt;“; 
}其他{
 echo”错误:“。  $ sql。  “&LT峰; br&gt;” 中 。  $ conn-&gt;错误; 
} 
} 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • draxu26480 2018-05-07 16:43
    已采纳

    You have to echo the sql variable inside the foreach loop like this.

    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;
    }
    

    Concatenate the variables while echoing.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • duanhong8839 2018-05-06 19:56

    You don't need a 3 dimensional array. You can use a 2 dimensional array and by using foreach you can insert them one by one. Here is an example:

     $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;
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题