Jump to content

user id


Html

Recommended Posts

I don't quite know what to state to where does the $user_id originates from, in the db it is simply user_id, so neither that or $id work in the profile code.

1 or 2 is from the db as the example image I've uploaded displays, so the code above has to read one of those numbers of the user logged in. It is that easy I'm sure, but I can't see it.

db.jpg

Link to comment
Share on other sites

You have syntax errors in your echo line, the quotes aren't right.  You need to fix that for the code to run at all.  You also have a return statement, do you know what return does?  If you don't, look it up.  Importantly, it stops code execution.  

You really need to re-read the sections in the book on variables.  You're trying to use a variable called $user_id, but you never create the variable and set a value for it.  You have the user ID available, you're just not using it, instead you're trying to use a non-existent variable called $user_id.  The name doesn't matter, either, it doesn't matter that your database has a column called user_id if you're thinking there's some sort of connection there.  There is no connection.  The names are arbitrary.  If you don't create a variable called $user_id and set a value, then it doesn't exist.  There's no magic to that, it's not complex - if you don't create it, it doesn't exist.

Link to comment
Share on other sites

<?php
ini_set('display_errors', 1); 
error_reporting(E_ALL);
# Access session.
session_start() ; 

# Redirect if not logged in.
if ( !isset( $_SESSION[ 'user_id' ] ) ) { require ( 'login_tools.php' ) ; load() ; }

# On success retrieve user_id, first_name, and last name from 'users' database.
  if ( empty( $errors ) ) 
  {
    $q = "SELECT user_id, first_name, last_name FROM users WHERE email='$user_id'" ;  
    $r = mysqli_query ( $dbc, $q ) ;
    if ( @mysqli_num_rows( $r ) == 1 ) 
    {
      $row = mysqli_fetch_array ( $r, MYSQLI_ASSOC ) ;
     
 {
	echo '<tr><td>' . 
	$row['first_name'] .' '. 
	$row['last_name'] . '<br>'. 
	</td><td>' . 
	</td> </tr>';
  }

  echo '</table>' ;

    }

?>

I'm not sure about that, bare in mind I only have the php 7 book which has a chapter on variables, never read that part, from what I can gather it mentions user, but nothing about a $user, you state it doesn't matter that much. In the defining constants page,

'USER', 'Mike');

echo 'Hello' . USER ; Hello how is your puppy is the statement example. You hare using php 7.0.4

Link to comment
Share on other sites

That is setting up a constant

define('USER',Mike);
echo 'Hello' . USER.' how is your puppy';

Output: Hello Mike how is your puppy. NOTHING about your current problem you are trying to solve.

1) you login with username and password.

2) you are authenticated, AND YOUR user id FROM database table is  SAVED to a SESSION['user_id']

3) Any page you go to that has session_start(); means you can access any predefined session of specific key name(user_id) and access its value.

4) You want to access specific record from database table to retrieve more details, this is always done using a id number which IS unique to specific records;

5) in SELECT query you will want to compare the databases table column/field name 'user_id' to a variable that contains a user id number, that will be assigned to variable that can be of any name $id, $q, $user_id, but which ever is used,  IT MUST have a value assigned to it before it gets to the SELECT query line.WHERE 'user_id' =

 I've giving you the clues, so where can it get the id number to assign a variable that will be used in the SELECT query statement.

Link to comment
Share on other sites

I only have the php 7 book which has a chapter on variables, never read that part

Well, you think that maybe you should start with the basics?  I mean, we've explicitly said multiple times in this and your other threads what you need to do and it doesn't sound like it's getting through.  You're obviously missing the basic information.

And, if you can't or don't want to configure PHP to display all errors in the config file, then copy and paste your code here to notice that it has an error which will stop any code from running, including those first couple lines that make sure that all error messages will be displayed:

https://www.piliapp.com/php-syntax-check/

If you don't fix that error then the code will not execute at all, there will only be an error message in an error log sitting ignored on your server somewhere.

Link to comment
Share on other sites

The only part I focused on was the comment box, or forum, which I simply just downloaded the files from the site, since I tried typing it all out, and it was a waste of time since it didn't work. So I took that short cut.

