Inserting Session Values into MySQL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
brmcdani44
Forum Commoner
Posts: 26
Joined: Fri Oct 08, 2010 3:52 pm

Inserting Session Values into MySQL

Post by brmcdani44 »

I am trying to insert a users username into the user database when he or she inserts an event into a database using session variables. I am having trouble getting the desired result. The scripting I have now allows for the event to be posted, but the username just never gets passed over to the MySql table for some reason.

If anyone could help me out I would really appreciate it. I have just a snippet of the code of the event add form. It does include check.php even though it is not in the code. Here is what I have so far:

CHECK.PHP

Code: Select all

<?php

session_start(); 

if(isset('username')){
$_Session['username'];
// Session exists, allow the user to view the page. 

} else { 

// Session doesn't exist, redirect to login and exit the page.

header( "Location: login.php" ); 
exit();

} 

?> 
Event Add Form

Code: Select all

if(isset($_POST['add_event']) && $_GET['add'] == 'new') {

	// Get POST vars.
	$username 		= unserialize(base64_decode(stripslashes($_POST['$_Session['username']')));
	$name 		= unserialize(base64_decode(stripslashes($_POST['name'])));
	$desc 		= unserialize(base64_decode(stripslashes($_POST['desc'])));
	$location 	= unserialize(base64_decode(stripslashes($_POST['location'])));
	$date 		= unserialize(base64_decode(stripslashes($_POST['date'])));
	$from 		= unserialize(base64_decode(stripslashes($_POST['from']))).unserialize(base64_decode(stripslashes($_POST['from2'])));
	$until 		= unserialize(base64_decode(stripslashes($_POST['until']))).unserialize(base64_decode(stripslashes($_POST['until2'])));

	$d_for = explode('/', $date);
	
	$day = $d_for[0];
	$month = $d_for[1];
	$year = $d_for[2];
	
	if(!$name) { echo "<div class='error_message'>You must enter an event name</div>"; exit(); }
	if(!$desc) { echo "<div class='error_message'>Please enter an event description</div>"; exit(); }
	if(!$location) { echo "<div class='error_message'>You must enter a location for your event</div>"; exit(); }
	if(!$date) { echo "<div class='error_message'>Your event must have a date</div>"; exit(); }

	$sql = "INSERT INTO calendar_event (user, event, description, location, day, month, year, time_from, time_until)
				VALUES (
					'".mysql_real_escape_string($username)."', 
					'".mysql_real_escape_string($name)."', 
					'".mysql_real_escape_string($desc)."', 
					'".mysql_real_escape_string($location)."', 
					'".mysql_real_escape_string($day)."', 
					'".mysql_real_escape_string($month)."', 
					'".mysql_real_escape_string($year)."', 
					'".mysql_real_escape_string($from)."', 
					'".mysql_real_escape_string($until)."'
				)";
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Inserting Session Values into MySQL

Post by Jonah Bron »

This:

Code: Select all

if(isset('username')){
$_Session['username'];
Needs to be corrected to this:

Code: Select all

if(isset($_SESSION['username'])){
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Inserting Session Values into MySQL

Post by phphelpme »

yeah, it looks like you need to change it to:

Code: Select all

if(isset($_SESSION['username'])){
and it should work just fine. I use session ids all the time and save to database, all login information, session id and time stamp so I have a record of when each user has logged in and out etc. Just helps me maintain the system.
brmcdani44
Forum Commoner
Posts: 26
Joined: Fri Oct 08, 2010 3:52 pm

Re: Inserting Session Values into MySQL

Post by brmcdani44 »

Now I am getting an error with this line of code? Is this not the correct syntax?

$username = unserialize(base64_decode(stripslashes($_POST['$_Session['username']')));
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Inserting Session Values into MySQL

Post by Jonah Bron »

Code: Select all

$username = unserialize(base64_decode(stripslashes($_SESSION['username')));
Try it, just a guess.
brmcdani44
Forum Commoner
Posts: 26
Joined: Fri Oct 08, 2010 3:52 pm

Re: Inserting Session Values into MySQL

Post by brmcdani44 »

I tried that right before the last post reply. It did accept it but it still is not inserting the username. I am going to keep plugging! Thanks
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

Re: Inserting Session Values into MySQL

Post by s992 »

What's the error you are getting with that code?
brmcdani44
Forum Commoner
Posts: 26
Joined: Fri Oct 08, 2010 3:52 pm

Re: Inserting Session Values into MySQL

Post by brmcdani44 »

I am not getting an error. The user's user name is just not getting inserted into the database.
Post Reply