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

时间:2022-09-25 19:01:25

I have searched through several similar problems but I could not update records no matter how I tried. it keeps showing the Notice: undefined variable. Here are my code:

我已经搜索了几个类似的问题,但无论我怎么做,我都无法更新记录。它不断显示通知:未定义的变量。这是我的代码:

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>

it seems like the primary key is there but disappears when I click the edit button. Any help will be highly appreciated.

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

1 个解决方案

#1


0  

You need to add Doctor_id to your edit form, try this:

您需要将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>

#1


0  

You need to add Doctor_id to your edit form, try this:

您需要将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>