表单与数据库中患者信息添加失败问题求助
问题排查与修复方案
针对你提交的医疗系统代码,数据无法插入数据库的核心问题及修复方案如下:
1. 预处理语句参数绑定数量不匹配
你的SQL插入语句包含12个字段,但bind_param方法的类型字符串仅写了11个s,缺少对应phone_number的类型标识,直接导致参数绑定失败,无法执行插入操作。
修复代码:
将原绑定代码:
$stmt->bind_param("sssssssssss", $first_name, $last_name, $date_of_birth, $gender, $address, $category, $state, $medical_history, $date_of_admission, $diagnoses, $prescription, $phone_number);
修改为:
$stmt->bind_param("ssssssssssss", $first_name, $last_name, $date_of_birth, $gender, $address, $category, $state, $medical_history, $date_of_admission, $diagnoses, $prescription, $phone_number);
(类型字符串补充一个s,总数与字段数保持一致)
2. 缺失错误检测逻辑
当前代码未对预处理语句创建、参数绑定、执行过程做错误校验,无法定位具体失败原因。需添加错误检测以便排查问题:
添加错误检测代码:
$stmt = $con->prepare($sql); // 检查预处理是否成功 if (!$stmt) { die("预处理失败: " . $con->error); } // 检查参数绑定是否成功 if (!$stmt->bind_param("ssssssssssss", $first_name, $last_name, $date_of_birth, $gender, $address, $category, $state, $medical_history, $date_of_admission, $diagnoses, $prescription, $phone_number)) { die("参数绑定失败: " . $stmt->error); } // 检查执行是否成功 if (!$stmt->execute()) { die("执行失败: " . $stmt->error); }
3. 性别单选框兼容性问题
表单中性别单选框的required属性在部分浏览器中支持不完善,若用户未主动选择性别,$_POST['gender']会不存在,触发外层if条件不成立,直接跳过插入逻辑。
优化方案:
给其中一个单选框设置默认选中状态:
<input type="radio" name="gender" value="male" id="gender_male" checked> Male <input type="radio" name="gender" value="female" id="gender_female"> Female<br>
4. 重复Session校验与HTML结构错误
页面存在两段独立的PHP代码块,重复执行session_start()且登录校验逻辑不一致,易引发Session冲突。同时HTML结构中head标签未闭合,body直接嵌套在head内,可能影响页面正常解析。
修复代码结构:
合并PHP逻辑,修正HTML结构:
<?php session_start(); // 统一登录校验逻辑 if (!isset($_SESSION['UserLogin']) || !$_SESSION['UserLogin']) { header("Location: login.php"); exit; } include_once('connections.php'); $con = connection(); if ($_SERVER['REQUEST_METHOD'] === 'POST') { // 检查必填字段是否齐全 $required_fields = ['first_name', 'last_name', 'date_of_birth', 'gender', 'address', 'category', 'state', 'medical_history', 'date_of_admission', 'diagnoses', 'prescription', 'phone_number']; $missing_fields = []; foreach ($required_fields as $field) { if (!isset($_POST[$field]) || trim($_POST[$field]) === '') { $missing_fields[] = $field; } } if (empty($missing_fields)) { // 参数赋值 $first_name = mysqli_real_escape_string($con, $_POST['first_name']); $last_name = mysqli_real_escape_string($con, $_POST['last_name']); $date_of_birth = mysqli_real_escape_string($con, $_POST['date_of_birth']); $gender = mysqli_real_escape_string($con, $_POST['gender']); $address = mysqli_real_escape_string($con, $_POST['address']); $category = mysqli_real_escape_string($con, $_POST['category']); $state = mysqli_real_escape_string($con, $_POST['state']); $medical_history = mysqli_real_escape_string($con, $_POST['medical_history']); $date_of_admission = mysqli_real_escape_string($con, $_POST['date_of_admission']); $diagnoses = mysqli_real_escape_string($con, $_POST['diagnoses']); $prescription = mysqli_real_escape_string($con, $_POST['prescription']); $phone_number = mysqli_real_escape_string($con, $_POST['phone_number']); // 执行插入 $sql = "INSERT INTO patient_rec (First_name, Last_name, Date_of_birth, Gender, Address, Category, State, Medical_history, Date_of_admission, Diagnoses, Prescription, Phone_number) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; $stmt = $con->prepare($sql); if (!$stmt) { die("预处理失败: " . $con->error); } $stmt->bind_param("ssssssssssss", $first_name, $last_name, $date_of_birth, $gender, $address, $category, $state, $medical_history, $date_of_admission, $diagnoses, $prescription, $phone_number); if ($stmt->execute()) { mysqli_close($con); session_write_close(); header("Location: index.php"); exit; } else { die("插入失败: " . $stmt->error); } } else { die("缺少必填字段: " . implode(', ', $missing_fields)); } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=.75"> <link rel="stylesheet" type="text/css" href="addform.css"> </head> <body> </br> <h1>Add Patient Information</h1> <h2>Bayawan District hospital</h2> <h3>236 Zamora St, Bayawan City, 6221 Negros Oriental</h3> </br> <img src="logo.png" alt="Logo" class="logo"> <form action="add.php" method="POST"> <label for="first_name">First Name:</label><br> <input type="text" name="first_name" id="first_name" placeholder="Please type your first name" required><br> <label for="last_name">Last Name:</label><br> <input type="text" name="last_name" id="last_name" placeholder="Please type your last name" required><br> <label for="date_of_birth">Date of Birth:</label><br> <input type="date" name="date_of_birth" id="date_of_birth" required><br> <label for="gender">Gender:</label><br> <input type="radio" name="gender" value="male" id="gender_male" checked> Male <input type="radio" name="gender" value="female" id="gender_female"> Female<br> <label for="address">Address:</label><br> <input type="text" name="address" id="address" placeholder="Please type your address" required><br> <label for="phone_number">Phone Number:</label><br> <input type="text" name="phone_number" id="phone_number" pattern="[0-9]{11}" required><br> <label for="category">Category:</label><br> <select name="category" id="category" required> <option value="">-Select Choices-</option> <option value="Primary Care">Primary Care</option> <option value="Specialty Care">Specialty Care</option> <option value="Emergency Care">Emergency Care</option> <option value="Urgent Care">Urgent Care</option> <option value="Long-term Care">Long-term Care</option> <option value="Hospice Care">Hospice Care</option> <option value="Mental Healthcare">Mental Healthcare</option> </select><br> <label for="state">Status:</label><br> <select name="state" id="state" required> <option value="">-Select Choices-</option> <option value="New patient">New patient</option> <option value="Established patient">Established patient</option> <option value="Outpatient">Outpatient</option> <option value="Inpatient">Inpatient</option> </select><br> <label for="medical_history">Medical History:</label><br> <textarea name="medical_history" id="medical_history" placeholder="Please type here..." rows="4" cols="50" required></textarea><br> <label for="date_of_admission">Date of Admission:</label><br> <input type="date" name="date_of_admission" id="date_of_admission" required><br> <label for="diagnoses">Diagnoses:</label><br> <textarea name="diagnoses" id="diagnoses" placeholder="Please type here..." rows="4" cols="50" required></textarea><br> <label for="prescription">Prescription:</label><br> <textarea name="prescription" id="prescription" placeholder="Please type here..." rows="4" cols="50" required></textarea><br> <input type="submit" value="Add Patient"> <a href="index.php"><button type="button" class="logbtn">Cancel</button></a> </form> </body> </html>
5. 表单布局小问题
phone_number输入框后缺少<br>标签,可能导致后续元素布局错乱,已在上方修复代码中补充。
内容的提问来源于stack exchange,提问作者beginner coder
相关产品推荐
相关产品推荐

