编辑mysql数据库表中已有的数据


Edit pre-existing data in a mysql database table

我在编辑数据和将其存储在数据库中有问题。我有一个表单,它从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>

所有这些表达式,如:

 ... "UPDATE patients_details SET patient_id='$_POST[patient_id]' ....

会导致"未知变量patient_id"等错误。应该是:

 ... "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');