【问题标题】:$_SESSION variable doesn't set$_SESSION 变量未设置
【发布时间】:2021-01-01 02:22:18
【问题描述】:

这是我尝试使用 $_SESSION id 变量从本地服务器获取数据时的代码。在此页面中,我收到错误消息:

<?php
$dbServername = "localhost";
$dbUsername = "root";
$dbPaasword = "";
$dbName = "ridobiko";

$con = mysqli_connect($dbServername, $dbUsername, $dbPaasword, $dbName) or die(mysqli_error($con));
if(!isset($_SESSION)) 
{ 
    session_start(); 
}

$bike_id = $_SESSION['id'];

?>



<!DOCTYPE html>
<html>
<head>
    <title>Page 2</title>
</head>
<body>
    <table>
        <thead>
            <tr><b>Bike Brand</b></tr>
            <tr><b>Bike Name</b></tr>
            <tr><b>Bike Image</b></tr>
            <tr><b>Bike rent price</b></tr>
        </thead>
        <tbody>
            <?php
            $query = "SELECT * FROM bike_details WHERE bike_id = '$bike_id';";
            $run_query = mysqli_query($con,$query) or die(mysqli_error($con));
            $row = mysqli_fetch_array($run_query);
            echo "
                <td>".$row['bike_brand']."</td>
                <td>".$row['bike_name']."</td>
                <td><img src='".$row['bike_image']."' height='100' width='100'></td>
                <td>".$row['bike_price']."</td>
            ";
            ?>
        </tbody>
    </table>
</body>

虽然我在上一页设置了 $_SESSION 变量。在此页面中,我与本地服务器连接,并通过此页面将我的详细信息存储在本地服务器中并存储 $_SESSION 变量值:

if($total_accounts>0){
    echo "
        <script type='text/javascript'>
            alert('Bike details alreay exists');
            window.location='question1.php';
        </script>
    ";
}

else {
    $filename = $_FILES["bike_image"]["name"];
    $tempname = $_FILES["bike_image"]["tmp_name"];
    $foldername = "bike_image/".$filename;
    move_uploaded_file($tempname, $foldername); 
    $query = "INSERT INTO bike_details (bike_brand,bike_name,bike_price,bike_image) VALUES ('$bike_brand', '$bike_name', '$bike_price', '$foldername');";
    $run_query = mysqli_query($con, $query) or die(mysqli_error($con));
    
    $_SESSION['id'] = $bike_id;
    echo $_SESSION['id'];
    echo "
        <script type='text/javascript'>
            alert('Bike details updated successfully');
            window.location='question2.php';
        </script>
    ";

}

这是我在运行代码时遇到的错误:

Bike BrandBike NameBike ImageBike rent price
Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\ridobiko_question1\question2.php on line 56

Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\ridobiko_question1\question2.php on line 57

Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\ridobiko_question1\question2.php on line 58

Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\ridobiko_question1\question2.php on line 59

【问题讨论】:

  • 请不要将代码发布为图片
  • @executable 你能再检查一次吗,我编辑了我的问题。希望对您有所帮助。

标签: php session xampp


【解决方案1】:

任何页面需要访问$_SESSION应该开头:

<?php
session_start();
/* other code... */

/* don't do this: 
if (!isset($_SESSION)) {
  session_start();
}
*/
?>

否则你会得到不想要的结果。此外,如果您想检查您是否已经在会话中设置了一个值,您可以通过以下方式对其进行测试:

<?php
session_start();

if (!isset($_SESSION["someVar"])) {
  $_SESSION["someVar"] = "defaultValue";
  /** any default values you want **/
}

/* now you can continue with $_SESSION["someVar"] where is needed */
?>

您应该应用这些更改,然后重试。

【讨论】:

    猜你喜欢
    • 2021-11-29
    • 1970-01-01
    • 2013-12-25
    • 2012-10-08
    • 1970-01-01
    • 2012-11-19
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多