Jump to content
Html

user id

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

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

I was just thinking about this and had a question - specifically which parts of the book HAVE you read?

  • Like 1

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

For every '{' there must be an equal '}' and '{' usually follows an if(), foreach(), while() where code is executed within opening'{' and closing ')' specific to condition or loop.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

The php 7 basic book is just that

But you don't know the basics, so it sounds like that's exactly what you need to read.

Share this post


Link to post
Share on other sites
Quote

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

The code you showed is wrong! compared to actual code in the forum.php file, they are not providing none working, wrong code! YOU ARE! cause you don't know the basics.

Share this post


Link to post
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.

Share this post


Link to post
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!

Share this post


Link to post
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

×