Jump to content

[SOLVED] Registering an 'id' as part of a session


ferpadro

Recommended Posts

Hi everybody! Im fairly new to the PHP world. Recently ran onto a problem and came here hoping someone can help me.

This is my situation: im using sessions to registrate separately each user logs into my system. I register each user´s name and password in $_SESSION['username'] and $_SESSION['password'] respectively. In my database i have a table called "User" with these fields:

 

userid

username

password

role

 

I ran into a problem in where i needed to insert in another table a new row with the value of the id of the user that is logged in to the system in that time. So i came up with this idea:

 

session_register('username');

session_register('password');

session_register('userid');

$_SESSION['username']= $_POST['user'];

$_SESSION['password'] = $_POST['pass'];

$sql = "SELECT userid FROM User WHERE (username = '$_SESSION[username]' AND password = '$_SESSION[password]')";

$result=mysql_query($sql);

$_SESSION['id'] = '$result'

 

I thought it was fairly easy, but it didn´t work in first attempt. I "manually debugged" the code adding some echo "$result" but it throws me something like "resourse id #7", and i don´t know what is this.

If someone can fix this piece of code or at least give me a hint, it will be greatly appreciated

 

Thanks in advance ^^

Link to comment
Share on other sites

$sql = "SELECT userid FROM User WHERE (username = '$_SESSION[username]' AND password = '$_SESSION[password]')";
$result=mysql_query($sql);
$_SESSION['id'] = '$result'

 

Should be:

 

$sql = "SELECT userid FROM User WHERE (username = '$_SESSION[username]' AND password = '$_SESSION[password]')";
$result=mysql_query($sql) or die(mysql_error().' query: '.$sql;
$row = mysql_fetch_assoc($result);
$_SESSION['id'] = $row['userid'];

Link to comment
Share on other sites

Here a little tutoral just wrote for u.

 

database_connection.php

<?php
$db=mysql_connect("localhost","username","password");
mysql_select_db("database_name",$db);
?>

 

login result.php

 

<?php session_start(); // <<< start the session


//add database.
include("database_connection");

// let say the user submitted the form with the value 
//username and password.


// submit the form with the button named as submit.

if(isset($_POST['submit'])){

//valadate username and password was submitted.

if($_POST['username'] && $_POST['password']){


// query the database that the user is in the database.

$query="select * from users where username='$username' and password='$password'";
$result=mysql_query($query);

// loop throw the database 

while($rec=mysql_fetch_assoc($result)){


// if the user exists in the database

if(mysql_num_rows($result==1)){


//create a session from the database.	

$_SESSION['username']=$rec['username'];
$_SESSION['password']=$rec['password'];

//send new user to members page.

header("location: members_page.php");

}else{

//if user not in the database echo message.

echo" Sorry please 
<a href='login_page.php'>register</a> 
or 
<a href='register.php'>try agin</a>
<br> We dont have your deatals in our database<br>";
exit;
}
}
 }else{
 	
// if the user didnt fill in all the form echo message.

 	echo"Sorry please go 
 	<a href='login.php'>back</a>
 	fill in usernae and password";
 	exit;
 }
  }
  
?>

 

 

members_page.php

 

<?php session_start(); //<< start the session.

//add database.
include("database_connection");

// This will echo the current session username and password
echo "Username: ".$_SESSION['username']." <BR><BR>  ".$_SESSION['password']." ";

// if we wanted to get the user's id from the database.

$query="select * from users where username=".$_SESSION['$username']." and password=".$_SESSION['$password']." ";
$result=mysql_query($query);

// loop throw the database 

while($rec=mysql_fetch_assoc($result)){

echo $rec['user_id'];
}

?>

Link to comment
Share on other sites

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.