donglangtun1850
donglangtun1850
2014-06-27 15:31
浏览 66
已采纳

未定义用户标识

Ok I'm trying to list all the users in mysql and that's working but when I add a profile.php?id= I get my else code The user ID is not defined. how do i set it so it wont say The user ID is not defined. here is my code:

<?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条回答 默认 最新

  • doulu8341
    doulu8341 2014-06-27 18:59
    已采纳

    You query is returning false on the following line

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

    You will need to find out why, you should not attempt anything untill it returns 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();
    }
    
    点赞 评论

相关推荐