1

在我下面的代码中,它无法识别该变量$userid,该变量确定了登录教师的 ID,但在 mysqli 代码中,它无法确定$userid我何时回显它。但它确实知道用户是通过它的$userid. 所以我的问题是,在 mysqli 为什么找不到$userid?请查看 mysqli 设置中的评论bind_param(),这就是问题所在。

以下是php和mysqli的代码:

让我声明 member.php 是包含以下$userid信息的脚本:

    <?php
     session_start();

        include('member.php');

    ...

        function PickSession()
        {

            //Get data from database
            $sessionquery = "
                SELECT s.SessionId, SessionName, s.TeacherId
                FROM Session s
                INNER JOIN Session_Complete sc ON sc.SessionId = s.SessionId
                WHERE
                (ModuleId = ? AND Complete = ? AND s.TeacherId = ?)
                ORDER BY SessionName 
                ";
            $complete = 1;

            global $mysqli;
            $sessionqrystmt=$mysqli->prepare($sessionquery);
            // You only need to call bind_param once
            $sessionqrystmt->bind_param("iii",$moduleId, $complete, $userid);//it doesn't recognse $userid
            // get result and assign variables (prefix with db)
            $sessionqrystmt->execute(); 
            $sessionqrystmt->bind_result($dbSessionId,$dbSessionName,$dbTeacherId);
            $sessionqrystmt->store_result();
            $sessionnum = $sessionqrystmt->num_rows();  

echo $userid; //nothing displayed in this echo
    ...

            if ((isset($username)) && (isset($userid))){ //user is successfully logged in

            include('teachername.php');

    ...

        <?php


        ShowAssessment(); // Show information



            }else{ 

    echo "Please Login to Access this Page | <a href='./teacherlogin.php'>Login</a>";

    }

            ?>

member.php 脚本:

if (isset($_SESSION['teacherid'])) {

      $userid = $_SESSION['teacherid'];

  }

if (isset($_SESSION['teacherusername'])) {

      $username = $_SESSION['teacherusername'];

  }

        ?>
4

4 回答 4

2

您尚未初始化$userid变量,您必须在查询中设置绑定变量的值,否则它将NULL为该变量取值。

$userid = "value";
$sessionqrystmt->bind_param("iii",$moduleId, $complete, $userid);

$userid变量在 中不可访问PickSession()。因此,建议要么$userid设为全局,要么将其作为函数中的参数传递。

于 2013-01-23T12:03:06.847 回答
1

可能是因为session_start()缺少

于 2013-01-23T12:02:40.480 回答
0

您无权访问$userid您的功能。你可以做的事情:

声明$userid为全局变量(不推荐)

function PickSession() {
    global $userid;
    // and the rest of the function here
}

$userid作为参数传递给函数PickSession

于 2013-01-23T12:03:59.900 回答
0

在你的PickSession函数中,$userid没有值,因为你没有定义它,你应该在调用它时将变量传递给函数,例如;

function PickSession($userid)
{
    ...
}

// Now call PickSession with the variable assigned:
PickSession($userid);
于 2013-01-23T12:05:49.353 回答