简体   繁体   中英

Cannot update database using php form

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.

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM