animone Posted May 23, 2011 Share Posted May 23, 2011 Hi all, i'm a bit more than a newbie. Here's the scenario. I have a page with tabbed navigation. On each tab there's a form (diving centers, teachers, and so on). Each form has a submit button with a unique name. Every submit button is processed by a series of if statements in an external included php file. As you can see in my code below what the file does is to process the if statements based on the submit pressed. It' important to notice that a JS script checks that the first form is filled with all the infos. If not is not possible to proceed to complete all the others. On the first form the user can register general information. Obviously there's an Id field (auto increment) that i grab with mysql_nsert_id. Also obvious is the fact that, for query reasons, i want to store the grabbed id in an id field present in all the tables of my database. The tables get the data from the various forms displayed on the tabbed navigation. Here's the code of the included file (notice that i use to start with a simple coding to test everything 's working fine) <?php require_once('Connections/Scubadiving.php'); if (!empty($_POST['theButton'])) { $nome=$_POST["nome"]; $indirizzo=$_POST["indirizzo"]; $insertSQL = "INSERT INTO centrisub (nome, indirizzo) VALUES ('$nome' , '$indirizzo')"; mysql_select_db($database_Scubadiving, $Scubadiving); $Result1 = mysql_query($insertSQL, $Scubadiving) or die(mysql_error()); $last_id = mysql_insert_id ($Scubadiving); } if (!empty($_POST['istruttori'])) { echo $_SESSION["$last_id"]; $insertSQL = "INSERT INTO istruttori (nome, data_nascita, curriulum, altre_info, idcentrisub) VALUES ('$_POST[nome]','$_POST[data_nascita]','$_POST[curriculum]','$_POST[altre_info]','$last_id')"; mysql_select_db($database_Scubadiving, $Scubadiving); $Result1 = mysql_query($insertSQL, $Scubadiving) or die(mysql_error()); } ?> What i don't understand is why $last_id is not passed form the first if statment (processed when the user submit the generel infos form) to the other if statement (in this case the teachers form). I've tried also with $_SESSION (trying to assign $last_id) but no success. (Probably because i don't know exactly how to use it) Hope everything's clear. What i'm missing ? Thanks in advance for your help. Quote Link to comment https://forums.phpfreaks.com/topic/237235-mysql_insert_id-problem/ Share on other sites More sharing options...
wildteen88 Posted May 23, 2011 Share Posted May 23, 2011 Because you are reloading the page PHP will not carry over the $last_id variable. In order to send this variable to the next page you need to either include it within your form as a hidden form field. Or set it as a session variable $_SESSION['last_id'] = $last_id; IMPORTANT make sure you call session_start() as the first line in your PHP files that uses sessions. Quote Link to comment https://forums.phpfreaks.com/topic/237235-mysql_insert_id-problem/#findComment-1219137 Share on other sites More sharing options...
animone Posted May 24, 2011 Author Share Posted May 24, 2011 Because you are reloading the page PHP will not carry over the $last_id variable. In order to send this variable to the next page you need to either include it within your form as a hidden form field. Or set it as a session variable $_SESSION['last_id'] = $last_id; IMPORTANT make sure you call session_start() as the first line in your PHP files that uses sessions. Thank you Wildteen. Tried with $_SESSION and worked perfectly. One last question about storing value in hidden field. Where should i put the hidden? On the second form and send it back with $_POST? Quote Link to comment https://forums.phpfreaks.com/topic/237235-mysql_insert_id-problem/#findComment-1219465 Share on other sites More sharing options...
wildteen88 Posted May 24, 2011 Share Posted May 24, 2011 You would only pass $last_id to a hidden form field if wasn't going to use sessions. Since you are now using sessions then there is no need to have it within your form. Quote Link to comment https://forums.phpfreaks.com/topic/237235-mysql_insert_id-problem/#findComment-1219676 Share on other sites More sharing options...
animone Posted June 7, 2011 Author Share Posted June 7, 2011 Even if after a while i want to thank wildteen for his precious help Quote Link to comment https://forums.phpfreaks.com/topic/237235-mysql_insert_id-problem/#findComment-1226307 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.