php把几个数据放进数组,php – 如何将多个数组插入数据库?

我从表单中获取一些数据作为数组.每个$_POST值都是一个数组本身:

//Example snippet from my code; I have some more data/arrays

$department_name = ($_POST[department_name]);

$participant_name = ($_POST[participant_name]);

$activity = ($_POST[activity]);

$location = ($_POST[location]);

现在我知道我可以使用foreach循环遍历每个数组并将值逐个插入到我的数据库中:

foreach($department_name as $department) {

$query = "INSERT INTO location_info (`department`) VALUES ('{$department}')";

$result = mysqli_query($connection, $query);

}

这似乎是我所有35个POST变量的大量代码以及服务器的大量工作.更重要的是,我将如何“对齐”每个数据?由于循环,它将在每次迭代时在数据库内创建一个新行,并为所有其他列留下空白字段.

所以我搜索了如何一次循环多个数组并找到了这个解决方案:

$ZZ = array('a', 'b', 'c', 'd');

$KK = array('1', '2', '3', '4');

foreach($ZZ as $index => $value) {

echo $ZZ[$index] . $KK[$index];

echo "
";

}

?>

但我真的不明白这是如何工作的以及如何将其应用到我的代码中?

基本上作为一个例子,我有多个数组,如:

$department_name = array("A", "B", "C");

$participant_name = array(1, 2, 3);

我需要将它们插入我的数据库,如下所示:

INSERT INTO location_info (`department`, `participant`) VALUES ('A', 1);

INSERT INTO location_info (`department`, `participant`) VALUES ('B', 2);

INSERT INTO location_info (`department`, `participant`) VALUES ('C', 3);

所以我认为我必须使用foreach循环并一次遍历所有数组以逐行插入数据,但我没有看到如何将上面找到的代码应用到我的代码中?

经过一些有用的评论后,我转而使用PDO并取得了一些进展.

我当前的代码如下所示.

连接:

try {

$dsn = 'mysql:host=localhost;dbname=assessment';

$db = new PDO($dsn, 'xxx', 'xxx');

} catch (Exception $e) {

$error = $e->getMessage();

}

?>

再往下我有:

try {

$sql = "INSERT INTO location_info (`department`, `participant`, `activity`, `location`, `rec_injuries`, `rec_injuries_timeframe`, `non_rec_injuries`, `non_rec_injuries_timeframe`, `competitor`, `cost_per_pair`, `usage_rate`, `leakage`, `cost_of_productivity`, `non_rec_impact`, `non_rec_sprain`, `non_rec_puncture`, `non_rec_dermatitis`, `non_rec_infection`, `non_rec_burns`, `non_rec_cuts`, `rec_impact`, `rec_sprain`, `rec_puncture`, `rec_dermatitis`, `rec_infection`, `rec_burns`, `rec_cuts`, `condition`, `general_id`)

VALUES (:department, :participant, :activity, :location, :rec_injuries, :rec_injuries_timeframe, :non_rec_injuries_timeframe, :competitor, :cost_per_pair, :usage_rate, :leakage, :cost_of_productivity,:non_rec_impact, :non_rec_sprain, :non_rec_puncture, :non_rec_dermatitis, :non_rec_infection, :non_rec_burns, :non_rec_cuts, :rec_impact, :rec_sprain, :rec_puncture, :rec_dermatitis, :rec_infection, :rec_burns, :rec_cuts, :condition, :general_id)";

$stmt = $db->prepare($sql);

for($i = 0, $l = count($_POST["department_name"]); $i < $l; $i++) {

$loc_info = array(':department' => $_POST["department_name"][$i],

':participant' => $_POST["participant_name"][$i],

':activity' => $_POST["activity"][$i],

':location' => $_POST["location"][$i],

':rec_injuries' => $_POST["injuries"][$i],

':rec_injuries_timeframe' => $_POST["injury_time_frame"][$i],

':non_rec_injuries' => $_POST["non_rec_injuries"][$i],

':non_rec_injuries_timeframe' => $_POST["non_rec_injury_timeframe"][$i],

':competitor' => $_POST["competitor"][$i],

':cost_per_pair' => $_POST["cost_per_pair"][$i],

':usage_rate' => $_POST["usage_rate"][$i],

':leakage' => $_POST["leakage"][$i],

':cost_of_productivity' => $_POST["cost_of_productivity"][$i],

':non_rec_impact' => $_POST["non_rec_impact"][$i],

':non_rec_sprain' => $_POST["non_rec_sprain"][$i],

':non_rec_puncture' => $_POST["non_rec_puncture"][$i],

':non_rec_dermatitis' => $_POST["non_rec_dermatitis"][$i],

':non_rec_infection' => $_POST["non_rec_infection"][$i],

':non_rec_burns' => $_POST["non_rec_burns"][$i],

':non_rec_cuts' => $_POST["non_rec_cuts"][$i],

':rec_impact' => $_POST["impact"][$i],

':rec_sprain' => $_POST["sprain"][$i],

':rec_puncture' => $_POST["puncture"][$i],

':rec_dermatitis' => $_POST["dermatitis"][$i],

':rec_infection' => $_POST["infection"][$i],

':rec_burns' => $_POST["burns"][$i],

':rec_cuts' => $_POST["cuts"][$i],

':condition' => $_POST["condition"][$i],

':general_id' => $_POST["id"][$i]

);

$stmt->execute($loc_info);

}

} catch (Exception $e) {

$error = $e->getMessage();

}

