gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 18:08:47 25 4
gpt4 key购买 nike

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

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

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

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.';
}
?>

感谢您的帮助。

最佳答案

我找到了解决方案。我只得删除

$row = mysqli_fetch_array($result,MYSQLI_ASSOC);

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

关于Php 获取当前用户的 id 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47580937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com