Namtip Posted October 4, 2010 Share Posted October 4, 2010 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? Quote Link to comment Share on other sites More sharing options...
techiefreak05 Posted October 4, 2010 Share Posted October 4, 2010 Well LAST_INSERT_ID() doesn't exist in PHP. Um... echo $name_id and see what happens Quote Link to comment Share on other sites More sharing options...
Namtip Posted October 4, 2010 Author Share Posted October 4, 2010 I echoed $name_id and it came up with 0 Quote Link to comment Share on other sites More sharing options...
gizmola Posted October 4, 2010 Share Posted October 4, 2010 Your insert query should either pass NULL or omit the name_id column entirely. Quote Link to comment Share on other sites More sharing options...
Namtip Posted October 4, 2010 Author Share Posted October 4, 2010 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 gizmola, what does that mean? me no understand. Any help appreciated. Quote Link to comment Share on other sites More sharing options...
Mchl Posted October 4, 2010 Share Posted October 4, 2010 Why did you put LAST_INSERT_ID() into your query? Is there another INSERT query being run before it? What is this table's structure? Quote Link to comment Share on other sites More sharing options...
gizmola Posted October 4, 2010 Share Posted October 4, 2010 $query = sprintf("INSERT INTO profile (bio, exhib)VALUES ('%s','%s')", Quote Link to comment Share on other sites More sharing options...
Namtip Posted October 4, 2010 Author Share Posted October 4, 2010 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. Quote Link to comment Share on other sites More sharing options...
gizmola Posted October 4, 2010 Share Posted October 4, 2010 $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'); Quote Link to comment Share on other sites More sharing options...
Namtip Posted October 4, 2010 Author Share Posted October 4, 2010 That's fixed it. Thanks so much, I'm looking at what you've done now. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.