douhuang2282 2019-02-05 08:36
浏览 89
已采纳

如何使用一个foreach从索引数组文本输入名称PHP向多列数据库插入数据?

first I hope You understand the question I made because I'm not English. I'm making simple football league table. So I want to record all the team's goal between home team and away team including the players assists, and own goals using PHP form. the problem is I want to SUBMIT them in ONE foreach() instead of foreach() for inserting the goals, foreach() for inserting the assists, and foreach() for inserting the own goals.

I have tried using foreach(), and I just only know that is for ONE indexed array variable or Associative Array;

The code of form input goals, asssists, and own goal (this is from JSON encode):

'.... <td><input class="goal-home form-control col-6" name="goal_home['+data.player[i].id_player+']" type="number" value="0"></td>'+
'<td><input class="assist-home form-control col-6" name="assist_home['+data.player[i].id_player+']" type="number" value="0"></td>'+
'<td><input class="owngoal-home form-control col-6" name="owngoal_home['+data.player[i].id_player+']" type="number" value="0"></td>'+ ....

The code for get the data from home team side:

$goal_home = $this->input->post('goal_home');
$assist_home = $this->input->post('assist_home');
$owngoal_home = $this->input->post('owngoal_home');

And i have tried to insert the data to database something like this:

 //The code for inserting each player's goal
    foreach ($goal_home as $goal => $val) {
                        $this->m->query("UPDATE tbl_player 
                                        SET goal = (goal + $val)
                                        WHERE id_player = $goal
                                        ");
                    }
    //The code for inserting each player's assist
    foreach ($assist_home as $assist => $val) {
                        $this->m->query("UPDATE tbl_player 
                                        SET assist = (assist + $val)
                                        WHERE id_player = $assist
                                        ");
                    }
    //The code for inserting each player's own goals
    foreach ($owngoal_home as $owngoal => $val) {
                        $this->m->query("UPDATE tbl_player 
                                        SET owngoal = (owngoal + $val)
                                        WHERE id_player = $owngoal
                                        ");
                    }

but my expecting something like this (I know this is wrong):

    foreach (($goal_home as $goal => $val1), ($assist_home as $assist => $val2), ($owngoal_home as $owngoal => $val3)) {

$this->m->query("UPDATE tbl_player 
                      SET goal = (goal + $val1), assist = (assist + $val2) ,owngoal = (owngoal + $val3)
                      WHERE id_player = $goal
                     ");

I know that was wrong but I don't know the best way to explain this, I've been searching this similiar problem, but no one can resolve or maybe I don't understand. Thank you

  • 写回答

2条回答 默认 最新

  • doutou7549 2019-02-05 08:52
    关注

    In all of your arrays, you use the same indexes. So an easy way would be to run trough one of the arrays and use the index to get the data from all arrays with the given index within each loop.

    $goal_home    = $this->input->post('goal_home');
    $assist_home  = $this->input->post('assist_home');
    $owngoal_home = $this->input->post('owngoal_home');
    
    foreach ($goal_home as $id => $dummy) {
     $this->m->query("UPDATE
                            tbl_player 
                         SET
                            Goal    = (goal    + $goal_home[$id]),
                            assist  = (assist  + $assist_home[$id])
                            owngoal = (owngoal + $owngoal_home[$id])
                       WHERE
                            id_player = $id");
    }
    

    But you could also cange the names of your input fields from

    <input name="goal_home['+data.player[i].id_player+']">
    <input name="assist_home['+data.player[i].id_player+']">
    <input name="owngoal_home['+data.player[i].id_player+']">
    

    to

    <input name="player['+data.player[i].id_player+'][goals]">
    <input name="player['+data.player[i].id_player+'][assists]">
    <input name="player['+data.player[i].id_player+'][owngoals]">
    

    Then you get one Array with all the data in it and you can go trough the data like this:

    $data = $this->input->post('player');
    
    foreach ($data as id => $item) {
     $this->m->query("UPDATE
                            tbl_player 
                         SET
                            Goal    = (goal    + $item['goals']),
                            assist  = (assist  + $item['assists'])
                            owngoal = (owngoal + $item['owngoals'])
                       WHERE
                            id_player = $id");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教