問題描述
我從表單中獲取一些數(shù)據(jù)作為數(shù)組.每個(gè) $_POST
值本身就是一個(gè)數(shù)組:
I get some data from a form as arrays. Each $_POST
value is an array itself:
//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]);
現(xiàn)在我知道我可以使用 foreach 循環(huán)來遍歷這些數(shù)組中的每一個(gè)并將值一個(gè)一個(gè)地插入到我的數(shù)據(jù)庫中:
Now I know that I could use a foreach loop to loop over each of these arrays and insert the values one by one into my database:
foreach($department_name as $department) {
$query = "INSERT INTO location_info (`department`) VALUES ('{$department}')";
$result = mysqli_query($connection, $query);
}
這似乎是我所有 35 個(gè) POST 變量的大量代碼以及服務(wù)器的大量工作.更重要的是,我將如何對(duì)齊"每條數(shù)據(jù)?由于循環(huán),它會(huì)在每次迭代時(shí)在數(shù)據(jù)庫內(nèi)創(chuàng)建一個(gè)新行,并為所有其他列保留空白字段.
This seems like a lot of code for all my 35 POST variables as well as a lot of work for the server. More importantly how would I go about to "align" each piece of data? Because of the loop it would create a new row inside the database each iteration and leave blank fields for all other columns.
所以我搜索了如何一次遍歷多個(gè)數(shù)組并找到了這個(gè)解決方案:
So I searched how I could loop through multiple arrays at once and found this solution:
<?php
$ZZ = array('a', 'b', 'c', 'd');
$KK = array('1', '2', '3', '4');
foreach($ZZ as $index => $value) {
echo $ZZ[$index] . $KK[$index];
echo "<br/>";
}
?>
但我真的不明白這是如何工作的以及如何將其應(yīng)用于我的代碼?
But I don't really understand how this works and how I can apply this to my code?
基本上作為一個(gè)例子,我有多個(gè)數(shù)組,如:
Basically as an example I have multiple arrays like:
$department_name = array("A", "B", "C");
$participant_name = array(1, 2, 3);
我需要像這樣將它們插入到我的數(shù)據(jù)庫中:
And I need to insert them into my database like this:
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 循環(huán)并一次循環(huán)遍歷所有數(shù)組以逐行插入數(shù)據(jù),但我看不到如何將上面找到的代碼應(yīng)用于我的代碼?
So I think I have to use a foreach loop and loop over all arrays at once to insert the data row by row, but I fail to see how I can apply the code I found above to my code?
經(jīng)過一些有用的評(píng)論后,我切換到 PDO 并取得了一些進(jìn)展.
After some helpful comments I switched to PDO and made some progress.
我當(dāng)前的代碼如下所示.
My current code looks like the following.
連接:
<?php
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();
}
但這仍然不起作用.
想法?數(shù)組里面不能放數(shù)組嗎?
Thoughts? Can I not put an array inside an array?
推薦答案
經(jīng)過幾次嘗試和錯(cuò)誤以及@Rizier123 的一些幫助后,好的,這里是答案:
Ok after a few trial and errors and some help from @Rizier123, here is the answer:
表單頁面上的html
為了清楚起見,我試圖弄清楚如何將幾個(gè)數(shù)據(jù)數(shù)組添加到我的數(shù)據(jù)庫中.所以在動(dòng)態(tài)表單頁面上,我的輸入類似于:
For clarity sake, I was trying to figure out how add several arrays of data to my db. So on dynamic form page, I have inputs similar to:
<p>Location: <input type='text' name='location[]' > </p>
處理表單并將數(shù)據(jù)輸入數(shù)據(jù)庫
首先,我從 mysqli 切換到 PDO,然后我用以下代碼運(yùn)行:
First, I switched from mysqli to PDO, then I ran with the following code:
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);
}
這篇關(guān)于如何將多個(gè)數(shù)組插入到數(shù)據(jù)庫中?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!