Conditional delete

In my data base , i want to delete records however the username ‘admin’ cannot be deleted. When i try to delete ‘admin’ i get error message " Admin cannot be deleted, how ever I am unable to delete non admin , Appreciate your help
code follows:-

  1 <!DOCTYPE html>
  2 <html>
  3 <head>
  4 <meta charset="utf-8">
  5 <title>Registration</title>
  6 <link rel="stylesheet" href="styles.css" /> 
  7 </head>
  8 <body>
  9 <center> <table border='0' cellpadding='10' cellspacing='0' style=\"border: 2px solid #07ACE    1;
 10             border-radius: 1em 1em 1em 1em;>
 11 
 12 <table border='1'>
 13                 <td id=tdfrow>User ID</td>
 14                 <td id=tdrow>Username</td>
 15                 <td id=tdrow>Email</td>
 16                 <td id=tdrow>Date  Time</td>
 17                 <td id=tdlrow> Action</td>
 18 
 19 <?php
 20 require ('header_new.php') ;
 21 require('config.php');
 22 echo"<h3>Display Users</h3>";
 23 
 24 $sql_data = "SELECT * FROM login";
 25 $result_data=mysqli_query($conn, $sql_data);
 26 
 27 if (!$result_data)  {
 28         die ('Could not get data:' . mysqli_error());
 29 }
 30 
 31 while($row_data = mysqli_fetch_assoc($result_data)) {
 32   echo "<tr>";
 33   echo "<td>" . $row_data['id'] . "</td>";
 34   echo "<td>" . $row_data['username'] . "</td>";
 35   echo "<td>" . $row_data['email'] . "</td>";
 36   echo "<td>" . $row_data['dtime'] . "</td>";
 37   echo "<td><form class='new' action= ''  method='POST'><input type='hidden'  name='id'  val    ue=" .$row_data['id']. "><input type='submit'  class='cancelbtn1' name='delete' value='DELET    E''></form></td>";
 38 }
 39 if(isset($_POST['delete'])) {
 40         $id=$_POST['id'];
 41 echo "$id";
 42 $query = "SELECT * FROM login WHERE id='$id'";
 43 $result_data = mysqli_query($conn,$query);
 44 while($row_data=mysqli_fetch_assoc($result_data )) {
 45         echo $row_data['username'];
 46         
 47         if($row_data['username']=='admin') {
 48        echo "Admin cannot be deleted";
49 } else {
 50             $query = "DELETE FROM login WHERE id='$id'";
 51             header("Location:register_view.php");
 52 
 53 }
 54 }
 55 }
 56 
 57 ?>
 58 </body>
 59 </html>

echo “

”;
33 echo “” . $row_data[‘id’] . “”;
34 echo “” . $row_data[‘username’] . “”;
35 echo “” . $row_data[‘email’] . “”;
36 echo “” . $row_data[‘dtime’] . “”;
37 echo “<input type=‘submit’ class=‘cancelbtn1’ name=‘delete’ value=‘DELETE’’>”;
38 }

When posting code, use bbcode [code][/code] tags. I have made this change to your first post.

Where are you executing the DELETE query?

If you see line 50 , the delete query

you mean that i enter the the code followed by here ?

I got it working partly, the echo “Cannot delete admin !..”; part is not working,
it deletes users except admin however it does not give the above message.

 while($row_data = mysqli_fetch_assoc($result_data)) {
 32   echo "<tr>";
 33   echo "<td>" . $row_data['id'] . "</td>";
 34   echo "<td>" . $row_data['username'] . "</td>";
 35   echo "<td>" . $row_data['email'] . "</td>";
 36   echo "<td>" . $row_data['dtime'] . "</td>";
 37   echo "<td><form class='new' action= ''  method='POST'><input type='hidden'  name='id'  value=" .$row_    data['id']. "><input type='submit'  class='cancelbtn1' name='delete' value='DELETE''></form></td>";
 38 
 39   if(isset($_POST['id'])) {
 40           $id=$_REQUEST['id'];
 41           if ($row_data['username'] !='admin') {
 42                 $query = "DELETE FROM login WHERE id=$id AND username!='admin'";
 43                 $result = mysqli_query($conn,$query);
 44                 header("Location: register_view.php");
 45         } else {
 46                 echo "Cannot delete admin !.....";
 47                   }     
 48                 }
Sponsor our Newsletter | Privacy Policy | Terms of Service