if语句确定$ _SESSION数据

时间:2019-03-07 06:19:01

标签: php if-statement session echo

我已经创建了一个客户数据库,其中4-5名员工将有权登录以查看,编辑和删除记录。

仅当登录的用户ID($ _SESSION [userID])与创建记录的用户ID匹配时,我才需要列出客户记录的html表以显示“编辑”和“删除”链接。因此,如果一个工作人员在5条记录中创建了3条,则他们只能看到针对这3条记录的“编辑”和“删除”超链接,而对其他2条记录则什么也没有。

我已经设法达到了工作阶段的目的-但是,对于PHP来说,我是不熟悉的,所以我不确定将IF语句确切地放在何处以呼应'Edit'和'Delete'链接-并完全迷失了准确地写出来。我已经尝试了很多尝试,但是现在正在把头发拔掉!任何帮助将不胜感激。

这是我的会话开始文件(authenticate.php):

<?php
session_start();
$_SESSION["staffID"] = "staffID";
?>

工作人员登录文件(staff_login.php):

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Staff login</title>
</head>
<body>
<?php
require("db.php");

session_start();
// If form submitted, insert values into the database.
if (isset($_POST['username'])){
        // removes backslashes
    $username = stripslashes($_REQUEST['username']);
        //escapes special characters in a string
    $username = mysqli_real_escape_string($con,$username);
    $password = stripslashes($_REQUEST['password']);
    $password = mysqli_real_escape_string($con,$password);
    //Checking if user existing in the database or not
        $query = "SELECT * FROM `staff login` WHERE username='$username'
and password='$password'";
    $result = mysqli_query($con,$query) or die(mysql_error());
    $rows = mysqli_num_rows($result);
        if($rows==1){
        $_SESSION['username'] = $username;
        $_SESSION[staffID] = $rows["$staffID"];
            // Redirect user to edit_contact.php - was index.php -
        header("Location: edit_contact.php");
         }
    else
    {
    echo "<div class='form'>
<h3>Username/password is incorrect.</h3>
<br/>Click here to <a href='staff_login.php'>Login</a></div>";
    }
    }else{
?>
<div class="form">
<h1>Staff login</h1>
<form action="" method="post" name="login">
<input type="text" name="username" placeholder="Username" required />
<input type="password" name="password" placeholder="Password" required />
<input name="submit" type="submit" value="Login" />
</form>
</div>
<?php } ?>
</body>
</html>

还有php文件,用于在带有“编辑”和“删除”超链接的表中显示记录:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Edit contact</title>
</head>
<body>
<h2>Tate Finance Customer contact details</h2>

<?php

//***edit_contact.php***///
// Developed by: []
// Contact: []
// Created: [November 2018]
// Last Modified: [26 November 2018]
/* Purpose: This file lists all contacts from the mycontacts database in a table for logged in users to add, edit or delete their contacts.*/

//include authenticate.php file on all secure pages
require('db.php');
include("authenticate.php");

    ?>
    <!--Add welcome note to staff user-->
    <p>Welcome <?php echo $_SESSION['username']; ?>!</p>
    <p><a href="logout.php">Logout</a></p>
    <h3><a href="insert.php">Add new customer</a></h3>
    <?php


$con = mysqli_connect("localhost","root","xxxxxx","mycontacts");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
else
{
    // Show all contacts from database in a table list
$query = "SELECT * FROM contact ORDER BY conName ASC";
$rst = mysqli_query($con,$query);

if($rst)
{
if(mysqli_num_rows($rst)>0)
{
    // Table design for contacts list
echo "<table border='1'><tr><td>Edit contact</td><td>Name</td><td>Address</td><td>Phone</td><td>Mobile</td><td>Email</td></tr>";
while ($row = mysqli_fetch_assoc($rst))
{
    /* Present contacts details in table list according to id selected, with links to edit or delete according to contactID selected */

/* This is where I think my IF statement needs to go, but can't figure out how/what to write to make it work */

echo "<tr><td><a href=editContact.php?id=".$row['contactID'].">Edit</a><a href=delete_record.php?id=".$row['contactID'].">&nbsp;&nbsp;Delete</a></td><td>".$row['conName']."</td><td>".$row['conAddress']."</td><td>".$row['conPhone']."</td><td>".$row['conMobile']."</td><td>".$row['conEmail']."</td></tr>";
}
echo "</table>";
}
}
else
{
echo "No results found";
}
}

    ?>

</body>
</html>

2 个答案:

答案 0 :(得分:0)

while ($row = mysqli_fetch_assoc($rst))
{
echo "<tr>";

if($_SESSION["staffID"] == $id_of_creator){
     echo "<td>".
     "<a href=editContact.php?id=".$row['contactID'].">Edit</a>".
     "<a href=delete_record.php? 
      id=".$row['contactID'].">&nbsp;&nbsp;Delete</a> ".
     "</td>";
}else echo "<td></td>";

echo "<td>".$row['conName']."</td><td>".$row['conAddress']."</td><td>".$row['conPhone']."</td><td>".$row['conMobile']."</td><td>".$row['conEmail']."</td></tr>";

}

答案 1 :(得分:0)

<?php
while($row = mysqli_fetch_assoc($selectAllCustomer)){
      $id = $row['customer_id'];
      $name= $row['customer_id'];
      $email= $row['customer_email'];

      echo "<tr>";

      if($_SESSION['staffID'] == $Admin_Id){
         echo "<td>".$name."</td>";
         echo "<td>".$email."</td>";
         echo "<td>";
         echo "<a href='editPage.php?edit='".$id."'>Edit</a>";
         echo "</td><td>";
         echo "<a href='deletePage.php?delete='".$id."'>Delete</a>";
         echo "</td>";

      }else{
          echo "<td>".$name."</td>";
          echo "<td>".$email."</td>";
         }

        echo "</tr>";
}

NB: the valiable $admin_Id, is a id of the creator
?>