user5891511 February 2016

how to get the last inserted id and how to check in where condition to update query using php

Hi iam inserting the data into database table and redirecting to another page and need to insert the details into the same table by comparing ids and email.But how to get the last inserted id and compare in where condition to update the details.

Here is my code:

index.php

<form method="post" action="properties.php" id="myform">
<input  type='hidden' value="<?php echo $username; ?>" name='email'>
<tr><th>Interest Paid</th><td><input type="text" name="house_interest_paid" value=""/></td>
<tr><th>Total Interest Paid</th><input type="text" name="house_total_interest_paid" value=""/></td>
<tr><th>House Number</th><input type="text" name="house_number" value=""/></td>
<tr><th>Street</th><input type="text" name="house_street" value=""/></td>
<button type="submit"  class = "large awesome yellows" onClick="document.location.href='ownerproperty.php'"> Specify Co-owners Property</button> </span>
</form>

properties.php

$email=$_POST['email'];
$interest=$_POST['house_interest_paid'];
$interestpaid=$_POST['house_total_interest_paid'];
$number=$_POST['house_number'];
$street=$_POST['house_street'];
$query=mysql_query("INSERT INTO house_details(email,house_interest_paid,house_total_interest_paid,house_number,house_street)values           ('$email','$interest','$interestpaid','$number','$street')");
if($query)
{       
    session_start();
    header("Location:ownerproperty.php");       
}
else{
    echo "Registration has not been completed.Please try again";
}

ownerproperty.php

<form style="display:none" method="POST" action="owner.php"> 
<h2>Owner Property details</h2>              
<input type='hidden' value="<?php echo $username; ?>        

Answers


durbnpoisn February 2016

You can add this:

$stmt = 'SELECT LAST_INSERT_ID() as sessionId';

Which should grab the ID from the last insert.


Nana Partykar February 2016

properties.php

$query=mysql_query("INSERT INTO house_details(email,house_interest_paid,house_total_interest_paid,house_number,house_street)values           ('$email','$interest','$interestpaid','$number','$street')");

$id = mysql_insert_id(); //For last inserted id.

if($query)
{       
    session_start();
    $_SESSION['sess_id'] = $id; // Set one session variable for last inserted id.
    header("Location:ownerproperty.php");       
}

owner.php

<?php
session_start(); // Start your session

$id = $_SESSION['sess_id']; // Use this id in query

$email=$_POST['email'];
$owner_name=$_POST['name_coowner'];
$pan_owner=$_POST['pan_coowner'];
$query=mysql_query("UPDATE house_details SET name_coowner='$owner_name',pan_coowner='$pan_owner'
                      WHERE email='$email' AND house_details_id='2'");
if($query)
{       
    session_start();
    header("Location:rentalproperty.php");          
}

[NOTE : mysql extensions are deprecated. Use PDO or mysqli_ database extensions.]


Vasim Vanzara February 2016

$query=mysql_query("INSERT INTO house_details(email,house_interest_paid,house_total_interest_paid,house_number,house_street)values           ('$email','$interest','$interestpaid','$number','$street')");
if($query)
{   
   //Last inserted ID
    $last_id = $query->insert_id;
    session_start();
    header("Location:ownerproperty.php");       
}
else{
    echo "Registration has not been completed.Please try again";
}


Hisham Elsayad February 2016

You can add this:

$stmt = 'SELECT MAX(house_details_id) AS "id" FROM house_details';
$result=mysql_query($stmt);
$row = mysql_fetch_array($result);

$id = $row['id'];

Post Status

Asked in February 2016
Viewed 2,123 times
Voted 5
Answered 4 times

Search




Leave an answer