Reputation: 3352
I have form
that inserts input data and file-locations into a MySQL database after submit. The script saves the data and the files successfully except it saves everything twice. I know it has something to do with having a foreach
statement within a for
statement, but I don't know how to re-arrange or re-write my code to have it accomplish what I want to.
I want it to do what it's doing right now, inserting the data into the database, but I want it to insert each value once.
<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 Script
I am aware that mysql_*
are depreciated functions and should be replaced with MySQLi
or PDO
methods
<?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();
}
}
}
}
?>
How can I fix my code so that it only inserts the values into the database once?
Upvotes: 0
Views: 1063
Reputation: 5897
A couple comments have the right idea. You're looping through the number of files which can be up to the same length as the number of records, so you're entering in the query for a single record for every file up tot he number of records so you could at a max end up always putting in x^2 number of records.
Try dumping that inside loop, and using the original counter as the place mark since it's only one file per record... Your next challenge is going to be making sure that the if a record in the middle doesn't have a file but ones after it do, that that file gets attached to the correct record. This is to whet your pallet though:
$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();
}
Upvotes: 1