Page 1 of 1

Inserting Session Values into MySQL

Posted: Sun Nov 21, 2010 6:57 pm
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)."'
				)";

Re: Inserting Session Values into MySQL

Posted: Sun Nov 21, 2010 7:16 pm
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'])){

Re: Inserting Session Values into MySQL

Posted: Sun Nov 21, 2010 7:57 pm
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.

Re: Inserting Session Values into MySQL

Posted: Sun Nov 21, 2010 8:26 pm
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']')));

Re: Inserting Session Values into MySQL

Posted: Sun Nov 21, 2010 8:40 pm
by Jonah Bron

Code: Select all

$username = unserialize(base64_decode(stripslashes($_SESSION['username')));
Try it, just a guess.

Re: Inserting Session Values into MySQL

Posted: Sun Nov 21, 2010 8:45 pm
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

Re: Inserting Session Values into MySQL

Posted: Sun Nov 21, 2010 11:00 pm
by s992
What's the error you are getting with that code?

Re: Inserting Session Values into MySQL

Posted: Mon Nov 22, 2010 12:50 am
by brmcdani44
I am not getting an error. The user's user name is just not getting inserted into the database.