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

未定义用户标识

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 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();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容