【发布时间】:2017-05-19 11:28:26
【问题描述】:
我正在尝试创建一个用户个人资料页面,而我想要的是如何从数据库中回显用户信息以在他们从登录页面登录后显示在个人资料页面中,但问题是会回显用户 ID 未定义。我需要帮助任何人都可以帮助我修复我的代码,这是 php 和 sql 的新手。
profile.php
<?php
include('db.php');
?>
<!DOCTYPE html">
<html>
<head>
<title>Profile of an user</title>
</head>
<body>
<div class="content">
<?php
//We check if the users ID is defined
if(isset($_GET['id']))
{
$id = intval($_GET['id']);
//We check if the user exists
$sql = mysql_query('SELECT fst, las, uid, pass,sts,ocp FROM users WHERE id="'.$id.'"');
if(mysql_num_rows($sql)>0)
{
$res = mysql_fetch_array($sql);
//We display the user datas
?>
This is the profile of "<?php echo htmlentities($res['fst']); ?>" :
<table style="width:500px;">
<tr>
<td class="left"><h1><?php echo htmlentities($res['fst']); ?></h1>
Email: <?php echo htmlentities($dnn['las']); ?><br />
This user joined the website on <?php echo htmlentities($res['uid']); ?></td>
</tr>
</table>
<?php
}
else
{
echo 'This user dont exists.';
}
}
else
{
echo 'The user ID is not defined.';
}
?>
</div>
</body>
</html>
登录.php
<?php
include 'db.php';
$uid = $_POST['uid'];
$pass = $_POST['pass'];
$sql = "SELECT * FROM users WHERE uid='$uid' AND pass='$pass'";
$result = mysqli_query($conn,$sql);
if($row = mysqli_fetch_assoc($result)){
header("Location: profile.php");
}else{
echo "invalid username or password";
}
?>
【问题讨论】:
-
以上代码有什么问题?
-
it echo 用户ID未定义。
-
为什么要混用 mysqli_ 和 mysql_ 函数?不要使用 mysql_ 它已被弃用。
-
我把它改成mysqli_,但还是有问题
-
@CFrancis 试试下面的答案