0

Dear please help me
i am getting some error in my code

this is my view.php page

<html>
   <body>
      <table  style="border:#333 solid"  border="2">
         <tr>
            <td>ID:</td>
            <td>NAME:</td>
            <td>EMAIL:</td>
            <td>MOBILE:</td>
            <td>EDIT:</td>
            <td>DELETE:</td>
         </tr>
<?php
include_once('connect.php');
$query="select * from emp";
$conn=mysqli_query($con,$query);
if(isset($conn))
{
  echo"This is all data of table.";
}
else
{
  die("query is not execute". mysqli_error());  
}   

 while($row=mysqli_fetch_array($conn))
 {
?>
  <tr>
    <td><?php echo $row['emp_id']; ?></td>
    <td><?php echo $row['emp_name']; ?></td>
    <td><?php echo $row['emp_address'] ?></td>
    <td><?php echo $row['emp_salary'] ?></td>
    <td><a href="edit.php?id=<?php echo $row['emp_id'];?>"><font   color="#FF0000">EDIT</font></a></td>
    <td><a href="delete.php?id=<?php echo $row['emp_id'];?>"><font   color="#FF0000">DELETE</font></a></td>
  </tr>
 <?PHP
 }
 ?>
       </table>
    </body>
 </html>

and this is my delete.php file:

  <?php 
  include_once('connect.php');
  $id=$_GET['emp_id'];
  $query="delete from emp where emp_id='$id'";
  $del=mysqli_query($con,$query);
  if($del)
  {
    echo"record has been deleted";  
  }
  else
  {
    die("Record is not deleted it is query error.".mysqli_error()); 
  }
  ?>

How can I access the ID of the view.php in the delete.php file and how to delete a single row in my db table. I don't understand how to access the id of view.php. In this program I don't delete the selected row. Please tell me how to delele data. please anyone help me.

Mainz007
  • 533
  • 5
  • 16
furkanali89
  • 150
  • 4
  • 14

3 Answers3

3

You are sending edit.php?id= so need to get id from query string using GET['id']

$id = $_GET['id'];
Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44
1

change the code in delete.php as follow

 <?php 
 include_once('connect.php');
 $id=$_GET['id'];
 $query="delete from emp where emp_id='$id'";
 $del=mysqli_query($con,$query);
 if($del)
 {
     echo"record has been deleted";   
 }
 else
 {
     die("Record is not deleted it is query error.".mysqli_error());  
 }
 ?>
Prateik Darji
  • 2,297
  • 1
  • 13
  • 29
0

Please also realize that using $_GET data within your queries is only asking for trouble, especially when it's not escaped properly. You're opening your self up to SQL injection attacks.

Make use of mysql_real_escape_string if you're going to be using mysql_* functions, just realize that it has been deprecated in PHP 5.5.

You also have the option of using MySQLi or PDO would be preferable since they both make use of prepared statements as mentioned in this previous question How can I prevent SQL-injection in PHP?

Community
  • 1
  • 1
Eliel
  • 164
  • 6