I then viewed the other files, and noticed I could use that login system as they were from a book called Php and mysql.

So yes, looked at the code, and took the forum part about inserting data, and used the user id part from the login and put them together, as displayed. And you've given me some pointers, which I'm struggling with.

The php 7 basic book is just that, it doesn't really go into explanations word for word. As for the Learning php book, no good really for me. That was why I preferred a video. Forget about that.

line 23 is an error according to that check system. I tried this, and now 30 is the problem.

  1. <?php
  2. ini_set('display_errors', 1); 
  3. error_reporting(E_ALL);
  4. # Access session.
  5. session_start() ; 
  6.  
  7. # Redirect if not logged in.
  8. if ( !isset( $_SESSION[ 'user_id' ] ) ) { require ( 'login_tools.php' ) ; load() ; }
  9.  
  10. # On success retrieve user_id, first_name, and last name from 'users' database.
  11.   if ( empty( $errors ) ) 
  12.   {
  13.     $q = "SELECT user_id, first_name, last_name FROM users WHERE email='$user_id'" ;  
  14.     $r = mysqli_query ( $dbc, $q ) ;
  15.     if ( @mysqli_num_rows( $r ) == 1 ) 
  16.     {
  17.       $row = mysqli_fetch_array ( $r, MYSQLI_ASSOC ) ;
  18.      
  19.  {
  20.         echo '<tr><td>' . 
  21.         $row['first_name'] .' '. 
  22.         $row['last_name'] . '<br>'. 
  23.         '</td> </tr>';
  24.   }
  25.  
  26.   echo '</table>' ;
  27.  
  28.     }
  29.  
  30. ?>

 

Edited by Html
Link to comment
Share on other sites

<?php
$page_title = 'forum'
include('includes/header.html');
require ('\connect_db.php');
$sql = 'SELECT * FROM forum';
$result = mysql_query($dbc, $sql);
if ( mysqli_num_rows($result)>0)
{
echo '<table><tr><th>Posted By</th>
<th>Subject</th><th id="msg">Message</th></tr>';
while ($rows=mysqli_fetch_array($result, MYSQLI_ASSOC))
{
echo '<tr><td>'.
$row['first_name'].''.
$row['last_name'].'<br>'.
$row['post_date'].'</td><td>'.
$row['subject'].''.
$row['message'].''.
}
echo'</table>';
else
{
echo '<p>There are currently no messages.</p>';
}

echo'<p><a href="post.php">Post Message</a></p>';
mysqli_close($dbc);
incluide('includes/footer.html');
?>

According to the forum code, there is one of those curly braces just under echo.

   if ( empty( $errors ) ) 
  {
    $q = "SELECT user_id, first_name, last_name FROM users WHERE email='$user_id'" ;  
    $r = mysqli_query ( $dbc, $q ) ;
    if ( @mysqli_num_rows( $r ) == 1 ) 
    {
      $row = mysqli_fetch_array ( $r, MYSQLI_ASSOC ) ;
     
 {
        echo '<tr><td>' . 
        $row['first_name'] .' '. 
        $row['last_name'] . '<br>'. 
        '</td> </tr>';
  }

 {
  echo '</table>' ;
 
 }
 
?> 

So this bottom part of profile was missing one of those, from your hint.

Link to comment
Share on other sites

Sure, I don't know the syntax and spacing of the code, as I found out when I typed out the forum tutorial page for page, and then you got me the files. So I got somewhere with that.

Okay, the code to get this to work is missing, and incorrect. Then this isn't quite as simple as I thought, you stated you left certain clues about the select query part.

<?php
ini_set('display_errors', 1); 
error_reporting(E_ALL);
# Access session.
session_start() ; 
 
# Redirect if not logged in.
if ( !isset( $_SESSION[ 'user_id' ] ) ) { require ( 'login_tools.php' ) ; load() ; }
 
# On success retrieve user_id, first_name, and last name from 'users' database.
  if ( empty( $errors ) ) 
  {
    $q = "SELECT user_id, first_name, last_name FROM users WHERE email='$user_id'" ;  
    $r = mysqli_query ( $dbc, $q ) ;
    if ( @mysqli_num_rows( $r ) == 1 ) 
    {
      $row = mysqli_fetch_array ( $r, MYSQLI_ASSOC ) ;
     
 {
        echo '<tr><td>' . 
        $row['first_name'] .' '. 
        $row['last_name'] . '<br>'. 
        '</td> </tr>';
  
 
  echo '</table>' ;
 }
    
 mysqli_close($dbc);
?> 

Not sure if that edit works.

Link to comment
Share on other sites

NO! it won't work! You haven't taken any notice about the curly braces number, if you have trouble counting, you can do a search on most better editors of '{' then '}' and the search will tell you on the total found. But! if you had a good editor, it would have highlighted the errors in the first place!

Quote

Not sure if that edit works.

Here's an idea, I'll just put it out there, TRY IT! AND FIND OUT!

Link to comment
Share on other sites

<?php
ini_set('display_errors', 1); 
error_reporting(E_ALL);
# Access session.
session_start() ; 
 
# Redirect if not logged in.
if ( !isset( $_SESSION[ 'user_id' ] ) ) { require ( 'login_tools.php' ) ; load() ; }
 
# On success retrieve user_id, first_name, and last name from 'users' database.
  if ( empty( $errors ) ) 
  {
    $q = "SELECT user_id, first_name, last_name FROM users WHERE email='$user_id'" ;  
    $r = mysqli_query ( $dbc, $q ) ;
    if ( @mysqli_num_rows( $r ) == 1 ) 
    
      $row = mysqli_fetch_array ( $r, MYSQLI_ASSOC ) ;
     
 
        echo '<tr><td>' . 
        $row['first_name'] .' '. 
        $row['last_name'] . '<br>'. 
        '</td> </tr>';
  
 
  echo '</table>' ;
 }
    
 mysqli_close($dbc);
?>

Okay the official php checker has given an okay with this.

 

 

Link to comment
Share on other sites

That code is syntactically correct, but it doesn't do what you think it does.

This reminds me of trying to actually speak a different language.  My wife speaks Portuguese and Finnish.  I feel like this is like me learning a few basic nouns and verbs and then trying to travel to Brazil or Finland and hold an entire conversation with someone without ever taking a class or reading a book, just watching a few videos of native speakers talking.

Other than the fact that the code will actually execute now, even though it would show several warnings, your SQL query isn't going to return any results (or, more specifically, it's going to return every user who doesn't have an email address).  Can you see why?

Since that code will actually run, I suggest you actually run it, look at the warnings from PHP, and try to figure out what the problems are.

Link to comment
Share on other sites

I managed to try out the file, web host is back up since last week, so yes, I tried out, and a bunch of errors, I tried tickering with it, and got this result, but yes it doesn't do what it should.

<?php
ini_set('display_errors', 1); 
error_reporting(E_ALL);
# Access session.
session_start() ; 
 
# Redirect if not logged in.
if ( !isset( $_SESSION[ 'user_id' ] ) ) { require ( 'login_tools.php' ) ; load() ; }
 
# On success retrieve user_id, first_name, and last name from 'users' database.
  if ( empty( $errors ) ) 
  {
    $user_id = "SELECT user_id, first_name, last_name FROM users WHERE email='$user_id'" ;  
    $r = mysqli_query ( $user_id, ) ;
    if ( @mysqli_num_rows( $r ) == 1 ) 
    
      $row = mysqli_fetch_array ( $r, MYSQLI_ASSOC ) ;
     
 
        echo '<tr><td>' . 
        $row['user_id'] .' '. 
        $row['last_name'] . '<br>'. 
        '</td> </tr>';
  
 
  echo '</table>' ;
 }
    
?>

 

Link to comment
Share on other sites

        $row['user_id'] .' '. 

The errors before tinkering with it were, lines 21, and 13, and 14.

$r = mysqli_query ( $user_id, ) ;

Once I changed them, the blank page was displayed.

8 hours ago, dsonesuk said:

The question remains, where is $user_id getting its value to compare with column email data, and why! If what the name of variable suggest a 'user id' NOT a email, are you comparing the user id with a email address? stored in column 'email'?

From the column in the db? email is just that email, user_id is exactly that as the image in the top post displays. Unless you are getting at the fact that there is no underscore in the actual column name?

Link to comment
Share on other sites

9 hours ago, justsomeguy said:

Paste the error messages

There's gotta be some error messages in that last lot of code you posted, Html.
I can see a syntax error in line 14, an undefined error in line 21. 

Copying the error messages helps us more than describing them.

Edited by Funce
Link to comment
Share on other sites

Notice: Undefined variable: user_id on line 13

Warning: mysqli_query() expects at least 2 parameters, 1 given on 14

Notice: Undefined variable: row on line 21

If I remove the comma, the error messages appear

( $user_id, )

 

And whether user_id or first name doesn't make any difference. I changed it back to first name.

Link to comment
Share on other sites

Arrrgg!

The minimum column names required in your database table name 'users' are user_id, first_name, last_name and email as used in your sql statement.

$q = "SELECT user_id, first_name, last_name FROM users WHERE email='$user_id'" ; (Note: The $user_id has been returned to original correct variable name of $q)

user_id 	first_name 	last_name 	email
1		 John 		Aaaaaa 	    	aaaa@aaaa.com
2		 Peter 		Bbbbbb 	    	bbbbb@bbbbb.com
3 		 Paul 		Ccccccc 	ccccc@cccc.com
4 		 Alan 		Dddddd 		ddddd@ddddd.com

There MUST! be a value added to $user_id variable, that what you currently have in SQL statement (which i think is wrong! but just in case its right!) Should be the value representing one of the email addresses.

aaaa@aaaa.com
bbbbb@bbbbb.com
ccccc@cccc.com
ddddd@ddddd.com

OR what i think is correct! with SQL statement as

$q = "SELECT user_id, first_name, last_name FROM users WHERE user_id='$user_id'" ;  where the $user_id will represent one of the values of 

1
2
3
4 	

Now where does $user_id get its value? Well in the if condition it checks if a value does not exist,

if ( !isset( $_SESSION[ 'user_id' ] ) ) { require ( 'login_tools.php' ) ; load() ; }

if it does not exist, process it through a function load(), in 'login_tools.php', what this does, i don't specifically don't know? but i would imagine it goes through the process of passing user validation and then setting the value for

$_SESSION[ 'user_id' ]

As a email address that

$q = "SELECT user_id, first_name, last_name FROM users WHERE email='$user_id'" ;

can work with.

OR as a user id reference that

$q = "SELECT user_id, first_name, last_name FROM users WHERE user_id='$user_id'" ;

will work with which I feel is most likely to most correct of the two.

In either case you will most likely be using

$user_id = $_SESSION['user_id'];

to check what type of value it will have try adding *

$user_id = $_SESSION['user_id'];

if (isset($user_id)) {
    echo '<div>We have a green light that $user_id is set and has a value <div style="width:1em; height: 1em; display: inline-block; background: lime;"></div> and equals: ' . $user_id . '</div>';
} else {
    echo 'We have a red light that $user_id is set, but has NO VALUE <div style="width:1em; height: 1em; display: inline-block; background: red;"> and equals: ' . $user_id . ' </div>';
}

under

if ( !isset( $_SESSION[ 'user_id' ] ) ) { require ( 'login_tools.php' ) ; load() ; }

Depending on it showing green box and type of value (email address or a integer number representing user_id, determine if you use

$q = "SELECT user_id, first_name, last_name FROM users WHERE email='$user_id'" ; 

or

$q = "SELECT user_id, first_name, last_name FROM users WHERE user_id='$user_id'" ; 

 

2 hours ago, Html said:

Notice: Undefined variable: user_id on line 13 

Warning: mysqli_query() expects at least 2 parameters, 1 given on 14

Notice: Undefined variable: row on line 21

If I remove the comma, the error messages appear

Line 13: undefined as it has not been defined or even holds a value *

line 14 need two parameters, you have given one only and YOU never leave it empty.

$r = mysqli_query ( $user_id, )

IT should be

$r = mysqli_query($dbc, $q);

Note: return back to original correct variables. $dbc is database connection, $q is the SQL statement (correct one to use TBD)

Line 21: If Line 14 is wrong, how do you expect it to work on line 21 when it relies on that line being correct!

 

Edited by dsonesuk
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...