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

如何使用一个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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化