biondizzle Posted April 7, 2011 Share Posted April 7, 2011 Kind of a n00b here. on my main table (users) i named a column as "id", set it to auto-increment and as the primary key and created it like this: CREATE TABLE `users` ( `id` int(20) NOT NULL auto_increment, `full_name` varchar(200) collate latin1_general_ci NOT NULL default '', `user_name` varchar(200) collate latin1_general_ci NOT NULL default '', `user_pwd` varchar(200) collate latin1_general_ci NOT NULL default '', `user_email` varchar(200) collate latin1_general_ci NOT NULL default '', `activation_code` int(10) NOT NULL default '0', `joined` date NOT NULL default '0000-00-00', `country` varchar(100) collate latin1_general_ci NOT NULL default '', `user_activated` int(1) NOT NULL default '0', PRIMARY KEY (`id`) ) On the second table i created it like this: CREATE TABLE about_me ( about_id int NOT NULL, nick_name varchar(255), descript varchar(255), aim varchar(255), cell varchar(255), school varchar(255), music varchar(255), aspire varchar(255), City varchar(255), id int, PRIMARY KEY (about_id), FOREIGN KEY (id) REFERENCES users(id) ) I believe i imported the key correctly into my new table (about_me). Well I expected the id column to cascade down into this new table automatically which it didn't. RIght now if you log into my site and use the about me form, it posts to the new table "about_me" but it doesn't identify the user on the table with the primary key assigned to him from the first table (users). How do I use PHP sessions to identify the user by his/her id from the primary key in the table. I attached the whole site. The php for the log in is a prefab and I'm attempting to do the about me part on my own, I'm having alot of trouble with the whole sessions thing. I'm not really sure if I'm doing this correctly. so yeah any point in the right direction would be awesome! -Mike [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/232929-php-sessions-help/ Share on other sites More sharing options...
3raser Posted April 7, 2011 Share Posted April 7, 2011 So you want their session ID to be added into about_me? To use the code below, make sure you add another column called session_id, or anything of your choice. <?php session_start(); if (!isset($_SESSION['user'])) { header("Location: login.php"); } include ('dbc.php'); if ($_POST['Submit'] == 'Post') $nick_name = $_POST['nick_name']; $descript = $_POST['descript']; $aim = $_POST['aim']; $cell = $_POST['cell']; $school = $_POST['school']; $music = $_POST['music']; $aspire = $_POST['aspire']; $city = $_POST['city']; mysql_query("INSERT INTO about_me (nick_name, descript, aim, cell, school, music, aspire, city) VALUES (null, '$nick_name', '$descript', '$aim', '$cell', '$school', '$music', '$aspire', '$city', '". $_SESSION['user'] ."')") or die(mysql_error()); echo("Profile Updated Successfully!"); exit; ?> Quote Link to comment https://forums.phpfreaks.com/topic/232929-php-sessions-help/#findComment-1198002 Share on other sites More sharing options...
btherl Posted April 7, 2011 Share Posted April 7, 2011 What I usually do is set $_SESSION['user_id'] = $user_id; when the user logs in. Then you need to put $_SESSION['user_id'] into the id column when you insert into the about_me table. Quote Link to comment https://forums.phpfreaks.com/topic/232929-php-sessions-help/#findComment-1198003 Share on other sites More sharing options...
spiderwell Posted April 7, 2011 Share Posted April 7, 2011 as per the last post, slot it in here on the login.php: list($user_id,$user_email) = mysql_fetch_row($result); // this sets variables in the session $_SESSION['user']= $user_email; $_SESSION['id']= $user_id [\code] then on the about me insert, do as Justin posted only substitute $_SESSION['user'] with $_SESSION['id'] personally i think emails work just as well as a unique identifier so you could still do it with the email address. Quote Link to comment https://forums.phpfreaks.com/topic/232929-php-sessions-help/#findComment-1198008 Share on other sites More sharing options...
biondizzle Posted April 7, 2011 Author Share Posted April 7, 2011 Damn you guys are fast on here! Thanks for getting back. Going to try this now, let you know what happens. Quote Link to comment https://forums.phpfreaks.com/topic/232929-php-sessions-help/#findComment-1198012 Share on other sites More sharing options...
spiderwell Posted April 7, 2011 Share Posted April 7, 2011 if the relationship between tables is 1 to 1, is there really any need to have a second table? thats what i am thinking now, that said maybe its more efficient to have a small table for logging in....... Quote Link to comment https://forums.phpfreaks.com/topic/232929-php-sessions-help/#findComment-1198015 Share on other sites More sharing options...
3raser Posted April 7, 2011 Share Posted April 7, 2011 as per the last post, slot it in here on the login.php: list($user_id,$user_email) = mysql_fetch_row($result); // this sets variables in the session $_SESSION['user']= $user_email; $_SESSION['id']= $user_id [\code] then on the about me insert, do as Justin posted only substitute $_SESSION['user'] with $_SESSION['id'] personally i think emails work just as well as a unique identifier so you could still do it with the email address. Why $_SESSION['id']? I looked in his login.php and it showd $_SESSION['user'] Quote Link to comment https://forums.phpfreaks.com/topic/232929-php-sessions-help/#findComment-1198017 Share on other sites More sharing options...
spiderwell Posted April 7, 2011 Share Posted April 7, 2011 $_SESSION['user'] is holding an email address, and therefore cannot be inserted into an integer field in a database, whereas the ID can Quote Link to comment https://forums.phpfreaks.com/topic/232929-php-sessions-help/#findComment-1198019 Share on other sites More sharing options...
3raser Posted April 7, 2011 Share Posted April 7, 2011 $_SESSION['user'] is holding an email address, and therefore cannot be inserted into an integer field in a database, whereas the ID can Ah, didn't see that. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/232929-php-sessions-help/#findComment-1198021 Share on other sites More sharing options...
biondizzle Posted April 7, 2011 Author Share Posted April 7, 2011 worked perfect, i adjusted the about me code a touch i used <?php session_start(); if (!isset($_SESSION['id'])) { header("Location: login.php"); } include ('dbc.php'); if ($_POST['Submit'] == 'Post') $nick_name = $_POST['nick_name']; $descript = $_POST['descript']; $aim = $_POST['aim']; $cell = $_POST['cell']; $school = $_POST['school']; $music = $_POST['music']; $aspire = $_POST['aspire']; $city = $_POST['city']; mysql_query("INSERT INTO about_me (nick_name, descript, aim, cell, school, music, aspire, city, id) VALUES ('$nick_name', '$descript', '$aim', '$cell', '$school', '$music', '$aspire', '$city', '". $_SESSION['id'] ."')") or die(mysql_error()); echo("Profile Updated Successfully!"); exit; ?> brought in the id from the user table all well and good. any knowledge i know on php is from google and that w3schools website lol. While I was making this one of my friends who went to college for sql and php was kind of a dick and like "ohh you need php sessions and its alot of work, you'll never understand it", by you guys showing me that it really helped and it was alot easier than originally thought. Thanks so much! ohh and as for why i created 2 tables, he also told me that for each section i should create a new table and use the primary key from the original table so all the information "cascades" as its changed. thanks so much! it makes sense now! Quote Link to comment https://forums.phpfreaks.com/topic/232929-php-sessions-help/#findComment-1198025 Share on other sites More sharing options...
spiderwell Posted April 7, 2011 Share Posted April 7, 2011 don't listen to your friend or anyone when it comes to how hard coding is, I find its different for everyone learning, and for me, PHP was VERY easy to learn, picked it up in about a week, but thats with a background of scripting in ASP for many years. For me, its all down to logic, and if you remember that its pretty easy really. THe problem I have I dont know half the commands available in PHP, and am probably missing some great little features. Also when i have 2 tables that share a unique ID, i tend to make the second table with an ordinary incrementing ID as the primary key and another column with the foreign key (although I am very bad at actually making it a foreign key, i just name it user_ID or something that indicates its an ID from another table) oh and everytihng I have ever learnt in code has pretty much come via google!! I bought the odd book years ago when I first started out, but the rest is just out there waiting to be discovered Quote Link to comment https://forums.phpfreaks.com/topic/232929-php-sessions-help/#findComment-1198127 Share on other sites More sharing options...
biondizzle Posted April 7, 2011 Author Share Posted April 7, 2011 don't listen to your friend or anyone when it comes to how hard coding is, I find its different for everyone learning, and for me, PHP was VERY easy to learn, picked it up in about a week, but thats with a background of scripting in ASP for many years. For me, its all down to logic, and if you remember that its pretty easy really. THe problem I have I dont know half the commands available in PHP, and am probably missing some great little features. Also when i have 2 tables that share a unique ID, i tend to make the second table with an ordinary incrementing ID as the primary key and another column with the foreign key (although I am very bad at actually making it a foreign key, i just name it user_ID or something that indicates its an ID from another table) oh and everytihng I have ever learnt in code has pretty much come via google!! I bought the odd book years ago when I first started out, but the rest is just out there waiting to be discovered Yeah man it's true, php was alot easier than I originally intended it to be. I used to program in VB like 6 years ago when making starcraft hacks was cool, once I got my hands on it it came quite easily to me. Now I just gotta worry about how well my coding holds up security wise. Quote Link to comment https://forums.phpfreaks.com/topic/232929-php-sessions-help/#findComment-1198426 Share on other sites More sharing options...
spiderwell Posted April 7, 2011 Share Posted April 7, 2011 yeah its like 10% building and 90% making it unbreakable. I am sure it will come back to you quickly! Quote Link to comment https://forums.phpfreaks.com/topic/232929-php-sessions-help/#findComment-1198428 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.