ferpadro Posted September 14, 2007 Share Posted September 14, 2007 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 ^^ Quote Link to comment https://forums.phpfreaks.com/topic/69278-solved-registering-an-id-as-part-of-a-session/ Share on other sites More sharing options...
redarrow Posted September 14, 2007 Share Posted September 14, 2007 can you post your whole page please cheers. should be using session[''] not session_register it out dated now. Quote Link to comment https://forums.phpfreaks.com/topic/69278-solved-registering-an-id-as-part-of-a-session/#findComment-348135 Share on other sites More sharing options...
Jessica Posted September 14, 2007 Share Posted September 14, 2007 $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']; Quote Link to comment https://forums.phpfreaks.com/topic/69278-solved-registering-an-id-as-part-of-a-session/#findComment-348136 Share on other sites More sharing options...
ferpadro Posted September 14, 2007 Author Share Posted September 14, 2007 WOW Thanks a lot Jesirose, that worked like a charm!!!! And thanks for the fast reply! ^^ Quote Link to comment https://forums.phpfreaks.com/topic/69278-solved-registering-an-id-as-part-of-a-session/#findComment-348139 Share on other sites More sharing options...
Jessica Posted September 14, 2007 Share Posted September 14, 2007 No problems Quote Link to comment https://forums.phpfreaks.com/topic/69278-solved-registering-an-id-as-part-of-a-session/#findComment-348141 Share on other sites More sharing options...
redarrow Posted September 14, 2007 Share Posted September 14, 2007 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']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/69278-solved-registering-an-id-as-part-of-a-session/#findComment-348142 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.