Simplest PHP code to delete a record

image

Listing4delete.php

<html>
<head>
<title>Search by Name</title>
</head>
<body>
Search for name<br>
<form action="" method="get" name="formsearch">
Enter a name <input name="txtsearch" type="text">
<input name="btnsearch" type="submit" value="Search">
</form>
 
<?php
//to create the bridge
$db=mysqli_connect("localhost","root","","mycompanyhr");
 
//connection failed
if($db==false){//connection error
    echo("Connection failed : ". 
            mysqli_connect_error($db));
    exit();
}
//u are connected to db
else{
    echo("Connected to database <hr>");
}
 
$n=$_GET['txtsearch'];
//sql command to retrieve records
$sql="select * from employee
        where FIRSTNAME like '%$n%'";
 
//execute the sql command
$qr=mysqli_query($db, $sql);
 
 
//if there's any SQL error
if($qr==false){
    echo ("Query cannot be executed!<br>");
    echo ("SQL Error : ".mysqli_error($db));
}
//no SQL error, so display data
else{
    echo "<table border=1>";
    while($rekod=mysqli_fetch_array($qr)){
        echo "<tr>";
        echo "<td>".$rekod['EMPNO']."</td>";
        echo "<td>".$rekod['FIRSTNAME']."</td>";
        echo "<td>".$rekod['LASTNAME']."</td>";
        echo "<td>".$rekod['PHONENO']."</td>";
        echo "<td>".$rekod['JOB']."</td>";
        echo "<td> <a href='delete.php?id=".$rekod['EMPNO']."'> 
                delete </a> </td>";
        echo "<tr>";
    }
    echo "</table>";
}
?>
</body>
</html>

 


image


Delete.php



<?php
//extract info from id 
  $empno=$_GET['id'];
 
//develop the SQL command to delete record 
  $sql="delete from employee 
        where EMPNO='$empno'"; 
 
//database connection 
$db=mysqli_connect("localhost","root","","mycompanyhr"); 
 
  // checking database connection 
  if ($db==false) { 
    echo "Connect failed: ". mysqli_connect_error($db); 
    exit(); 
  } 
  else { 
    echo "Connection successful"; 
  } 
 
//execute the SQL command 
  $rs=mysqli_query($db,$sql); 
 
//check the execution status 
  if($rs==false){ 
    echo "Record cannot be deleted!<br>"; 
    echo "SQL Error : ".mysqli_error($db); 
  } 
  else{ 
    echo "<br>Record for $empno deleted!<br>"; 
    echo "<a href='listing4delete.php'> 
          Back to the list</a>"; 
  } 
?> 

Popular Posts