如何获取最后插入的id以及如何使用php检查更新查询的条件

时间:2016-02-08 18:14:20

标签: php mysql

您好我将数据插入数据库表并重定向到另一个页面,需要通过比较ID和电子邮件将详细信息插入到同一个表中。但是如何获取最后插入的ID并比较where条件以更新详细信息。

这是我的代码:

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

当我第一次点击提交按钮时,数据会插入到数据库并重定向到ownerproperty.php。我需要获取插入的ID并需要comapre该id和email并需要更新所有者属性当电子邮件和身份证相同时,同一列中的详细信息。但是如何获取该ID并在任何人帮助我的情况下进行比较。

4 个答案:

答案 0 :(得分: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");          
}

[注意mysql扩展程序已弃用。使用PDOmysqli_数据库扩展。]

答案 1 :(得分:0)

您可以添加:

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

哪个应该从最后一次插入中获取ID。

答案 2 :(得分: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";
}

答案 3 :(得分:0)

您可以添加:

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

$id = $row['id'];