使用会话变量时出现未定义的索引错误

时间:2020-05-10 08:21:37

标签: php mysql session

我正在一个已经建立了注册,登录,配置文件更新表单的项目中。

我正在使用会话获取当前登录的用户信息,以便能够使用个人资料更新表单来更新其个人资料。现在的问题是,当我分配这些会话变量以更新表格时。我收到以下错误。

这是我的authController.php,其中对php的所有处理进行了编码。

session_start();
$username = "";
$gender = "";
$dob = "";
$country = "";
$state = "";

  if (isset($_POST['update-btn'])) {
    if (empty($_POST['username'])) {
        $errors['username'] = 'Username required';
    }
    if (empty($_POST['gender'])) {
        $errors['gender'] = 'Gender required';
    }
    if (empty($_POST['dd'])) {
        $errors['dd'] = 'Day required';
    }
    if (empty($_POST['mm'])) {
        $errors['mm'] = 'Month required';
    }
    if (empty($_POST['yyyy'])) {
        $errors['yyyy'] = 'Year required';
    }
    if (empty($_POST['country'])) {
        $errors['country'] = 'Country required';
    }
    if (empty($_POST['state'])) {
        $errors['state'] = 'State required';
    }

    $username = $_POST['username'];
    $gender = $_POST['gender'];
    $dob = $_POST['yyyy']."/". $_POST['mm']."/".$_POST['dd'];   
    $country = $_POST['country'];
    $state = $_POST['state'];

        if (count($errors) === 0) {
        $query = "UPDATE users SET username=?, gender=?, dob=?, country=?, state=? WHERE id = '".$_SESSION['id']."'";
        $stmt = $conn->prepare($query);
        $stmt->bind_param('sssss', $username, $gender, $dob, $country, $state);
        $result = $stmt->execute();

        $stmt->close();

            $_SESSION['username'] = $username;
            $_SESSION['gender'] = $gender;
            $_SESSION['dob'] = $dob;
            $_SESSION['country'] = $country;
            $_SESSION['state'] = $state;
            $_SESSION['message'] = 'Your profile is updated!';
            $_SESSION['type'] = 'alert-success';
            header('location: updateprofileform.php');
        } else {
            $_SESSION['error_msg'] = "Database error: Could not update user";
        }
    }

这是配置文件page.php,只是指出错误的一小段代码

<?php
require_once 'controllers/authController.php';

// If the session variable is empty, this  
// means the user is yet to login 
// User will be sent to 'login.php' page 
// to allow the user to login 
if (!isset($_SESSION['username'])) { 
    $_SESSION['msg'] = "You have to log in first"; 
    header('location: login.php'); 
} 

?>

      <p class="user-info">
        <b><span style="color:tomato;">Username : </span><?php echo $_SESSION['username']; ?></b>
        <br>
        <b><span style="color:tomato;">Gender : </span><?php echo $_SESSION['gender']; ?></b>
        <br>
        <b><span style="color:tomato;">DOB : </span><?php echo $_SESSION['dob']; ?></b>
        <br>
        <b><span style="color:tomato;">Country : </span><?php echo $_SESSION['country']; ?></b>
        <br>
        <b><span style="color:tomato;">State : </span><?php echo $_SESSION['state']; ?></b>
      </p>

它显示了我想使用会话变量显示值的性别,性别,国家,州的未定义索引错误

更多信息:

我已经在authController.php中启动了会话,并使用一次require将其链接到更新表单

以前,当我在进行此操作时没有问题,现在每当我在浏览器中打开配置文件页面时,它就会显示以下错误。当我提交带有详细信息的表格时,错误消失了。但是,如果我在实际服务器上使用此代码,则当用户第一次进入配置文件页面时会显示错误,我不希望这种情况发生。

我知道,如果未将值分配给变量,则会显示此错误,但我已分配了值 我不知道我要去哪里错了

有人可以帮我吗

谢谢

0 个答案:

没有答案