1

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.

Chetan Gawai
  • 2,361
  • 1
  • 25
  • 36
  • 1
    [Please, don't use `mysql_*` functions in new code](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [red box](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). **You are also wide open to [SQL injections](http://stackoverflow.com/q/60174)** – John Conde Apr 18 '14 at 11:49
  • Which page you are executing..? – Mahendra Jella Apr 18 '14 at 11:50

1 Answers1

0

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>
Alexey Palamar
  • 1,440
  • 1
  • 10
  • 16
  • Thank you but it does not work. It is still the same problem "
    Notice: Undefined variable: row in D:\xampp\htdocs\Khoa\dmod_doctor.php on line 23
    "
    – user3548673 Apr 18 '14 at 14:25
  • try to do var_dump($row); after if(isset($_GET['edit'])){}. Do you have some data in $row? – Alexey Palamar Apr 18 '14 at 14:27
  • I don't think I have any data in $row. However, i'm new to this one so I am not sure. I tried var_dump($row) and the Doctor_name was changed but not the Specialization. I tried to delete **value=""** and **value=""** and got the same result as var_dump($row). – user3548673 Apr 18 '14 at 14:44
  • UPDATE doctor SET Doctor_name='$new_Doctor_name' WHERE Doctor_id='$id' In this query you update only Doctor name. You don't use specialization.. – Alexey Palamar Apr 18 '14 at 14:54