-5

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
}
?>
  • 4
    *"profile.php?`id`="* and you're using `where user_id` - *ahem* – Funk Forty Niner Jun 27 '14 at 15:32
  • 2
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](http://j.mp/XqV7Lp). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – esqew Jun 27 '14 at 15:34
  • in my database the id name is user_id and I removed the mysql functions and still getting the error The user ID is not defined. – user3783761 Jun 27 '14 at 15:43
  • You need to use `profile.php?user_id` and not `profile.php?id` as per your column name and `$id = intval($_GET['user_id']);` – Funk Forty Niner Jun 27 '14 at 15:45
  • ok I see but I'm still getting the error her is the list of users – user3783761 Jun 27 '14 at 15:53
  • On your users.php I see a link with profile.php?id= so on your profile.php change $_GET['user_id']; to $_GET['id']; ?? – Malcolm Jun 27 '14 at 16:06
  • my bad its profile.php?id= – user3783761 Jun 27 '14 at 16:31
  • You still need to change $_GET['user_id']; to $_GET['id']; on profile.php or change profile.php?id= to profile.php?user_id= on users.php. The value straight after the ? i.e profile.php?user_id= is the value you look for in $_GET['user_id']; – Malcolm Jun 27 '14 at 16:34
  • I changed all of it and it still won't work for some reason – user3783761 Jun 27 '14 at 16:40
  • Is $dnn['user_id']; getting a value on users.php? – Malcolm Jun 27 '14 at 16:43
  • now its saying This user dont exists. – user3783761 Jun 27 '14 at 16:48
  • here is user.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)) { ?> – user3783761 Jun 27 '14 at 16:49
  • here is profile.php //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 id="'.$id.'"'); if(mysql_num_rows($dn)>0) { $dnn = mysql_fetch_array($dn); //We display the user datas ?> – user3783761 Jun 27 '14 at 16:50
  • OK this works for me " " – Malcolm Jun 27 '14 at 17:07
  • on profile.php?user_id=10 I'm getting this error Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\xampp\root\www\profile.php on line 11 This user dont exists. – user3783761 Jun 27 '14 at 17:23

1 Answers1

0

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();
}
meda
  • 45,103
  • 14
  • 92
  • 122