0

Hello guys I am new to php and we have a project where only admin can delete the records and no other user can delete it. I researched about this and understood about how this requires session but was not able to implement it here also my question is how do I understand which user is logged in

This is the code for Login page

<?php

$link = mysqli_connect('localhost', 'root', '','kaamkhoj');
if (!$link)
 {
    die('Could not connect: ' . mysql_error());
}




    //to check login button clicked or not
   if(isset($_POST['btn']))
   {
    $Username    = $_POST['username'];
    $Password = $_POST['psw1'];

    $query = "select * from adminlogin WHERE username='$Username'AND password= '$Password' ";
    $query_run = mysqli_query($link,$query);


      if(mysqli_num_rows($query_run)>0)
      {
        echo "<center><font face='Verdana' size='2' color=green><strong>Welcome, You have successfully logged_In.</strong><br><br><a href=adminindex.php>Click here to visit</a><br></font></center>";
      }

      else
      {
        echo '<script type="text/javascript"> alert("Invalid Username And Password!")</script>';
      }

   }

?>

This is where data entry will be deleted

<?Php
session_start();

include "index1.php";



?>






</br>
</br>

<?php

$link = mysqli_connect('localhost', 'root', '','kaamkhoj');
if (!$link)
 {
    die('Could not connect: ' . mysql_error());
}

$sql = "SELECT * FROM (
    SELECT * FROM employee ORDER BY id DESC LIMIT 0,1000
) sub
ORDER BY id ASC";
$_SESSION["low"] = 0;
$_SESSION["high"] = 1000;
$sq = "select count(id) from employee";

$res = mysqli_query($link, $sq);
$rows = mysqli_fetch_array($res,MYSQLI_NUM);

$result = mysqli_query($link, $sql);
$rowcount=mysqli_num_rows($result);

$_SESSION["rows"] = $rows[0];
echo "<div align='center'> <strong> TOTAL RESULTS : $rowcount/$rows[0] </strong> </div>";
?>

<button class="btn btn-primary center" onclick="location.href='batch.php'">Next Page</button>

<?php

if($rowcount>0) {


?>
<table class="table table-responsive " align=center border="2">  <tr>
<?php

  printf("<th> Id </th>");
  printf("<th> Date </th>");
  printf("<th> Name </th>");
  printf("<th> Gender </th>");
  printf("<th> Age </th>");
  printf("<th> Religion </th>");
  printf("<th> City </th>");
  printf("<th> Area </th>");
  printf("<th> Phone </th>");
  printf("<th> Whataspp </th>");

  printf("<th> JobCategory </th>");
  printf("<th> Hours </th>");
    printf("<th> Email </th>");
    printf("<th> Action </th>");



while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
 {


   printf("<tr bgcolor='#ffffff'>");
    printf("<td  >");
    printf( "%s" , $row["id"]);
    printf("</td>");



    printf("<td >");
    printf( "%s" , $row["Date"]);
    printf("</td>");
    printf("<td  >");




    printf("%s", $row["Name"]);

    printf("</td>");
    printf("<td   >");

    printf("%s", $row["Gender"]);

   printf("</td>");
    printf("<td  >");

    printf("%s", $row["Age"]);

    printf("</td>");
    printf("<td>");

    printf("%s", $row["Religion"]);

    printf("</td>");
    printf("<td>");

    printf("%s", $row["City"]);

    printf("</td>");
    printf("<td>");

    printf("%s", $row["Area"]);

    printf("</td>");
    printf("<td>");

    printf("%s", $row["Phone"]);

    printf("</td>");


    printf("<td>");   
    printf("%s", $row["Whatsapp"]);
    printf("</td>");






    printf("<td>");

    printf("%s", $row["JobCategory"]);
    printf("</td>");
    printf("<td>");

    printf("%s", $row["Hours"]);
    printf("</td>");


    printf("<td>");

    printf("%s", $row["email"]);
    printf("</td>");
        printf("<td  >");

     echo '<a href=" employeeupdate.php?id='.$row["id"].'"><strong>UPDATE</strong> </a>';
      echo '<a href=" employeedelete.php?id='.$row["id"].'"><strong>DELETE</strong> </a>';
      echo '<a href=" employeematch.php?id='.$row["Name"].'&gender='.$row["Gender"].'&age='.$row["Age"].'&city='.$row["City"].'&area='.$row["Area"].'&job='.$row["JobCategory"].'"><strong>MATCH</strong> </a>';



    printf("</td>");





}
printf("</tr></table>");
mysqli_free_result($result);



} else {

    echo "<div align='center'> <strong> RESULT NOT FOUND </strong> </div>";
}






?>


    <!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

  </body>
</html>



Sayaji
  • 127
  • 1
  • 1
  • 8
  • You have an error. [`mysql_error()`](https://www.php.net/manual/en/function.mysql-error.php) worked only for the old API. Please consider switching error mode on instead. [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Dec 27 '19 at 12:34

1 Answers1

0

You can use the session to mark the user as admin. For example Login page

<?php
session_start();

//...code

$query = "SELECT count(*) FROM adminlogin WHERE username='$Username' AND password= '$Password'";
$query_run = mysqli_query($link,$query);

if(mysqli_num_rows($query_run) > 0) {
    $_SESSION['is_admin'] = true;
    echo "<center><font face='Verdana' size='2' color=green><strong>Welcome, You have successfully logged_In.</strong><br><br><a href=adminindex.php>Click here to visit</a><br></font></center>";
}
//...code

and script where data entry will be deleted

//...code
printf("</td>");
printf("<td  >");

if (!empty($_SESSION['is_admin']) && true === $_SESSION['is_admin']) {
    echo '<a href=" employeeupdate.php?id='.$row["id"].'"><strong>UPDATE</strong> </a>';
    echo '<a href=" employeedelete.php?id='.$row["id"].'"><strong>DELETE</strong> </a>';
}

//...code
Mike Foxtech
  • 1,633
  • 1
  • 6
  • 7