未定义用户标识

时间:2014-06-27 15:31:24

标签: php mysql userid

好的我试图列出mysql中的所有用户并且正在工作但是当我添加profile.php?id=时我得到了我的其他代码我没有定义用户ID。我如何设置它所以它不会说用户ID没有定义。这是我的代码:

<?php
include('config/db.php');
?>
<?php
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="password"; // Mysql password 
$db_name="login"; // Database name 
$tbl_name="users"; // Table name 
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
//We check if the members ID is defined
if(isset($_GET['user_id']))
{
$id = intval($_GET['user_id']);
//We check if the user exists
$dn = mysql_query('select user_name, user_email from users where       user_id="'.$id.'"');
if(mysql_num_rows($dn)>0)
{
    $dnn = mysql_fetch_array($dn);
    //We display the user datas
?>
This is the profile of "<?php echo htmlentities($dnn['user_name']); ?>" :
<table style="width:500px;">
<tr>
    <td><?php
if($dnn['avatar']!='')
{
echo '<img src="'.htmlentities($dnn['avatar'], ENT_QUOTES, 'UTF-8').'" alt="Avatar"     style="max-width:100px;max-height:100px;" />';
}
else
{
echo 'This user dont have an avatar.';
}
?></td>
    <td class="left"><h1><?php echo htmlentities($dnn['user_name'], ENT_QUOTES, 'UTF-  8'); ?></h1>
    Email: <?php echo htmlentities($dnn['user_email'], ENT_QUOTES, 'UTF-8'); ?><br />
    This user joined the website on <?php echo date('Y/m/d',$dnn['signup_date']); ?>  </td>
</tr>
</table>
<?php
}
else
{
    echo 'This user dont exists.';
}
}
else
{
echo 'The user ID is not defined.';
}
?>
this is the users.php
    <?php
//We get the IDs, usernames and emails of members
$req = mysql_query('SELECT `user_id`, `user_name`, `user_email`, `user_sign_up_date`  FROM `users`');
while($dnn = mysql_fetch_array($req))
{
?>
<tr>
    <td class="left"><?php echo $dnn['user_id']; ?></td>
    <td class="left"><a href="profile.php?<?php echo $dnn['user_id']; ?>"><?php echo  ($dnn['user_name']); ?></a></td>
    <td class="left"><?php echo($dnn['user_email']); ?></td>
</tr>
<?php
}
?>

1 个答案:

答案 0 :(得分:0)

您的查询在以下行返回false

$dn = mysql_query('select user_name, user_email from users where user_id="'.$id.'"');

你需要找出原因,你不应该尝试任何东西,直到它返回true:

$sql = sprintf("SELECT user_name, user_email FROM users WHERE user_id = %s",
                mysql_real_escape_string($id));
$dn = mysql_query($sql);

if(!$dn){
  echo "mysql_query() failed";
  exit();
}