首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用于确定$_SESSION数据的if语句

用于确定$_SESSION数据的if语句
EN

Stack Overflow用户
提问于 2019-03-07 14:19:01
回答 2查看 45关注 0票数 0

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

我需要的html表,其中列出了客户记录,以显示一个‘编辑’和‘删除’链接,只有在登录的userID ($_SESSIONuserID)匹配谁创建了记录的userID。因此,如果一名工作人员创建了5条记录中的3条,他们应该只能在这三条记录上看到“编辑”和“删除”超链接,而在其他两条记录上什么也看不到。

我已经设法达到了会话工作的目的--然而,作为PHP的新手,我不确定应该把IF语句放在哪里来回应'Edit‘和'Delete’链接--并且完全不知道如何准确地编写它。我已经试过很多次了,但我现在累死了!任何帮助都将不胜感激。

这是我的会话启动文件(authenticate.php):

代码语言:javascript
运行
复制
<?php
session_start();
$_SESSION["staffID"] = "staffID";
?>

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

代码语言:javascript
运行
复制
<!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文件,在带有“Edit”和“Delete”超链接的表中显示记录:

代码语言:javascript
运行
复制
<!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>
EN

回答 2

Stack Overflow用户

发布于 2019-03-07 17:10:48

代码语言:javascript
运行
复制
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
EN

Stack Overflow用户

发布于 2019-03-07 17:46:46

代码语言:javascript
运行
复制
<?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
?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55037233

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档