gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 06:17:04 25 4
gpt4 key购买 nike

您好,我正在将数据插入数据库表并重定向到另一个页面,需要通过比较 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>

属性.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 并需要比较该 ID 和电子邮件,并且需要更新所有者属性当电子邮件和 id 相同时,详细信息在同一列中。但是如何获取该 id 并在 where 条件下进行比较,任何人都可以帮助我。

最佳答案

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_ 数据库扩展。]

关于php - 如何获取最后插入的 id 以及如何使用 php 检查更新查询的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35276422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com