Php获取当前用户的id不工作

时间:2017-11-30 19:10:48

标签: php mysql logging xampp session-variables

我正在尝试创建一个可以登录的页面,然后更改您的昵称或/和密码。 mySQL数据库中的所有内容,但是当我尝试将id保存到会话变量时,它不起作用。有什么建议吗?

我正在使用XAMPP,用户是我在数据库用户中的表,我不会发布登录表单代码,因为它非常简单。

一切都已连接,代码不会发出任何警告或错误。

login.php(片段):

$sql = "SELECT * FROM users WHERE nickname = '$myusername' and pass = '$mypassword' and confirmed = 1";
  $result = mysqli_query($conn,$sql);
  $row = mysqli_fetch_array($result,MYSQLI_ASSOC);

  $count = mysqli_num_rows($result);

  // If result matched $myusername and $mypassword, table row must be 1 row

  if($count == 1) {

        $logged = true;

        while($row = mysqli_fetch_assoc($result)) {
            echo "id: " . $row["id"];
            $_SESSION['currentId'] = $row["id"];
            echo 'Id: ' . $_SESSION['currentId'];
        }
  }else {
     $error = "Your Login Name or Password is invalid";
  }
} 

change.php(整体):

<?php
session_start();
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "Users";
$currentId = $_SESSION['currentId'];

if($currentId<1){echo 'No Id.';}
else {echo 'CurrentId: ';
echo $currentId;}
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully <br>";

if($_SERVER["REQUEST_METHOD"] == "POST") {
  // username and password sent from form 

  $aCUname = mysqli_real_escape_string($conn,$_POST['CUname']);
  $aCUpass = mysqli_real_escape_string($conn,$_POST['CUpass']); 

  $sql = "UPDATE users SET nickname = '$aCUname', pass = '$aCUpass' WHERE id = '$currentId';";
  $result = mysqli_query($conn,$sql);
    echo 'Updated successfully.';
}
?>

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我有一个解决方案。我只是要删除

pager.branch

来自login.php。感谢@Shashikumar Misal!