<?php
// 连接数据库
$conn = mysqli_connect("localhost", "tms", "123456", "nut");
// 检查连接是否成功
if (!$conn) {
die("数据库连接失败: " . mysqli_connect_error());
}echo "111";
// 处理表单提交
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// 获取表单数据
$aoiStep = $_POST['aoi_step'];
$defectType = $_POST['defect_type'];
$layerCode = $_POST['layer_code'];
$type = $_POST['type'];
$dpet = $_POST['dpet'];
$subcode = $_POST['subcode'];
$codeDescription = $_POST['code_description'];
$determinationRule = $_POST['determination_rule'];
$imagePaths = []; // 存储图片路径的数组
// 处理上传的图片
for ($i = 1; $i <= 5; $i++) {
$imageField = "image" . $i;
$targetDir = "D:/phpstudy_pro/WWW/192.168.1.16/images"; // 设置上传目录的路径
$fileName = uniqid() . '_' . $_FILES[$imageField]["name"]; // 生成唯一文件名
$targetFile = $targetDir . '/' . basename($fileName);
// 将反斜杠替换为正斜杠
$targetFile = str_replace('\\', '/', $targetFile);
if (isset($_FILES[$imageField]) && $_FILES[$imageField]["error"] == UPLOAD_ERR_OK && move_uploaded_file($_FILES[$imageField]["tmp_name"], $targetFile)) {
$imagePath = $targetFile;
} else {
$imagePath = "";
}
$imagePaths[] = $imagePath;
}
// 其他图片的处理代码,类似上面的处理方式
$stmt = $conn->prepare("INSERT INTO tms (aoi_step, defect_type, layer_code, type, dpet, subcode, code_description, image1_path, image2_path, image3_path, image4_path, image5_path, determination_rule) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
if (!$stmt) {
die("预处理失败: " . $conn->error);
}
$stmt->bind_param("sssssssssssss", $aoiStep, $defectType, $layerCode, $type, $dpet, $subcode, $codeDescription, $imagePaths[0], $imagePaths[1], $imagePaths[2], $imagePaths[3], $imagePaths[4], $determinationRule);
if ($stmt->execute()) {
echo "数据插入成功";
} else {
echo "数据插入失败: " . $stmt->error;
}
// 关闭数据库连接
mysqli_close($conn);
}
?>在这个代码中,当我有多行数据时,只上传最后一条,我需要怎么修改,才能将数据都进行上传