PHP Session Variable and MySQL INSERT INTO Trouble

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
princeofvegas
Forum Newbie
Posts: 11
Joined: Wed Jun 30, 2010 1:21 am

PHP Session Variable and MySQL INSERT INTO Trouble

Post by princeofvegas »

I have a photo upload script that I am working on and I have never run into this problem before. Everything on the script works fine, however on the insert into command the business_id session variable is not being posted to the database. It is showing up as empty. I do not know what I am doing wrong and as far as I see it should work. Any help would be appreciated.

Code: Select all

<?php
session_start();
include('../includes/database.php');
function genRandomString() {
    $length = 128;
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*';
    $string = ”;    
    for ($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, strlen($characters))];
    }
    return $string;
}

$random =  genRandomString();
$randommd5 = md5($random);
$path_parts = pathinfo($_FILES['uploadfile']['name']);
$extension = $path_parts['extension'];
$newfile = $randommd5 . "." . $extension;
if($_FILES['uploadfile']['size'] > 1048576){
	echo "error file size > 1 MB";
	unlink($_FILES['uploadfile']['tmp_name']);
	exit;
}
if($_FILES['uploadfile']['error'] >= 1){
	echo "unknown error";
        unlink($_FILES['uploadfile']['tmp_name']);
	exit;
}
if(move_uploaded_file($_FILES['uploadfile']['tmp_name'], "../uploads/$newfile")){
mysql_query("INSERT INTO account_pictures (business_id, picture) VALUES ('{$_SESSION['business_id']}','$newfile')");
echo "success"; 
}
?>
princeofvegas
Forum Newbie
Posts: 11
Joined: Wed Jun 30, 2010 1:21 am

Re: PHP Session Variable and MySQL INSERT INTO Trouble

Post by princeofvegas »

I was able to get it working by passing the business_id as a variable to the page like upload.php?business_id=XX and changing the mysql_query from:

Code: Select all

mysql_query("INSERT INTO account_pictures (business_id, picture) VALUES ('{$_SESSION['business_id']}','$newfile')");
to:

Code: Select all

mysql_query("INSERT INTO account_pictures (business_id, picture) VALUES ('{$_GET['business_id']}','$newfile')");
I am still curious as to why this does not work with the session variable.
giov85
Forum Newbie
Posts: 3
Joined: Tue Dec 14, 2010 10:56 am

Re: PHP Session Variable and MySQL INSERT INTO Trouble

Post by giov85 »

how about if you print the $_SESSION outside of the query? it is set or not?
maybe the session id is not correct pass...
Post Reply