Jump to content

Recommended Posts

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.

 

Link to comment
https://forums.phpfreaks.com/topic/237235-mysql_insert_id-problem/
Share on other sites

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.

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?

 

 

 

 

  • 2 weeks later...
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.