是否可以将会话与sql进行比较?

时间:2017-06-20 07:38:19

标签: php sql session if-statement

我试图通过比较$ resultsub(通过sql)和$ _id(会话)来使用if语句。我试过用<>和==。它似乎不起作用。我认为我的 if语句有一些错误。请帮我纠正我的PHP代码。

这是代码。

    <?php
include 'db_connect.php';

session_start(); 
$_id = $_SESSION['staff_id'];


if(isset($_POST['submit']))
{
    $sub_code = $_POST['sub_code'];
    $doc_name = $_POST['doc_name'];

    $checksub = "SELECT staff_id FROM subject WHERE sub_code='$sub_code' ";
    $resultsub = mysqli_query( $link,$checksub) or die("Query failed");

    if ($resultsub == $_id)
    {


      if((IsChecked('tick','delete')) && (IsChecked('tick','add')))
      {
           //some other code 

      }
    }

   else
     echo "you cannot upload file. Only coordinator for every subject only can upload file." ;


}


?>

它没有显示任何错误。但它不检查我的if语句并跳转到else。

1 个答案:

答案 0 :(得分:0)

尝试在php open标签之后启动你的会话,如下面的

<?php
session_start();
include 'db_connect.php';

,第二件事是你需要使用函数$result_sub从变量mysqli_fetch_array()中获取必填字段。试试这段代码。

    <?php
session_start(); 
include 'db_connect.php';


$_id = $_SESSION['staff_id'];


if(isset($_POST['submit']))
{
    $sub_code = $_POST['sub_code'];
    $doc_name = $_POST['doc_name'];

    $checksub = "SELECT staff_id FROM subject WHERE sub_code='$sub_code' ";
    $resultsub = mysqli_query( $link,$checksub) or die("Query failed");

    $row=mysqli_fetch_array($resultsub,MYSQLI_ASSOC);

    if ($row['nameOfYourIdField'] == $_id)
    {


      if((IsChecked('tick','delete')) && (IsChecked('tick','add')))
      {
           //some other code 

      }
    }

   else
     echo "you cannot upload file. Only coordinator for every subject only can upload file." ;


}


?>