Page 1 of 1

PHP Session Variable and MySQL INSERT INTO Trouble

Posted: Wed Dec 15, 2010 5:29 pm
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"; 
}
?>

Re: PHP Session Variable and MySQL INSERT INTO Trouble

Posted: Wed Dec 15, 2010 5:48 pm
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.

Re: PHP Session Variable and MySQL INSERT INTO Trouble

Posted: Thu Dec 16, 2010 4:27 am
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...