过去几周我一直在使用php,mysql和javascript开发预约系统。我在尝试将数据插入表格时遇到了问题。我在php页面上使用javascript将一个GET请求发送到一个php文件,该文件从数据库记录生成一个表。我添加了一个预约约会'每行末尾的按钮,如果可用,应该让用户确认他们的约会并预订。我编写了所有代码以从表中获取所有相关数据,然后插入到新表中。问题是它没有插入。如果我在php中这样做我可以这样做我现在如何做它,但它不会是动态的。我没有得到任何错误,它只是不插入数据。我需要在javascript中执行ajax请求吗?我知道有很多可用的在线帮助解决这些问题,但似乎无法找到我特定问题的答案。
继承我的代码:
<body>
<div id="textDisplay"<h1>Doctor availability and appointments will be listed here...</h1></div>
</body>
<script>
function showAppointments(str, patient_id, patient_surname) {
if (str == "" || patient_id == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("textDisplay").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","getAppointments.php?listOption="+str + "&patient_id="+patient_id + "&surname="+patient_surname, true);
xmlhttp.send();
}
}
}
这个javascript函数向php页面发送一个get请求,其中一些数据附加在url中,因此可以通过php页面获取它。
getAppointments.php然后生成包含相关数据库记录的表。
***** getAppointments.php:*****
include 'dbConnect.php';
$listOptionVal = intval($_GET['listOption']);
$patient_id = $_GET['patient_id'];
$surname = $_GET['surname'];
if ($listOptionVal == 0) {
$sql="SELECT * FROM patient_info, appointment_slots INNER JOIN doctors ON appointment_slots.doctor_id=doctors.doctor_id WHERE
appointment_time BETWEEN '09:00:00' AND '12:00:00' AND appointment_date = '2015-11-02' AND patient_id = $patient_id;";
$result = $conn->query($sql);
echo "<center><h2>Available Appointments</h2><center>";
echo "<center><table>
<tr>
<th>Patient ID</th>
<th>Patient Surname</th>
<th>Date</th>
<th>Times</th>
<th>Avaialable</th>
<th>Doctor</th>
<th>Book Appointment</th>
</tr>";
while($row = $result->fetch_assoc()) {
echo '<input type="hidden" name="patient_id" value="' . $row['patient_id'] . '" />';
echo '<input type="hidden" name="surname" value="' . $row['surname'] . '" />';
echo '<input type="hidden" name="appointment_date" value="' . $row['appointment_date'] . '" />';
echo '<input type="hidden" name="appointment_time" value="' . $row['appointment_time'] . '" />';
echo '<input type="hidden" name="appointment_avaliability" value="' . $row['appointment_avaliability'] . '" />';
echo "<form action='confirmed-appointments.php' method='post' enctype='multipart/form-data'>";
echo "<tr>";
echo "<td>" . $row['patient_id'] . "</td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td>" . $row['appointment_date'] . "</td>";
echo "<td>" . $row['appointment_time'] . "</td>";
echo "<td>" . $row['appointment_avaliability'] . "</td>";
if ($row['appointment_avaliability'] == 'Y') {
echo "<td style='color: #ACFB62;'>".$row['doctor_forename']." </td>";
} else if ($row['appointment_avaliability'] == 'N') {
echo "<td style='color: #FF6666;'>".$row['doctor_forename']." </td>";
}
echo "<td>
<input type='submit' name='bookAppointment' value='Book Appointment'>
</td>";
echo "</tr>";
}
}
echo "</table></center>";
echo "</form>";
然后,这将生成一个包含约会可用时间的表。每行附加一个按钮,允许用户预约该约会。这是我想将数据发送到数据库然后遍历结果并在新页面上显示的地方。
****确认了appointmentments.php - 应循环插入数据行****
<?php
include 'dbConnect.php';
if (isset($_POST['bookAppointment'])) {
var_dump($_POST);
$patient_id = $_POST['patient_id'];
$surname = $_POST['surname'];
$forename = $_POST['appointment_date'];
$dob = $_POST['appointment_time'];
$doctor = $_POST['doctor_forename'];
$sql = "INSERT INTO confirmed_appointments SET patient_id='" . $patient_id . "', surname='" . $surname . "', appointment_date='" . $appointment_date . "', appointment_time='" . $appointment_time . "', doctor='" . $doctor . "'";
if ($conn->query($sql) === TRUE) {
echo "Record added successfully";
} else {
echo "Error: " . $sql . "
" . $conn->error;
}
}
$sql = "SELECT * from confirmed_appointments;";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
echo "<table border = 1>";
echo "<tr>";
echo "<td>" . $row['patient_id'] . "</td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td>" . $row['appointment_date'] . "</td>";
echo "<td>" . $row['appointment_time'] . "</td>";
echo "<td>" . $row['doctor'] . "</td>";
echo "</tr>";
echo "</table>";
}
$conn->close();
?>
数据未插入,因此不会显示在confirmed-appointmentments.php页面上。我希望我已经足够清楚,我希望我的问题不会被关闭!谢谢你的时间
答案 0 :(得分:1)
假设所有的帖子数据都是正确的,数据库连接成功,并且你的任何变量都没有输入错误...有时只是没有关闭双引号就行了......
$sql = "INSERT INTO confirmed_appointments SET patient_id='" . $patient_id . "', surname='" . $surname . "', appointment_date='" . $appointment_date . "', appointment_time='" . $appointment_time . "', doctor='" . $doctor . "'";
相反,您是否尝试过var_dump $ _POST?我不认为你的表格中有任何价值。
while($row = $result->fetch_assoc()) {
echo "<form method='get'>";
echo "<tr>";
echo "<td>" . $row['patient_id'] . "</td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td>" . $row['appointment_date'] . "</td>";
echo "<td>" . $row['appointment_time'] . "</td>";
echo "<td>" . $row['appointment_avaliability'] . "</td>";
if ($row['appointment_avaliability'] == 'Y') {
echo "<td style='color: #ACFB62;'>".$row['doctor_forename']." </td>";
} else if ($row['appointment_avaliability'] == 'N') {
echo "<td style='color: #FF6666;'>".$row['doctor_forename']." </td>";
}
echo "<td>
<input type='submit' name='bookAppointment' value='Book Appointment' action='confirmed-appointments.php'>
</td>";
echo "</tr>";
echo "</form>";
}
}
您只是显示值并且表单方法已获取,您是否有任何输入值?
我在
看到你的变量$patient_id = $_POST['patient_id'];
$surname = $_POST['surname'];
$forename = $_POST['appointment_date'];
$dob = $_POST['appointment_time'];
$doctor = $_POST['doctor_forename'];
但你的
在哪里input type="text" name="patient_id" value=" . $row['patient_id']
在你的getAppointments.php?
getAppointments.php
include 'dbConnect.php';
$listOptionVal = intval($_GET['listOption']);
$patient_id = $_GET['patient_id'];
$surname = $_GET['surname'];
if ($listOptionVal == 0) {
$sql="SELECT * FROM patient_info, appointment_slots INNER JOIN doctors ON appointment_slots.doctor_id=doctors.doctor_id WHERE
appointment_time BETWEEN '09:00:00' AND '12:00:00' AND appointment_date = '2015-11-02' AND patient_id = $patient_id;";
$result = $conn->query($sql);
echo "<center><h2>Available Appointments</h2><center>";
echo "<form method='post' action = 'confirmed-appointments.php'>";
echo "<center><table>
<tr>
<th>Patient ID</th>
<th>Patient Surname</th>
<th>Date</th>
<th>Times</th>
<th>Avaialable</th>
<th>Doctor</th>
<th>Book Appointment</th>
</tr>";
while($row = $result->fetch_assoc()) {
// things that you might miss out
echo '<input type="hidden" name="patient_id" value="' . $row['patient_id'] . '" />';
echo '<input type="hidden" name="surname" value="' . $row['surname'] . '" />';
echo '<input type="hidden" name="appointment_date" value="' . $row['appointment_date'] . '" />';
echo '<input type="hidden" name="appointment_time" value="' . $row['appointment_time'] . '" />';
echo '<input type="hidden" name="appointment_avaliability" value="' . $row['appointment_avaliability'] . '" />';
echo '<input type="hidden" name="doctor_forename" value="' . $row['doctor_forename'] . '" />';
echo "<tr>";
echo "<td>" . $row['patient_id'] . "</td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td>" . $row['appointment_date'] . "</td>";
echo "<td>" . $row['appointment_time'] . "</td>";
echo "<td>" . $row['appointment_avaliability'] . "</td>";
if ($row['appointment_avaliability'] == 'Y') {
echo "<td style='color: #ACFB62;'>".$row['doctor_forename']." </td>";
} else if ($row['appointment_avaliability'] == 'N') {
echo "<td style='color: #FF6666;'>".$row['doctor_forename']." </td>";
}
echo "<td>
<input type='submit' name='bookAppointment' value='1'>
</td>";
echo "</tr>";
}
}
echo "</table></center>";
echo "</form>";
确认约会.php
if (isset($_POST['bookAppointment'])) {
$patient_id = $_POST['patient_id'];
$surname = $_POST['surname'];
$forename = $_POST['appointment_date'];
$dob = $_POST['appointment_time'];
$doctor = $_POST['doctor_forename'];
$sql = "INSERT INTO confirmed_appointments SET patient_id='" . $patient_id . "', surname='" . $surname . "', appointment_date='" . $appointment_date . "', appointment_time='" . $appointment_time . "', doctor='" . $doctor . "'";
if ($conn->query($sql) === TRUE) {
echo "Record added successfully";
} else {
echo "Error: " . $sql . "
" . $conn->error;
}
}