如何单独更新每一行?

时间:2015-11-10 15:17:59

标签: php html mysql sql forms

因此,我已经尝试了几天来弄清楚如何分别更新每一行,我无法弄明白。这是我第一次使用php,对于丑陋的代码感到遗憾。

我似乎可以从每一行提取信息,但更新不会起作用,但它会从地址栏中的所有行发送所有数据

<html>
<head>
    <title>EDIT Patient data</title>
</head>
<body> 
    <br>
    <a href="Patients.php">Edit Patients</a><br>
    <a href="PatientRequest.php">Patient Request</a><br>
    <a href="PatientConfirmed.php">Booked Patients</a>
    <?php
        include 'connect.php';
        $sql = "SELECT * FROM patients;";
        $result = mysql_query($sql);
        $num_of_row = mysql_num_rows($result);

        if (isset($_GET['submit'])) {
            $id = $_GET['did'];
            $fname = $_GET['dfname'];
            $lname = $_GET['dlname'];
            $email = $_GET['demail'];
            $phone = $_GET['dphone'];
            $address = $_GET['daddress'];
            $query = mysql_query("update patients set fName='$fname', lName='$lname', email='email', phone='$phone', address='$address' where patientID ='$id'");
        }

        echo "<table border=1>";
        echo " <tr> <td> Patient ID </td>  ";
        echo "<td> First name </td>";
        echo "<td> last name </td>";
        echo "<td> email </td>";
        echo "<td> phone number</td>";
        echo "<td> address </td>";
        echo "<td> date of birth </td> ";
        echo "<td> update </td></tr>";

        while($row = mysql_fetch_assoc($result))
        {
            echo  "<tr> <form class='form' method='get'>";
            echo  "<td> <input type='text' name='did' value='".$row['patientID']."' readonly /></td>";
            echo  "<td> <input type='text' name='dfname' value ='".$row['fName']."' /></td>";
            echo  "<td> <input type='text' name='dlname' value = '".$row['lName']."' /></td>";
            echo  "<td> <input type='text' name='demail' value = '".$row['email']."' /></td>";
            echo  "<td> <input type='text' name='dphone' value ='".$row['phone']."' /></td>";
            echo  "<td> <input type='text' name='daddress' value ='".$row['address']."' /></td>";
            echo  "<td> <input type='text' name='ddob'  value ='".$row['dob']."' readonly /></td>"; 
            echo  "<td>  <input class='submit' type='submit' name='submit' value='update' /> </td> </tr>";
        }    

    ?>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

您的用户是否拥有更新权限?

如果不是,则需要使用GRANT UPDATE ON *.* TO 'User'@'Host';

但是你应该使用MySQLi,因为MySQL已被弃用。您还应该查看参数化查询和MySQL注入,值得了解!

答案 1 :(得分:0)

如果您想要更新单个行,则必须在while循环周围加<form>,如下所示:

echo "<form action='' method='GET'>";
while ($row = mysql_fetch_assoc($result)) {
      echo  "<tr> <form class='form' method='get'>";
      echo  "<td> <input type='text' name='did' value='".$row['patientID']."' readonly /></td>";
      echo  "<td> <input type='text' name='dfname' value ='".$row['fName']."' /></td>";
      echo  "<td> <input type='text' name='dlname' value = '".$row['lName']."' /></td>";
      echo  "<td> <input type='text' name='demail' value = '".$row['email']."' /></td>";
      echo  "<td> <input type='text' name='dphone' value = '".$row['phone']."' /></td>";
      echo  "<td> <input type='text' name='daddress' value ='".$row['address']."' /></td>";
      echo  "<td> <input type='text' name='ddob'  value ='".$row['dob']."' readonly /></td>"; 
      echo  "<td>  <input class='submit' type='submit' name='submit' value='update' /> </td> </tr>";
}
echo "</form>";

表单将允许根据表单的方法(GET和POST)提交输入字段。