[英]How to insert data to multiple column database from indexed array text input name PHP using one foreach?
首先,我希望您能理解我提出的问题,因为我不是英语。 我正在制作简单的足球联赛表。 因此,我想记录主队和客队之间所有球队的进球,包括球员的助攻,以及使用PHP表单自己的进球。 问题是我想将它们提交给一个 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
");
我知道那是错误的,但是我不知道最好的解释方式,我一直在寻找这个类似的问题,但是没有人可以解决,或者我听不懂。 谢谢
在所有阵列中,您使用相同的索引。 因此,一种简单的方法是运行一个数组并使用索引从每个循环中具有给定索引的所有数组中获取数据。
$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");
}
如此多的更新。 这是非常低效的。 我将收集所有数据作为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此处文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.