[英]INSERT multiple records in MySQL with one PHP form
使用一個PHP表單在MySQL中插入多個記錄。
簡單的形式
<form action="process.php" method="post">
<p><label>Beamline ID</label>
<input type="text" name="bline_id[][bline_id]" />
<label>Flow</label>
<input type="text" name="flow[][flow]" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[][bline_id]" />
<label>Flow</label>
<input type="text" name="flow[][flow]" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[][bline_id]" />
<label>Flow</label>
<input type="text" name="flow[][flow]" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[][bline_id]" />
<label>Flow</label>
<input type="text" name="flow[][flow]" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[][bline_id]" />
<label>Flow</label>
<input type="text" name="flow[][flow]" />
</p>
<input name="Submit" type="submit" />
</form>
//process.php
<?php
// connect to the database
include('connect-db.php');
$cnt = count($_POST['bline_id']);
$cnt2 = count($_POST['flow']);
if ($cnt > 0 && $cnt == $cnt2) {
$insertArr = array();
for ($i=0; $i<$cnt; $i++) {
$insertArr[] = "('" . mysql_real_escape_string($_POST['bline_id'][$i]) . "', '" . mysql_real_escape_string($_POST['flow'][$i]) . "')";
}
$query = "INSERT INTO bltest (bline_id, flow) VALUES " . implode(", ", $insertArr);
mysql_query($query) or trigger_error("Insert failed: " . mysql_error());
}
echo("<pre>\n");
print_r($_POST);
echo("</pre>\n");
mysql_close($connection);
?>
數組結果
Array
(
[bline_id] => Array
(
[0] => Array
(
[bline_id] => 1
)
[1] => Array
(
[bline_id] => 2
)
[2] => Array
(
[bline_id] => 3
)
[3] => Array
(
[bline_id] => 4
)
[4] => Array
(
[bline_id] => 5
)
)
[flow] => Array
(
[0] => Array
(
[flow] => 11
)
[1] => Array
(
[flow] => 22
)
[2] => Array
(
[flow] => 33
)
[3] => Array
(
[flow] => 44
)
[4] => Array
(
[flow] => 55
)
)
[Submit] => Submit Query
)
INSERT結果當然是5行,但沒有為$ bline_id或$ flow插入數據。 但是看一下陣列,那就是正確的數據。
相反,使用PDO或Mysqli,這些擴展具有prepare選項,因此您需要ony傳遞一次查詢,並使用while循環來更改數據!
<?php
// pdo example
$sql = 'INSERT INTO `table` (field1, field2, field3) VALUES (:value1, :value2, :value3)';
// $dbh is pdo connection
$insertTable = $dbh->prepare($sql);
$countArray = count($array);
$i = 0;
while ($i < $countArray) {
$insertTable->bindParam(':value1', $array[1][$i], PDO::PARAM_INT); // if value is int
$insertTable->bindParam(':value2', $array[2][$i], PDO::PARAM_STR); // if value is str
$insertTable->bindParam(':value3', $array[3][$i], PDO::PARAM_STR);
$insertTable->execute();
$i++;
}
?>
好的。 鑒於你告訴我的,這是我想到的解決方案。 我不打算給你代碼; 它打敗了你寫作的重點。
我希望用戶在兩個輸入文本字段“bline_id”和“flow”中鍵入一些值。
當他們點擊提交按鈕時,這些值將被添加到PHP代碼中的數組中。
然后將該數組序列化為一個字符串,並存儲為會話cookie 。
在輸入每個值時,您將對cookie進行反序列化 ,以便將其轉換為數組,將新值添加到數組中,然后重復。
當用戶點擊另一個按鈕“Store in Database”時。 此選項將反序列化cookie, 循環遍歷數組中的每個元素, 並將每個值存儲在數據庫中。
還要記住,你有兩個值,bline_id和flow。 這些可以存儲在兩個數組中並存儲為兩個cookie。 這意味着你正在為兩個不同的陣列而不是一個陣列執行此過程。
最后,您應該使用PDO對象。 我已經通過“商店”鏈接將您鏈接到了它。 這是推薦的方法。
我的天啊!!!! 我終於明白了! 這是我的愚蠢形式!
糾正下面-------
<form action="process.php" method="post">
<p><label>Beamline ID</label>
<input type="text" name="bline_id[]" value="<?php echo $bline_id; ?>" />
<label>Flow</label>
<input type="text" name="flow[]" value="<?php echo $flow; ?>" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[]" value="<?php echo $bline_id; ?>" />
<label>Flow</label>
<input type="text" name="flow[]" value="<?php echo $flow; ?>" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[]" value="<?php echo $bline_id; ?>"/>
<label>Flow</label>
<input type="text" name="flow[]" value="<?php echo $flow; ?>" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[]" value="<?php echo $bline_id; ?>" />
<label>Flow</label>
<input type="text" name="flow[]" value="<?php echo $flow; ?>" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[]" value="<?php echo $bline_id; ?>" />
<label>Flow</label>
<input type="text" name="flow[]" value="<?php echo $flow; ?>" />
</p>
<input name="Submit" type="submit" />
</form>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.