我有一个将用户输入数据保存到数据库的表单。在这种形式我有一个上传功能。表单按原样执行,它将数据保存到数据库以及图像。问题是,假设我有三个图像,现在发生了什么,它是否为三个图像中的每一个在DB中创建了一个新行。
例如,我填写表格一次,但上传三张图片。现在发生的事情是,我完成表单的数据在数据库中创建了三次,每个数据都有自己的ID来计算上传的三个图像。
这是表格:
<form method="post" action="submit_vehicle.php" enctype="multipart/form-data">
<input name="submit" type="submit" class="button" value="Save">
<a role="button" href="#" class="button success">Save & New</a>
<a role="button" href="manage_vehicles.php" class="button secondary">Cancel</a>
<div class="row">
<div class="large-6 columns">
<label>Stock Number
<input name="stock_number" type="text" placeholder="Stock Number" />
</label>
</div>
<div class="large-6 columns">
<label>VIN Number
<input name="vin_number" type="text" placeholder="VIN Number" />
</label>
</div>
</div>
<div class="row">
<div class="large-6 columns">
<label>Make
<input name="make" type="text" placeholder="Make" required/>
</label>
</div>
<div class="large-6 columns">
<label>Model
<input name="model" type="text" placeholder="Model" required/>
</label>
</div>
</div>
<div class="row">
<div class="large-6 columns">
<label>Colour
<input name="colour" type="text" placeholder="Colour" required/>
</label>
</div>
<div class="large-6 columns">
<label>Milage
<input name="milage" type="text" placeholder="Milage" required/>
</label>
</div>
</div>
<div class="row">
<div class="large-6 columns">
<label>Year
<input name="year" type="text" placeholder="Year" required/>
</label>
</div>
<div class="large-6 columns">
<label>Price
<input name="price" type="text" placeholder="Price" required/>
</label>
</div>
</div>
<div class="row">
<div class="large-12 columns">
<label>Vehicle Description
<textarea name="description" placeholder="Describe this vehicle"></textarea>
</label>
</div>
</div>
<div class="row">
<div class="large-12 columns">
<label>Choose images for this vehicle (Note: Supported file types are JPEG, JPG and PNG)
<input type="file" name="files[]" multiple="">
</label>
</div>
</div>
<h4 class="border"></h4>
<input type="submit" class="button" name="submit" value="Save">
<a role="button" href="#" class="button success">Save & New</a>
<a role="button" href="manage_vehicles.php" class="button secondary">Cancel</a>
</form>
这是PHP:
if (isset($_POST['submit'])) {
//Get form data
$stock_number = $_POST['stock_number'];
$vin_number = $_POST['vin_number'];
$make = $_POST['make'];
$model = $_POST['model'];
$colour = $_POST['colour'];
$milage = $_POST['milage'];
$year = $_POST['year'];
$price = $_POST['price'];
$description = $_POST['description'];
$errors = array();
if (isset($_FILES['files'])) {
foreach ($_FILES['files']['tmp_name'] as $key => $tmp_name) {
$file_name = $key . $_FILES['files']['name'][$key];
$file_size = $_FILES['files']['size'][$key];
$file_tmp = $_FILES['files']['tmp_name'][$key];
$file_type = $_FILES['files']['type'][$key];
if ($file_size > 2097152) {
$errors[] = 'Files must be smaller that 2MB';
}
$directory = 'uploaded_images/' . $file_name;
$sql = "
INSERT INTO vehicles
(stock_number, vin_number, make, model, colour, milage, year, price, description, file_name, file_size, file_type, file_path)
VALUES
('$stock_number', '$vin_number', '$make', '$model', '$colour', '$milage', '$year', '$price', '$description', '$file_name', '$file_size', '$file_type', '$directory')
";
mysqli_query($conn, $sql);
}
if (empty($errors)) {
header('Location: manage_vehicles.php');
exit();
}
}
}