代码拉取完成,页面将自动刷新
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "cyberpunk";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
// 检查是否为POST请求
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
echo '<script>alert("没有检测到数据");window.history.back();</script>';
exit();
}
// 定义要插入的表及其对应的字段名和POST参数名
$tablesData = [
['table' => 'category_id', 'field' => 'category', 'param' => 'category'],
['table' => 'vehicle_brands', 'field' => 'name', 'param' => 'brand'],
['table' => 'vehicle_seats', 'field' => 'seats', 'param' => 'seats'],
];
$inserted = 0; // 记录成功插入的条数
foreach ($tablesData as $data) {
$value = isset($_POST[$data['param']]) && $_POST[$data['param']] !== '' ? $_POST[$data['param']] : null;
if ($value !== null) {
$sql = "INSERT INTO {$data['table']} ({$data['field']}) VALUES (?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $value);
if ($stmt->execute()) {
$inserted++;
} else {
echo "Error inserting data into {$data['table']}: " . $conn->error;
}
$stmt->close();
}
}
if ($inserted > 0) {
echo "<script>alert('成功插入{$inserted}条数据!'); window.location.href='vehicles.php';</script>";
} else {
echo "<script>alert('请至少填写一项车辆信息!'); window.history.back();</script>";
}
// 获取表单提交的数据
$category_name = isset($_POST['category']) && $_POST['category'] !== '' ? $_POST['category'] : null;
$brand_name = isset($_POST['brand']) && $_POST['brand'] !== '' ? $_POST['brand'] : null;
$seats_number = isset($_POST['seats']) && $_POST['seats'] !== '' ? $_POST['seats'] : null;
// 如果所有数据都非空,则插入新的类别、品牌和座位数
if ($category_name !== null && $brand_name !== null && $seats_number !== null) {
// 插入操作...
} else {
echo "<script>alert('请填写完整的车辆信息!'); window.history.back();</script>";
exit();
}
// 获取表单提交的数据
$category_name = $_POST['category'];
$brand_name = $_POST['brand'];
$seats_number = $_POST['seats'];
// 检查数据库中是否存在相同名称的类别
$check_category_sql = "SELECT * FROM `category_id` WHERE `category` = ?";
$stmt_category = $conn->prepare($check_category_sql);
$stmt_category->bind_param("s", $category_name);
$stmt_category->execute();
$category_result = $stmt_category->get_result();
if ($category_result->num_rows > 0) {
echo "<script>alert('此类别已存在!'); window.history.back();</script>";
} else {
// 检查数据库中是否存在相同名称的车辆品牌
$check_brand_sql = "SELECT * FROM `vehicle_brands` WHERE `name` = ?";
$stmt_brand = $conn->prepare($check_brand_sql);
$stmt_brand->bind_param("s", $brand_name);
$stmt_brand->execute();
$brand_result = $stmt_brand->get_result();
if ($brand_result->num_rows > 0) {
echo "<script>alert('此车辆品牌已存在!'); window.history.back();</script>";
} else {
// 检查数据库中是否存在相同的车辆座位数
$check_seats_sql = "SELECT * FROM `vehicle_seats` WHERE `seats` = ?";
$stmt_seats = $conn->prepare($check_seats_sql);
$stmt_seats->bind_param("s", $seats_number);
$stmt_seats->execute();
$seats_result = $stmt_seats->get_result();
if ($seats_result->num_rows > 0) {
echo "<script>alert('此车辆座位数已存在!'); window.history.back();</script>";
} else {
// 如果所有数据都不存在,则插入新的类别、品牌和座位数
$insert_category_sql = "INSERT INTO `category_id` (`category`) VALUES (?)";
$stmt_category_insert = $conn->prepare($insert_category_sql);
$stmt_category_insert->bind_param("s", $category_name);
$insert_brand_sql = "INSERT INTO `vehicle_brands` (`name`) VALUES (?)";
$stmt_brand_insert = $conn->prepare($insert_brand_sql);
$stmt_brand_insert->bind_param("s", $brand_name);
$insert_seats_sql = "INSERT INTO `vehicle_seats` (`seats`) VALUES (?)";
$stmt_seats_insert = $conn->prepare($insert_seats_sql);
$stmt_seats_insert->bind_param("s", $seats_number);
if ($stmt_category_insert->execute() && $stmt_brand_insert->execute() && $stmt_seats_insert->execute()) {
echo "<script>alert('插入车辆类别、品牌和座位数成功!'); window.location.href='vehicles.php';</script>";
} else {
echo "Error inserting data: " . $conn->error;
}
}
}
}
$stmt_category->close();
$stmt_brand->close();
$stmt_seats->close();
$conn->close();
?>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。