编辑 mysql 数据库 table 中预先存在的数据

Edit pre-existing data in a mysql database table

我在编辑数据并将其存储在数据库中时遇到问题。我有一个从 patients_details table 加载数据并显示在页面上的 html table 中的表单。我已经做到了,所以数据库的每个字段都有文本框,因此用户可以选择更改数据,然后单击一个按钮将数据发送到数据库,然后重新显示页面。我的问题在于当我点击按钮时,记录没有保存到数据库中,因此不显示在 table 中。我花了几个小时研究这个,但似乎无法理解为什么它不起作用。我设法将信息添加到单独的 table,然后将数据显示到页面上,但在编辑时有所不同。我认为这可能是我的数据库有问题,但我不太确定。

这是我的代码:

****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]' ....

会导致 n "unknown variable 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 语句,但没有给出字段名称,则 table 中的所有列都必须有一个值,并且这些值是以这些列的定义顺序给出。否则使用此语法:

      insert into patients_details (field1, field2) values ('one', 'two');