1

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; ?>" name='email'>
<?php include "ownership.php"; ?>
<p><label for="name_coowner">Coowner Name</label> <input value="<?php echo $row['name_coowner'];?>" type="text"    name="name_coowner"  /></p>
<p><label for="pan_coowner">PAN Of Coowner</label> <input value="<?php echo $row['pan_coowner'];?>" type="text"   name="pan_coowner"  /></p>            
<button type="submit"  class = "medium" style="background-color: #2daebf;">Save</button>
</form>

Ownership.php

$res = "SELECT * FROM house_details 
   WHERE email ='$username'";
$result=mysql_query($res);
$row = mysql_fetch_array($result);

Owner.php

$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");          
}

For the first time when i click on submit button the data is inserting into db and redirecting to ownerproperty.php .in that i need to get the inserted id and need to comapre that id and email and need to update the owner property details in the same column when the email and id are same.But how to get that id and compare in the where condition can anyone help me.

user5891511
  • 71
  • 3
  • 9
  • 2
    Please dont use the `mysql_` database extension, it is deprecated (gone for ever in PHP7) Especially if you are just learning PHP, spend your energies learning the `PDO` or `mysqli_` database extensions, [and here is some help to decide which to use](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – RiggsFolly Feb 08 '16 at 18:19

4 Answers4

2

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.]

halfer
  • 19,824
  • 17
  • 99
  • 186
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
0

You can add this:

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

Which should grab the ID from the last insert.

durbnpoisn
  • 4,666
  • 2
  • 16
  • 30
0
$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";
}
Vasim Shaikh
  • 4,485
  • 2
  • 23
  • 52
0

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'];
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
Hisham Elsayad
  • 418
  • 5
  • 12