-2

I am going to update the student database which contains name,location,address and phonenumber for a particular id.When i run the below code it is saying undefined mid is giving, can u please check it and rectify ,my error please....here is my code insert.php

<?php
   $user="root";
   $server="localhost";
   $password="";
   $db="students";
   $dbconn= mysql_connect($server,$user,$password);
   mysql_select_db($db,$dbconn);
   if(isset($_POST['submit'])) {
      $name=mysql_real_escape_string($_POST['name']);
      $location=mysql_real_escape_string($_POST['location']);
      $address=mysql_real_escape_string($_POST['address']);
      $phonenumber=mysql_real_escape_string($_POST['phonenumber']);    
      $str="insert into info(name,location,address,phonenumber) VALUES ('$name','$location','$address','$phonenumber')";
      $query=mysql_query($str);
      if($query) {
         echo"Inserted Successfully";
      } else {
         echo "Insert Failed";
      }
      $query2=mysql_query("select * from info");
      echo "<table border='2'>";
      echo "<tr><th>Name</th><th>Location</th><th>Address</th><th>Phonenumber</th><th>Action</th></tr>";
      while($row=mysql_fetch_array($query2)) {
         echo "<tr>";
         echo "<td>".$row['name']."</td>";
         echo "<td>".$row['location']."</td>";
         echo "<td>".$row['address']."</td>";
         echo "<td>".$row['phonenumber']."</td>";
         echo "<td><a href='modify.php?sid=".$row['id']."'>Update</a></td>";        
         echo "</tr>";
      }
      echo "</table>";
   }
?>

modify.php

<?php
   $user="root";
   $server="localhost";
   $password="";
   $db="students";
   $dbconn= mysql_connect($server,$user,$password);
   mysql_select_db($db,$dbconn);
   if(isset($_GET['sid'])) {
      $mid=$_GET['sid'];
      echo  $mid;
   }
   $q="select * from info where id=$mid";
   echo $q;
   $query3=mysql_query($q);    
   $row=mysql_fetch_array($query3);
?>
<html>
<head><title>Updation</title></head>
<body>    
   <form method="POST" action="edit.php" id="myform">
      EnterName:<input type="text" name="name" id="name" value="<?php echo $row['name'];?>"><br/>
      EnterLocation:<input type="text" name="location" id="location"value="<?php echo $row['location'];?>"><br/>
      EnterAddress:<input type="text" name="address" id="address" value="<?php echo $row['address'];?>"><br/>
      EnterPhoneNumber:<input type="text" name="phonenumber" id="phonenumber" value="<?php echo $row['phonenumber'];?>"><br/>
      <input type="hidden" name="id" value=<?php if(isset($mid)) echo $mid; ?>>
      <input type="submit" name="update" value="update">
   </form>    
</body>
</html>

Finally i am redirecting update to edit.php edit.php

<?php
   $user="root";
   $server="localhost";
   $password="";
   $db="students";
   $dbconn= mysql_connect($server,$user,$password);
   mysql_select_db($db,$dbconn);
   if(isset($_GET['sid'])) {
      $mid=$_GET['sid'];
      echo  $mid;
   }
?>
<html>
<head><title></title></head>
<body>
   <form>
      <input type="hidden" name="id" value="<?php if(isset($mid)) echo $mid; ?>">
   </form>
</body>
</html>
<?php
   if(isset($_POST['update'])) {
      echo $mid;
      $name=mysql_real_escape_string($_POST['name']);
      $location=mysql_real_escape_string($_POST['location']);
      $address=mysql_real_escape_string($_POST['address']);
      $phonenumber=mysql_real_escape_string($_POST['phonenumber']);
      $query5=mysql_query("update info set name='$name',location='$location',address='$address',phonenumber='$phonenumber' where id=$mid");
      if($query5) {
         echo "update success";
      } else {
         echo "Update Failed";
      }    
   }
?>
M Hamza Javed
  • 1,269
  • 4
  • 17
  • 31
  • 3
    Where you get this code from ? :S You may search for PDO update query in PHP in google, and never use MySQL_ again ! You really need to write your scripts again with proper codding ! –  Feb 23 '17 at 10:09
  • 1
    If `$mid` is undefined, it usually means that it is not defined, troubleshoot your code! – JustBaron Feb 23 '17 at 10:09

2 Answers2

0

if you want to get simple answer you can add

$mid = '';

before

if(isset($_GET['sid'])) { $mid=$_GET['sid']; echo$mid; }

so your code be like this :

$mid = ''; if(isset($_GET['sid'])) { $mid=$_GET['sid']; echo$mid; }

OR you can add isset every you use $mid.

echo isset($mid) ? $mid : '';
0

In edit.php file you have used $_GET['sid'] whereas in you have used field name id in hidden field of modify.php file

replace this line

<input type="hidden" name="id" value=<?php if(isset($mid)) echo $mid; ?>>

with below line

<input type="hidden" name="sid" value=<?php if(isset($mid)) echo $mid; ?>>

One more thing. You have used POST method in edit.php changed it with GET or access sid using $_POST['sid']

this will solve you problem

Thanks
Hope! this will help you

Shishil Patel
  • 3,449
  • 2
  • 12
  • 16