解决switch语句中的会话状态

时间:2018-02-19 16:25:20

标签: php session switch-statement

我正在使用会话来保存状态。正如我已经检查过的那样,州正在得救。但是,仅当会话设置为状态4(排序电子邮件)时,其他功能是在完成时通过电子邮件对数据库进行排序。如果我选择任何其他排序(状态1-3),则一旦调用任何其他功能(删除/更新等),会话就会被保存,数据将恢复为按ID排序。为什么这只在数据按电子邮件(状态4)排序时才起作用,而不是在按其他3个状态排序时?我似乎无法找到这个错误。

<?php

// include database configuration
include "dbheader.php";

// model with database and business logic code
include "crud.model.php";

// make my TPL array at the top, because it might be populated during 
// the switch actions
$TPL = array(); 

$TPL[‘displayTable’] = true;
$TPL[‘updateTable’] = false;
$TPL["fetchedrecord"] = [];

session_start();

switch ($_REQUEST["act"]) { 

  case "updatestart":
          $recordid = $_REQUEST["id"];
          $TPL["fetchedrecord"] = fetchRecord($conn, $recordid);
          $TPL[‘displayTable’] = false;
          $TPL[‘updateTable’] = true;

      if($_SESSION["sess"] == 1){
        $TPL["phonebook"] = sortDataLName($conn); 
      }
      if($_SESSION["sess"] == 2){
        $TPL["phonebook"] = sortDataPosition($conn); 
      }
      if($_SESSION["sess"] == 3){
        $TPL["phonebook"] = sortDataDepartment($conn);
      }
      if($_SESSION["sess"] == 4){
        $TPL["phonebook"] = sortDataEmail($conn);
      }
      else{
        $TPL["phonebook"] = getAllData($conn);
      }
  break;

  // updating a record from the database 
  case "update":

      updateRecord($conn,
              $_REQUEST["id"],
              $_REQUEST["fnameUP"],
              $_REQUEST["lnameUP"],
              $_REQUEST["phoneUP"],
              $_REQUEST["emailUP"],
              $_REQUEST["locationUP"],
              $_REQUEST["mcUP"],
              $_REQUEST["posUP"],
              $_REQUEST["deptUP"]);
            $TPL[‘displayTable’] = true;
            $TPL[‘updateTable’] = false;

      if($_SESSION["sess"] == 1){
        $TPL["phonebook"] = sortDataLName($conn); 
      }
      if($_SESSION["sess"] == 2){
        $TPL["phonebook"] = sortDataPosition($conn); 
      }
      if($_SESSION["sess"] == 3){
        $TPL["phonebook"] = sortDataDepartment($conn);
      }
      if($_SESSION["sess"] == 4){
        $TPL["phonebook"] = sortDataEmail($conn);
      }
      else{
        $TPL["phonebook"] = getAllData($conn);
      }

    break;

  // deleting a record from the database 
  case "delete":

    $recordid = $_REQUEST["id"];
      deleteRecord($conn, $recordid);
      if($_SESSION["sess"] == 1){
        $TPL["phonebook"] = sortDataLName($conn); 
      }
      if($_SESSION["sess"] == 2){
        $TPL["phonebook"] = sortDataPosition($conn); 
      }
      if($_SESSION["sess"] == 3){
        $TPL["phonebook"] = sortDataDepartment($conn);
      }
      if($_SESSION["sess"] == 4){
        $TPL["phonebook"] = sortDataEmail($conn);
      }
      else{
        $TPL["phonebook"] = getAllData($conn);
      }

    break;


  // sorting records by last nae 
  case "sortname":
        $TPL["phonebook"] = sortDataLName($conn); 
        $_SESSION["sess"] = 1;
    break;

  case "sortemail":
        $TPL["phonebook"] = sortDataEmail($conn); 
        $_SESSION["sess"] = 4;
    break;

  case "sortposition":
        $TPL["phonebook"] = sortDataPosition($conn);  
        $_SESSION["sess"] = 2;
    break;

  case "sortdepartment":
        $TPL["phonebook"] = sortDataDepartment($conn);
        $_SESSION["sess"] = 3;
    break;

  // insert a new record into the database  
  case "insert":

      insertRecord($conn,
              $_REQUEST["fname"],
              $_REQUEST["lname"],
                  $_REQUEST["phone"],
                  $_REQUEST["email"],
              $_REQUEST["location"],
              $_REQUEST["mc"],
              $_REQUEST["pos"],
              $_REQUEST["dept"]);
      if($_SESSION["sess"] == 1){
        $TPL["phonebook"] = sortDataLName($conn); 
      }
      if($_SESSION["sess"] == 2){
        $TPL["phonebook"] = sortDataPosition($conn); 
      }
      if($_SESSION["sess"] == 3){
        $TPL["phonebook"] = sortDataDepartment($conn);
      }
      if($_SESSION["sess"] == 4){
        $TPL["phonebook"] = sortDataEmail($conn);
      }
      else{
        $TPL["phonebook"] = getAllData($conn);
      }
    break;

  default:  
     $TPL["phonebook"] = getAllData($conn);
}

// put code here for things that need to happen every time the page loads



// view with our user interface
include "crud.view.php";

?>

1 个答案:

答案 0 :(得分:1)

您有一系列if语句彼此无关,但您仍在尝试最后使用else。这些块中的每个if都必须是elseif

if ($_SESSION["sess"] == 1) {
    $TPL["phonebook"] = sortDataLName($conn); 
} elseif($_SESSION["sess"] == 2) {
    $TPL["phonebook"] = sortDataPosition($conn); 
} elseif($_SESSION["sess"] == 3) {
    $TPL["phonebook"] = sortDataDepartment($conn);
} elseif($_SESSION["sess"] == 4) {
    $TPL["phonebook"] = sortDataEmail($conn);
} else{
    $TPL["phonebook"] = getAllData($conn);
}

你目前的结构方式,如果它匹配除了最后一个条件以外的任何东西,它也会匹配其他,这似乎不是你的意图。

另一种选择是switch语句:

switch ($tpl['phonebook']) {
    case 1:  sortDataLName($conn);      break;
    case 2:  sortDataPosition($conn);   break;
    case 3:  sortDataDepartment($conn); break;
    case 4:  sortDataEmail($conn);      break;
    default: getAllData($conn);         break;
}