I have two files, ApplicationRegister.php and UserRegister.php
In ApplicationRegister.php the user fill the details and submit the form to database, it redirects to UserRegistration.php.

In UserRegistration.php, User fills up the form and save the data to the lastest inserted id.
I have used mysql_insert_id() to get the last inserted id, but its not working.

I did like this in my UserRegistration.php

<?php
                 include('connect.php');

                $id = mysql_query("select last_insert_id()"); 



              echo $id;
              $sql = "select * from ApplicationRegister where application_id= $id" ;
              $result = mysql_query($sql) or die (mysql_error());
              while ($row = mysql_fetch_assoc($result))

                {
              echo "<table width='85%' cellpadding='4' border='0'>" ;
              echo "<tr><td> <h3> Company Name:   </h3></td> <td>" . $row['CompanyName'] . "</td> </tr> ";

                  echo "</table>";             

                }

When i say echo $id; I get result as 0.

Actually i am inserting data in ApplicationRegister.php like this

$sql = "INSERT INTO ApplicationRegister (CompanyName, CompanyEmail, CompanyContact, CompanyAddress, RegistrationType, ApplicationPlan, ApplicationStatus, CreatedDate) VALUES ('$CompanyName', '$CompanyEmail', '$CompanyContact', '$CompanyAddress', '$RegistrationType', '$Plans', '$Status', NOW() )";
$result = mysql_query($sql) or die(mysql_error());


//if(isset($plan == "trail"))
if($plan == "trail")
{

header("Location: userRegister.php");
}
else
{
    header("Location : PaymentGateway.php");
}

This data will be submitted to the ApplicationRegister table. after submitting user will redirect to user registration page, there i wan to get the last inserted id from the ApplicationRegister table. So the insert command will be in the previous page.
How can i solve this?
Please help me.

Recommended Answers

All 2 Replies

You need to get the last insert id directly after the insert query. So move it there, and store the result in a session variable, or pass it as a parameter.

@my3h
why not you set last inserted id in session. use the below code in ApplicationRegister.php file after line #8.

session_start();
$id = mysql_insert_id();
$SESSION['id'] = $id;

and on UserRegister.php file use:

session_start();
$id = $SESSION['id'];
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.