但这仍然无效.

思考?我可以不在数组中放一个数组吗?

解决方法:

经过一些试验和错误以及@ Rizier123的一些帮助后,这是答案:

表单页面上的HTML

为了清楚起见,我试图弄清楚如何将几个数据数组添加到我的数据库中.所以在动态表单页面上,我输入类似于:

Location:

处理表单并将数据输入到DB

首先,我从mysqli切换到PDO,然后运行以下代码:

try {

$sql = "INSERT INTO location_info (`department`, `participant`, `activity`, `location`, `rec_injuries`, `rec_injuries_timeframe`, `non_rec_injuries`, `non_rec_injuries_timeframe`, `competitor`, `cost_per_pair`, `usage_rate`, `leakage`, `cost_of_productivity`, `non_rec_impact`, `non_rec_sprain`, `non_rec_puncture`, `non_rec_dermatitis`, `non_rec_infection`, `non_rec_burns`, `non_rec_cuts`, `rec_impact`, `rec_sprain`, `rec_puncture`, `rec_dermatitis`, `rec_infection`, `rec_burns`, `rec_cuts`, `condition`, `general_id`)

VALUES (:department, :participant, :activity, :location, :rec_injuries, :rec_injuries_timeframe, :non_rec_injuries, :non_rec_injuries_timeframe, :competitor, :cost_per_pair, :usage_rate, :leakage, :cost_of_productivity,:non_rec_impact, :non_rec_sprain, :non_rec_puncture, :non_rec_dermatitis, :non_rec_infection, :non_rec_burns, :non_rec_cuts, :rec_impact, :rec_sprain, :rec_puncture, :rec_dermatitis, :rec_infection, :rec_burns, :rec_cuts, :condition, '{$id}')";

$stmt = $db->prepare($sql);

for($i = 0, $l = count($_POST["department_name"]); $i < $l; $i++) {

$loc_info = array(':department' => $_POST["department_name"][$i],

':participant' => $_POST["participant_name"][$i],

':activity' => $_POST["activity"][$i],

':location' => $_POST["location"][$i],

':rec_injuries' => $_POST["injuries"][$i],

':rec_injuries_timeframe' => $_POST["injury_time_frame"][$i],

':non_rec_injuries' => $_POST["non_rec_injuries"][$i],

':non_rec_injuries_timeframe' => $_POST["non_rec_injury_timeframe"][$i],

':competitor' => $_POST["competitor"][$i],

':cost_per_pair' => $_POST["cost_per_pair"][$i],

':usage_rate' => $_POST["usage_rate"][$i],

':leakage' => $_POST["leakage"][$i],

':cost_of_productivity' => $_POST["cost_of_productivity"][$i],

':non_rec_impact' => $_POST["non_rec_impact"][$i],

':non_rec_sprain' => $_POST["non_rec_sprain"][$i],

':non_rec_puncture' => $_POST["non_rec_puncture"][$i],

':non_rec_dermatitis' => $_POST["non_rec_dermatitis"][$i],

':non_rec_infection' => $_POST["non_rec_infection"][$i],

':non_rec_burns' => $_POST["non_rec_burns"][$i],

':non_rec_cuts' => $_POST["non_rec_cuts"][$i],

':rec_impact' => $_POST["impact"][$i],

':rec_sprain' => $_POST["sprain"][$i],

':rec_puncture' => $_POST["puncture"][$i],

':rec_dermatitis' => $_POST["dermatitis"][$i],

':rec_infection' => $_POST["infection"][$i],

':rec_burns' => $_POST["burns"][$i],

':rec_cuts' => $_POST["cuts"][$i],

':condition' => $_POST["condition"][$i] );

$stmt->execute($loc_info);

}

标签:php,arrays,mysql,foreach

来源: https://codeday.me/bug/20191002/1844773.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值