无法使用php表单更新数据库

时间:2014-04-18 11:48:22

标签: php phpmyadmin

我搜索了几个类似的问题,但无论我怎么做,我都无法更新记录。它一直显示Notice: undefined variable。 这是我的代码:

Doctor.php

<html>
<body>
<table align=center width=750 cellspacing=0 cellpadding=5>
<tr>
  <td><a href=dadd_doctor.php> Add New Record</a></td></tr>
<tr><td>
<table width=750 cellspacing=0 cellpadding=5>
<tr bgcolor=#ccccc><td align=center>Doctor ID</td><td align=center>Doctor
Name</td><td align=center>Specialization</td><td align=center>Option</td></tr>


<?php
$counter=1;
mysql_connect("localhost","root","");
mysql_select_db("hospital");
$result=mysql_query("SELECT * from doctor");

while( $row=mysql_fetch_array($result)) {
echo "<tr>
<td align=center>$counter</td
><td align=center>$row[1]</td>
<td align=center>$row[2]</td>
<td><a href='dmod_doctor.php?edit=$row[0]'>Edit</a> | 
<a href='ddel_doctor.php?rno=$row[0]'>Delete</a></td>
</tr>";
$counter++;
}

?>

</table>
</td></tr>
</table>
</body>
</html>

dmod_doctor.php

    <?php

include_once('Connect.php');
            if( isset($_GET['edit']) )
            {
                $id = $_GET['edit'];
                $res= mysql_query("SELECT * FROM doctor WHERE Doctor_id='$id'");
                $row= mysql_fetch_array($res);
            }

            if( isset($_POST['new_Doctor_name']) )
            {
                $new_Doctor_name    = $_POST['new_Doctor_name'];
                $new_Specialization = $_POST['new_Specialization'];
                $sql                = "UPDATE doctor SET Doctor_name='$new_Doctor_name' WHERE Doctor_id='$id'";
                $res                = mysql_query($sql) or die("Could not Update".mysql_error());
            }   
?>

<FORM ACTION="dmod_doctor.php" METHOD="post"> 
Type doctor name <input type="text" name="new_Doctor_name" value="<?php echo $row[1]; ?>" ><br />
Type doctor specialization <input type="text" name="new_Specialization" value="<?php echo $row[2]; ?>" >
<INPUT TYPE="SUBMIT" NAME="UPDATE" VALUE="UPDATE"> 
<p><a href=doctor.php>Back to the Table</a></p>
</FORM>

似乎主键存在,但当我单击编辑按钮时消失。任何帮助将受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

您需要在编辑表单中添加Doctor_id,试试这个:     

include_once('Connect.php');
            if( isset($_GET['edit']) )
            {
                $id = $_GET['edit'];
                $res= mysql_query("SELECT * FROM doctor WHERE Doctor_id='$id'");
                $row= mysql_fetch_array($res);
            }

            if( isset($_POST['new_Doctor_name']) )
            {
                $id    = $_POST['id'];
                $new_Doctor_name    = $_POST['new_Doctor_name'];
                $new_Specialization = $_POST['new_Specialization'];
                $sql                = "UPDATE doctor SET Doctor_name='$new_Doctor_name' WHERE Doctor_id='$id'";
                $res                = mysql_query($sql) or die("Could not Update".mysql_error());
            }   
?>

<FORM ACTION="dmod_doctor.php" METHOD="post"> 
<input type="hidden" name="id" value="<?php echo $id; ?>" />
Type doctor name <input type="text" name="new_Doctor_name" value="<?php echo $row[1]; ?>" ><br />
Type doctor specialization <input type="text" name="new_Specialization" value="<?php echo $row[2]; ?>" >
<INPUT TYPE="SUBMIT" NAME="UPDATE" VALUE="UPDATE"> 
<p><a href=doctor.php>Back to the Table</a></p>
</FORM>