Jump to content

PHP sessions help


biondizzle

Recommended Posts

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]

Link to comment
Share on other sites

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;
?> 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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']

Link to comment
Share on other sites

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.  :D

 

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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.