我有form
在提交后将输入数据和文件位置插入MySQL数据库。该脚本成功保存数据和文件,除了它保存所有内容两次。我知道它与foreach
语句中的for
语句有关,但我不知道如何重新安排或重写我的代码以使其完成我想要的。
我希望它能够立即执行它正在执行的操作,将数据插入数据库,但我希望它能够插入每个值一次。
<label>Length</label><input type="text" name="length[]" value="<?php echo $sav['length']; ?>" size="2">
<label>Width</label><input type="text" name="width[]" value="<?php echo $sav['width']; ?>" size="2">
<label>Color</label><input type="text" name="color[]" value="<?php echo $sav['color']; ?>" size="4">
<label>Quantity</label><input type="text" name="quantity[]" value="<?php echo $sav['quantity']; ?>" size="2">
<label>Invoice Is Hidden</label><input type="hidden" name="invoice[]" value="<?php echo $invoice_no; ?>">
<input type="hidden" name="price[]" value="<?php echo $sav['price']; ?>" />
<input type="hidden" name="PaymentStatus[]" value="PAID">
<input type="hidden" name="MAX_FILE_SIZE[]" value="2000000" />
<input type="file" name="newImage[]">
PHP脚本
我知道mysql_*
是折旧函数,应替换为MySQLi
或PDO
方法
<?php
require("addrow_info.php");
for($i=0;$i<count($_POST['length']);$i++) {
$newOrder = array(
'length' => $_POST['length'][$i],
'width' => $_POST['width'][$i],
'color' => $_POST['color'][$i],
'quantity' => $_POST['quantity'][$i],
'price' => $_POST['price'][$i],
'invoice' => $_POST['invoice'][$i],
'paymentStatus' => $_POST['PaymentStatus'][$i],
);
// Opens a connection to a MySQL server
$connection=mysql_connect ("localhost", $username, $password);
if (!$connection) {
die('Not connected : ' . mysql_error());
}
// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
foreach ($_FILES['newImage']['error'] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES['newImage']['tmp_name'][$key];
$imageName = $_FILES['newImage']['name'][$key];
move_uploaded_file($tmp_name, "decalImages/newOrders/$imageName");
$uploadDir = 'decalImages/newOrders/';
// Insert new row with user data
$query = "INSERT INTO orders (PaymentStatus, invoice_no, length, width, color, quantity, price, imagePath, orderStatus ) VALUES ('".$newOrder['paymentStatus']."','".$newOrder['invoice']."','".$newOrder['length']."', '".$newOrder['width']."', '".$newOrder['color']."', '".$newOrder['quantity']."', '".$newOrder['price']."', '".$uploadDir."".$imageName."', 'PENDING')";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
echo "$query";
mysql_close();
}
}
}
}
?>
如何修复我的代码,以便只将值插入数据库一次?
答案 0 :(得分:1)
一些评论有正确的想法。你循环遍历的文件数量可以达到与记录数量相同的长度,因此你要在每个文件的单个记录的查询中输入记录数量,这样你就可以达到最大值。最终总是输入x ^ 2个记录。
尝试转储内部循环,并使用原始计数器作为地点标记,因为每个记录只有一个文件...您的下一个挑战是确保中间的记录是否没有文件,但它后面的文件,该文件附加到正确的记录。这是为了你的托盘:
$tmp_name = $_FILES['newImage']['tmp_name'][$i];
$imageName = $_FILES['newImage']['name'][$i];
move_uploaded_file($tmp_name, "decalImages/newOrders/$imageName");
$uploadDir = 'decalImages/newOrders/';
// Insert new row with user data
$query = "INSERT INTO orders (PaymentStatus, invoice_no, length, width, color, quantity, price, imagePath, orderStatus ) VALUES ('".$newOrder['paymentStatus']."','".$newOrder['invoice']."','".$newOrder['length']."', '".$newOrder['width']."', '".$newOrder['color']."', '".$newOrder['quantity']."', '".$newOrder['price']."', '".$uploadDir."".$imageName."', 'PENDING')";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
echo "$query";
mysql_close();
}