我在编辑数据并将其存储在数据库中时遇到问题。我有一个表单,从Patients_details表加载数据,并显示在页面上的html表中。我已经这样做,因此数据库的每个字段都有文本框,因此用户可以选择更改数据,然后单击按钮将数据发送到数据库,然后重新显示页面。我的问题在于,当我单击按钮时,记录不会保存到数据库中,因此不会显示在表中。我花了几个小时研究这个,似乎无法理解为什么它不会工作。我设法将信息添加到单独的表中,然后将数据显示在页面上,但在编辑时它的不同。我认为这可能是我的数据库的一个问题,但我不太确定。
继承我的代码:
****updateUsers.php*****
<html>
<head>
<title>Current Patients</title>
<head>
<meta charset='utf-8' />
<link href='../fullcalendar.css' rel='stylesheet' />
<link href='../fullcalendar.print.css' rel='stylesheet' media='print' />
<script src='../lib/moment.min.js'></script>
<script src='../lib/jquery.min.js'></script>
<script src='../lib/jquery-ui.custom.min.js'></script>
<script src='../fullcalendar.min.js'></script>
</head>
<body>
<?php
$con = mysql_connect('localhost', 'root', 'password');
if (!$con) {
die("Cannot connect" . mysql_error());
}
mysql_select_db('DoctorScheduler');
if (isset($_POST['submit']) && $_POST['submit'] == 'updatePatients') {
$updatePatientsDetailsQuery = "UPDATE patients_details SET patient_id='$_POST[patient_id]', patient_surname='$_POST[patient_surname]', patient_forename='$_POST[patient_forename]', patient_dob='$_POST[patient_dob]', patient_doctor='$_POST[patient_doctor]', phone_num='$_POST[patient_number]', patient_email='$_POST[patient_email]', patient_address='$_POST[patient_address]' WHERE patient_id='$_POST[hidden] ' ";
$patientRecords =mysql_query($updatePatientsDetailsQuery);
}
$sql = "SELECT * FROM patients_details";
$records=mysql_query($sql);
// <a href="form1.php"> Request an Appointment </a>
echo "<table border=1>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Surname</th>";
echo "<th>Forename</th>";
echo "<th>Date of Birth</th>";
echo "<th>Doctor</th>";
echo "<th>Phone Number</th>";
echo "<th>Email</th>";
echo "<th>Address</th>";
echo "</tr>";
while($currentPatients = mysql_fetch_assoc($records) ) {
echo "<form action=updateUsers.php method=post>";
echo "<tr>";
echo "<td>" . "<input type=hidden name=patient_id value=" . $currentPatients['patient_id'] . " </td>";
echo "<td>" . "<input type=text name=patient_surname value=" . $currentPatients['patient_surname']." </td>";
echo "<td>" . "<input type=text name=patient_forename value=" . $currentPatients['patient_forename'] . " </td>";
echo "<td>". "<input type=text name=patient_dob value=" . $currentPatients['patient_dob'] . " </td>";
echo "<td>". "<input type=text name=patient_doctor value=" . $currentPatients['patient_doctor'] . " </td>";
echo "<td>". "<input type=text name=patient_num value=" . $currentPatients['patient_num'] . " </td>";
echo "<td>". "<input type=text name=patient_email value=" . $currentPatients['patient_email'] . " </td>";
echo "<td>". "<input type=text name=patient_address value=" . $currentPatients['patient_address'] . " </td>";
echo "<td>" . "<input type='submit' name='updatePatients' value='Update Patients' onclick='updateDatabase()'" . " </td>";
echo "</tr>";
echo "</form>";
} // end of while
echo "</table>";
?>
<script>
function updateDatabase() {
var xmlhttp;
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET", "updateData.php?patient_surname=" + document.getElementById("patient_surname").value +
"&patient_forename="+document.getElementById("patient_forename").value + "&patient_dob="+document.getElementById("patient_dob").value +
"&patient_doctor="+document.getElementById("patient_doctor").value + "&patient_num="+document.getElementById("patient_num").value +
"&patient_email="+document.getElementById("patient_email").value + "&patient_address="+document.getElementById("patient_address").value,false);
xmlhttp.send(null);
}
</script>
</body>
****updataData.php******
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/html4/loose.dtd">
<html>
<head>
<title> updateData </title>
<meta http-equiv="Content Type" content="text/html"; charset="UTF-8">
</head>
<body>
<?php
$patient_surname=$_GET['patient_surname'];
$patient_forename=$_GET['patient_forename'];
$patient_dob=$_GET['patient_dob'];
$patient_doctor=$_GET['patient_doctor'];
$patient_num=$_GET['patient_num'];
[enter image description here][1]$patient_email=$_GET['patient_email'];
$patient_address=$_GET['patient_address'];
mysql_connect("localhost", "root", "password");
mysql_select_db("DoctorScheduler");
mysql_query("insert into patients_details values('$patient_surname', '$patient_forename', '$patient_dob', '$patient_doctor', '$patient_num', '$patient_email', '$patient_address')");
?>
</body>
</html>
答案 0 :(得分:0)
所有这些表达方式如:
... "UPDATE patients_details SET patient_id='$_POST[patient_id]' ....
会导致n&#34;未知变量patient_id&#34;和类似的错误。它应该是:
... "UPDATE patients_details SET patient_id='" . $_POST['patient_id'] . "' ....
只要$_POST[patient_id]
未被定义为常量(我不假设),表达式patient_id
就是错误的。
必须是$_POST['patient_id']
(请注意引号)并且因为您在转义引号时丢失,否则请使用字符串连接符.
;
修改强>
如果发出类似insert into patients_details values('?
的SQL语句而没有给出字段名,则必须为该表中的所有列提供值,并且这些值是按这些列的定义顺序给出的。否则请使用以下语法:
insert into patients_details (field1, field2) values ('one', 'two');