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

时间:2019-02-05 08:36:38

标签: php mysql codeigniter

首先,我希望您能理解我提出的问题,因为我不是英语。我正在制作简单的足球联赛表。因此,我想记录主队和客队之间所有球队的进球,包括球员的助攻,以及使用PHP表单自己的进球。问题是我想在 ONE 中将它们提交foreach()而不是foreach()来插入目标,foreach()来插入辅助,以及foreach()来插入自己的目标。

我尝试使用foreach(),但我只知道这是针对一个索引数组变量或关联数组的;

表单输入目标,协助者和自己目标的代码(来自JSON编码):

'.... <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>'+ ....

从主队获得数据的代码:

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

我试图将数据插入数据库,如下所示:

 //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
                                        ");
                    }

但是我期望这样的事情(我知道这是错误的):

    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
                     ");

我知道这是错误的,但是我不知道最好的解释方式,我一直在寻找这个类似的问题,但是没有人可以解决,或者我听不懂。谢谢

2 个答案:

答案 0 :(得分:0)

在所有阵列中,您使用相同的索引。因此,一种简单的方法是运行一个数组并使用索引从每个循环中具有给定索引的所有数组中获取数据。

$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");
}

但是您也可以通过以下方式更改输入字段的名称

<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+']">

<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]">

然后您将获得一个包含所有数据的数组,您可以像这样处理数据:

$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 :(得分:0)

这么多的更新。这是非常低效的。我将收集所有数据作为SQL值,用所有数据创建一个临时表,然后更新主表。

您的输入字段共享相同的索引。好!收集数据:

# assumeing, that $goal_home, $assist_home and $owngoal_home exist
$keys = array_keys($goal_home);
$values = '';

foreach ( $keys as key )
{
    # assume, that all value are integers.
    # '%d' avoids SQL injection

    $key = intval($key);
    $values .= sprintf(',"%s",%d,%d,%d)',
                 $key, $goal_home[$key],
                 $assist_home[$key], $owngoal_home[$key] );
}
$values = substr($values,1); # delete first comma

现在,我们创建一个TEMPORARY表,该表将自动 关闭数据库连接后删除。 但是首先,我们删除表以允许多次使用。

$this->m->query("DROP TABLE IF EXISTS update_temp");
$query = <<< __EOT__
    CREATE TEMPORAY TABLE update_temp
    (
        id_player int not null primary key,
        goal      int not null,
        assist    int not null,
        owngoal   int not null
    )
__EOT__;
$this->m->query($quote);

现在我们填写表格:

$query = <<< __EOT__
    INSERT INTO update_temp (id_player,goal,assist,owngoal)
    VALUES $values
__EOT__;
$this->m->query($quote);

现在我们更新主表

$query = <<< __EOT__
    UPDATE tbl_player p, update_temp t
    SET p.goal    = p.goal    + t.goal,
        p.assist  = p.assist  + t.assist,
        p.owngoal = p.owngoal + t.owngoal
    WHERE p.id_player = t.id_player
__EOT__;
$this->m->query($quote);

最后至少要注意的几点:

  • 我写下了这段代码,没有对其进行测试。因此,将其用作模板并验证语法和语义。
  • 必须添加错误处理。
  • <<< __EOT__,直到__EOT__;PHP here document