Jump to content

Cannot view the $fields unless i state select all , but it still does not work


Recommended Posts

function user_data($user_id){

 

Global $dbc;

 

  $data=array();

 

  $user_id = (int)$user_id;

 

  

 

  $func_num_args=func_num_args();

 

 $func_get_args=func_get_args();

 

if($func_num_args >=1){ 

 

unset($func_get_args[0]);

    

    $fields=' ` '.implode(' ` , ` ' , $func_get_args).' ` ';

 

 

 

 

 

 

 

  $data=mysqli_query($dbc," SELECT '$fields' FROM `users`  WHERE `user_id` = '$user_id'");

  

  //mysqli_error($dbc);

 

  if ($data === false) {

    die("error on mysqli_query: ". mysqli_error($dbc));


 

$data=mysqli_fetch_assoc($data);

 

    

 

print_r ($data);

 

 

  //return $data;

 

}

 

 

}

 

 

 

 

function logged_in(){

 

 

return(isset($_SESSION['user_id'])) ? true :false;

}

 

 

when i print above to screen i get this below 

Array ( [` user_id ` , ` username ` , ` password ` , ` name ` , ` surname ` , ` email ` ] => ` user_id ` , ` username ` , ` password ` , ` name ` , ` surname ` , ` email ` )

 

when i change the $fields in the in the query to * it displays the correct info 

 

but when i use the file below to echo a the user_id nothing echo's out 

 

this is the file that suppose to work but it doe not echo anything . this file is called init.php contains all functions ect.

 


<?php

 

session_start();

 

 

 

require('connect.php');

require('functions/general.php');

require('functions/users.php');

 

 

 

 

 

// to be accessed by every page to check errors for login

 

 

 

if(logged_in()===true){

 

 

$session_user_id=$_SESSION['user_id'];

 

$user_data = user_data($session_user_id,'user_id','username','password', 'name' , 'surname' ,'email' );

 

   echo $user_data['user_id'];

 

 

 

} else echo "cannot retreive data";


 

This is not a PHP Math problem posted in wrong place. But to answer you question

 

There are two problems with the user_data function, You're not constructing the SQL query properly

$data=mysqli_query($dbc," SELECT '$fields' FROM `users`  WHERE `user_id` = '$user_id'");

Remove the quotes around $fields, this will treat the value of $fields as a string not as individual fields listed within $fields.

 

The other problem is you need to uncomment (remove the //) the last line in user_data function 

//return $data;

Now your script should work as you expect.

 

Also please post your code within code tags (by hitting the <> button in the reply box) This makes your code alot clear to read

Edited by Ch0cu3r

This is not a PHP Math problem posted in wrong place. But to answer you question

 

There are two problems with the user_data function, You're not constructing the SQL query properly

$data=mysqli_query($dbc," SELECT '$fields' FROM `users`  WHERE `user_id` = '$user_id'");

Remove the quotes around $fields, this will treat the value of $fields as a string not as individual fields listed within $fields.

 

The other problem is you need to uncomment (remove the //) the last line in user_data function 

//return $data;

Now your script should work as you expect.

 

Also please post your code within code tags (by hitting the <> button in the reply box) This makes your code alot clear to read

thanks alot but it now says  

 

 error on mysqli_query: Unknown column ' user_id ' in 'field list'

You have spaces padding your field names. This is because of this line

$fields=' ` '.implode(' ` , ` ' , $func_get_args).' ` ';
It is padding your field names as `(space)field_name(space)`. Change the above line to

$fields='`'.implode('`, `', $func_get_args).'`';
Edited by Ch0cu3r
Field names shouldn't be in single quotes anyway.

 

 

Are you referring to the backticks in the code below?

$fields='`'.implode('`, `', $func_get_args).'`';

If so, they're necessary if a column is named "desc"...or one of the other reserved words in MySQL.

Are you referring to the backticks in the code below?

$fields='`'.implode('`, `', $func_get_args).'`';

If so, they're necessary if a column is named "desc"...or one of the other reserved words in MySQL.

thanks  it works now i see the correct field names and the values from the database but i cannot echo out the different field names , in this section nothing appears at all i been trying for over hour 

 

if(logged_in()===true){
 
 
$session_user_id=$_SESSION['user_id'];
 
$user_data = user_data($session_user_id,'user_id','username','password','name' ,'surname' ,'email');
 
   echo $user_data['user_id'];
 
 
 
} else echo "cannot retreive data";

Are you referring to the backticks in the code below?

$fields='`'.implode('`, `', $func_get_args).'`';

If so, they're necessary if a column is named "desc"...or one of the other reserved words in MySQL.

Must clean my glasses!

I have marked the topic as solved. If you need anything else, please mark it as unsolved...or start a new topic.  :happy-04:

another problem has come up for me, i am abit clueless now. i registered another user on the data base but all the database is recognizing is my user id not the new one . the new one logs registers and logs in on the page but it does not display correct username , it thinks it is mine all the time 

 

function user_data($user_id){
 
Global $dbc;
 
  $data=array();
 
  $user_id = (int)$user_id;
 
  
 
  $func_num_args=func_num_args();
 
 $func_get_args=func_get_args();
 
if($func_num_args >=1){ 
 
unset($func_get_args[0]);
    
 $fields='`'.implode('`, `', $func_get_args).'`';
 
 
 
 
 
 
 
  $data=mysqli_query($dbc," SELECT $fields FROM `users`  WHERE `user_id` = $user_id ");
  
  //mysqli_error($dbc);
 
  if ($data === false) {
    die("error on mysqli_query: ". mysqli_error($dbc));
 
$data=mysqli_fetch_assoc($data);
 
    
 
 
//print_r ($data);
 
 
  return $data;
 
}
 
 
}
 
 
 
 
function logged_in(){
GLOBAL $dbc;
 
return(isset($_SESSION['user_id'])) ? true : false;
 
 
 
}
 
 
 
 
function user_exists($username){
      
     GLOBAL $dbc;
$username=sanatize($username);
 
 
$query= mysqli_query($dbc,"SELECT COUNT(`user_id`) FROM `users`  WHERE `username` = '$username' ");
 
 
 
$check= mysqli_fetch_array( $query , MYSQLI_BOTH);
 
return ($check[0]==1)?true:false;
 
}
 
 
function email_exists($email){
      
     GLOBAL $dbc;
$email=sanatize($email);
 
 
$query= mysqli_query($dbc,"SELECT COUNT(`user_id`) FROM `users`  WHERE `email` = '$email' ");
 
 
 
$check= mysqli_fetch_array( $query , MYSQLI_BOTH);
 
return ($check[0]==1)?true:false;
 
}
 
//its suppose to  count the user_id but count didnt work here i am not sure why
 
function user_id_from_username($username){
 
GLOBAL $dbc;
   
   $username = sanatize($username);
 
 
    $query= mysqli_query($dbc,"SELECT `user_id` FROM `users`  WHERE `username` = '$username' ");
    $check= mysqli_fetch_array( $query , MYSQLI_BOTH);
 
    return  $check[0]=='user_id';
     
 
 
 
}
 
 
 
Edited by h1234

Please when posting code, paste it into


tags (click the <> button before posting message).

 

How you should log in a user is to query the database with the user/password credentials they provided. The query should retrieve all their data you need when the username/password matches.  Example query would be

SELECT user_id, password, email, time_of_last_login, access_lvl, etc.. FROM users_table WHERE username=$username AND password=$password

When the username/password matches it'll return all the info in the SELECT clause. You store this information to the $_SESSION variable. You should not be having to re-query the database to get more information about them. 

 

To see if they are logged in you just checked the $_SESSION variable validates to your requirements. The basic way to do this is to set a $_SESSION['is_logged_in'] variable to true when the user successfully logs in. For any page that requires the user to be logged in you check this variable is true eg

<?php
session_start();

if(isset($_SESSION['is_logged_in']) && $_SESSION['is_logged_in'] === true)
{
    // user is logged in display page
}
else
{
    // user is not logged in
    // display error and login form
}

When logging users out you just clear the $_SESSION variable with unset.  To completly remove the session you'd use session_destroy.

Please when posting code, paste it into


tags (click the <> button before posting message).

 

How you should log in a user is to query the database with the user/password credentials they provided. The query should retrieve all their data you need when the username/password matches.  Example query would be

SELECT user_id, password, email, time_of_last_login, access_lvl, etc.. FROM users_table WHERE username=$username AND password=$password

When the username/password matches it'll return all the info in the SELECT clause. You store this information to the $_SESSION variable. You should not be having to re-query the database to get more information about them. 

 

To see if they are logged in you just checked the $_SESSION variable validates to your requirements. The basic way to do this is to set a $_SESSION['is_logged_in'] variable to true when the user successfully logs in. For any page that requires the user to be logged in you check this variable is true eg

<?php
session_start();

if(isset($_SESSION['is_logged_in']) && $_SESSION['is_logged_in'] === true)
{
    // user is logged in display page
}
else
{
    // user is not logged in
    // display error and login form
}

When logging users out you just clear the $_SESSION variable with unset.  To completly remove the session you'd use session_destroy.

thanks  alot but when i log in with user 2  it recognizes but it does not recognise the users id so it displays as user 1 , for example when i echo the user id to say "hello" its suppose to say "hello user2" but always displays user1 name. 

 

like <?php  echo $user_data['name'];?>  this does not work. I am not sure why. i cant set session again or it gives an error that the sesssion is already set

Edited by h1234

 i cant set session again or it gives an error that the sesssion is already set

Sounds like you're calling session_start() multiple times. You only need to call this function once to start the session not each time you need to modify the $_SESSION vars.

 

You can set or reset the $_SESSION variables as many times as you like.

Sounds like you're calling session_start() multiple times. You only need to call this function once to start the session not each time you need to modify the $_SESSION vars.

 

You can set or reset the $_SESSION variables as many times as you like.

thanks so u suggest i save session start in a different file? as i have it in my confiq file with my  database connections , functions ect.

Edited by h1234

Just make sure you call it once right at the top of each script that uses session variables

thanks so this would solve my problem of  the user ID always shows 1 it never shows user 2 for example. it recognizes the user exists but does not recognize that specific  users name or id. i get this error

 

Fatal error: Call to undefined function logged_in()

Edited by h1234
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.