Jump to content

Putting a unique auto increment id into a session variable without a queryt


Namtip

Recommended Posts

I've been trying to grab the unique auto incrementing table id and put into a session variable.

 

$name_id = mysql_insert_id();

$query = sprintf("INSERT INTO profile (
		 name_id, bio, ex)
	VALUES (LAST_INSERT_ID(),'%s','%s')",
mysql_real_escape_string($_SESSION['pro']['bio']),
mysql_real_escape_string($_SESSION['pro']['ex']));

$result = mysql_query($query, $db) or die(mysql_error($db));
$name_id = mysql_insert_id();

	$_SESSION['name_id'] = $name_id; //this is the key line
                $_SESSION['logged'] = 1;
	header('Refresh: 5; URL=main.php');

 

I've echoed out the result of $_SESSION['name_id'] and it says '0'.

 

I've tried changing the key line to

$_SESSION['name_id'] = LAST_INSERT_ID();

 

but I get this error message: Fatal error: Call to undefined function LAST_INSERT_ID() in C:\x\xa

 

Is my goal acheivavle or should I just set up a select query after the insert query and extract the id key?

Link to comment
Share on other sites

prehaps the sprintf function is messing it up and I need to printf it out?

 

$query = sprintf("INSERT INTO profile (
		 name_id, bio, exhib)
	VALUES (LAST_INSERT_ID(),'%s','%s')",
mysql_real_escape_string($_SESSION['pro']['bio']),
mysql_real_escape_string($_SESSION['pro']['exhib']));

$result = mysql_query($query, $db) or die(mysql_error($db));
printf($id = mysql_insert_id());

	$_SESSION['name_id'] = $id;
        $_SESSION['logged'] = 1;
	header('Refresh: 5; URL=main.php');
	echo $_SESSION['name_id']

oh no still got a 0  :wtf:

 

gizmola, what does that mean? :shrug: me no understand.

 

Any help appreciated.

Link to comment
Share on other sites

I tried the above and still received an 0 output when I ecoed the variable.

$query = sprintf("INSERT INTO user (
		name, password, admin_level, ip)
	VALUES ('%s','%s','%s','%s')",
mysql_real_escape_string($_SESSION['name']),
mysql_real_escape_string(md5(SALTY . $_SESSION['values']['password'])),
mysql_real_escape_string($_SESSION['admin_level']),
mysql_real_escape_string($ip12));

//let's run the query
$result = mysql_query($query, $db) or die(mysql_error($db));

$query = sprintf("INSERT INTO contact (
		name_id, first_name, last_name, email, address, city, county,
		post, home, mobile)
	VALUES (LAST_INSERT_ID(),'%s','%s','%s','%s','%s','%s','%s','%s','%s')",
mysql_real_escape_string($_SESSION['values']['first_name']),
mysql_real_escape_string($_SESSION['values']['last_name']),
mysql_real_escape_string($_SESSION['values']['email']),
mysql_real_escape_string($_SESSION['values']['address']),
mysql_real_escape_string($_SESSION['values']['city']),
mysql_real_escape_string($_SESSION['values']['county']),
mysql_real_escape_string($_SESSION['values']['post']),
mysql_real_escape_string($_SESSION['values']['home']),
mysql_real_escape_string($_SESSION['values']['mobile']));

//let's run the query
$result = mysql_query($query, $db) or die(mysql_error($db));

$query = sprintf("INSERT INTO profile (
		 bi, ex)
	VALUES ('%s','%s')",
mysql_real_escape_string($_SESSION['pro']['bi']),
mysql_real_escape_string($_SESSION['pro']['ex']));

$result = mysql_query($query, $db) or die(mysql_error($db));
$id = mysql_insert_id();

	$_SESSION['name_id'] = $id;
        $_SESSION['logged'] = 1;
	header('Refresh: 5; URL=main.php');
	echo $_SESSION['name_id'];

 

That's the whole insert.

Link to comment
Share on other sites

$query = sprintf("INSERT INTO user (
	name, password, admin_level, ip)
	VALUES ('%s','%s','%s','%s')",
mysql_real_escape_string($_SESSION['name']),
mysql_real_escape_string(md5(SALTY . $_SESSION['values']['password'])),
mysql_real_escape_string($_SESSION['admin_level']),
mysql_real_escape_string($ip12));

//let's run the query
$result = mysql_query($query, $db) or die(mysql_error($db));
$id = mysql_insert_id();

$query = sprintf("INSERT INTO contact (
		name_id, first_name, last_name, email, address, city, county,
		post, home, mobile)
	VALUES ('%u', '%s','%s','%s','%s','%s','%s','%s','%s','%s')",
$id,
mysql_real_escape_string($_SESSION['values']['first_name']),
mysql_real_escape_string($_SESSION['values']['last_name']),
mysql_real_escape_string($_SESSION['values']['email']),
mysql_real_escape_string($_SESSION['values']['address']),
mysql_real_escape_string($_SESSION['values']['city']),
mysql_real_escape_string($_SESSION['values']['county']),
mysql_real_escape_string($_SESSION['values']['post']),
mysql_real_escape_string($_SESSION['values']['home']),
mysql_real_escape_string($_SESSION['values']['mobile']));

//let's run the query
$result = mysql_query($query, $db) or die(mysql_error($db));

$query = sprintf("INSERT INTO profile (name_id, bi, ex)
	VALUES ('%u', '%s','%s')",
$id,
mysql_real_escape_string($_SESSION['pro']['bi']),
mysql_real_escape_string($_SESSION['pro']['ex']));

$result = mysql_query($query, $db) or die(mysql_error($db));

$_SESSION['name_id'] = $id;
$_SESSION['logged'] = 1;
echo $_SESSION['name_id'];
header('Refresh: 5; URL=main.php');

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.