if语句确定$ _SESSION数据

问题描述 投票:0回答:2

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

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

我已经成功地完成了会议的工作 - 然而,对PHP不熟悉我不确定在哪里确切地说我的IF语句来回应'编辑'和'删除'链接 - 并完全迷失了如何编写它究竟。我尝试过很多尝试,但是现在我的头发已经撕掉了!任何帮助将非常感激。

这是我的会话启动文件(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>
php if-statement session echo
2个回答
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>";

}

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
?>
© www.soinside.com 2019 - 2024. All rights reserved.