php - 在找不到记录时插入数据,否则使用 PHP Mysql 使用最后插入的 ID 更新上一条记录
问题描述
我有 2 个表属性作为父表,sale_listings 作为子表。我想以编程方式将销售列表记录插入或更新到 sale_listings 表中,同时插入属性表的数据。
如果在 sale_listings 表中找到先前的记录,则更新记录,否则插入新的销售列表记录。
两个表数据都成功插入,无需检查 sale_listing(子表)中的先前记录。
当我尝试使用 rowCount 检查 sale_listing 中的先前记录时,应用程序根本不会影响任何行。
我想知道如何使用 PHP-Mysql 实现这一点?
下面是源代码:
//Assign Property Values
$property_type = isset($_POST["property_type"] ) ? $_POST["property_type"]: '';
$agent = isset($_POST["agent"] ) ? $_POST["agent"]: '';
$suburb = isset($_POST["suburb"] ) ? $_POST["suburb"]: '';
$street_no = isset($_POST["street_no"] ) ? $_POST["street_no"]: '';
$street_name = isset($_POST["street_name"] ) ? $_POST["street_name"]: '';
$desc = isset($_POST["desc"] ) ? $_POST["desc"]: '';
$property_status = isset($_POST["property_status"] ) ? $_POST["property_status"]: '';
$num_bathrooms = isset($_POST["num_bathrooms"] ) ? $_POST["num_bathrooms"]: '';
$num_beds = isset($_POST["num_beds"] ) ? $_POST["num_beds"]: '';
$num_garages = isset($_POST["num_garages"] ) ? $_POST["num_garages"]: '';
$num_lounges = isset($_POST["num_lounges"] ) ? $_POST["num_lounges"]: '';
$air_con = isset($_POST["air_con"] ) ? $_POST["air_con"]: '';
$pool = isset($_POST["pool"] ) ? $_POST["pool"]: '';
$cottage = isset($_POST["cottage"] ) ? $_POST["cottage"]: '';
$price = isset($_POST["price"] ) ? $_POST["price"]: '';
if((!empty($agent)) || (!empty($suburb)) || (!empty($property_type)) ||
(!empty($street_no)) || (!empty($street_name)) ||(!empty($desc)) ||
(!empty($property_status)) || (!empty($num_bathrooms)) ||
(!empty($num_beds)) || (!empty($num_garages)) || (!empty($num_lounges)) ||
(!empty($air_con)) || (!empty($pool)) || (!empty($cottage)) ||
(!empty($price))){
//Insert data into Properties table
$query = "INSERT INTO properties (agent_id, suburb_id, property_type,
property_status, street_no, street_name, property_desc, num_bathrooms,
num_beds, num_garages, num_lounges, air_con, pool, cottage, price)
VALUES('$agent', '$suburb', '$property_type', '$property_status',
'$street_no', '$street_name', '$desc', '$num_bathrooms', '$num_beds',
'$num_garages', '$num_lounges', '$air_con', '$pool', '$cottage',
'$price')";
$row_count = $this->conn->exec($query);
//Retrieve the last inserted Property ID
$last_insert_property_id = $this->conn->lastInsertId();
if($row_count){
$query = "UPDATE suburbs SET total_properties = total_properties + 1
WHERE suburb_id = '$suburb'";
$row_count = $this->conn->exec($query);
//Check if the last inserted property ID exists
if($last_insert_property_id){
//If the last property_id exists, make it equal to property ID
$property_id = $last_insert_property_id;
//Check if previous sale listing exist
$query_sel = "SELECT sale_listing_id, property_id,
sale_listing_amount, discount_percent, total_listing
FROM sale_listings";
$result = $this->conn->query($query_sel);
if($result->rowCount() > 0){
$query = "UPDATE sale_listings SET total_listing = total_listing + 1 WHERE property_id = '$property_id'";
$row_count = $this->conn->exec($query);
}else{
$sale_amount = 65;
$discount = 0;
$total_listing = 1;
$sql = "INSERT INTO sale_listings (property_id,
sale_listing_amount, discount_percent, total_listing)
VALUES('$property_id', '$sale_amount', '$discount',
'$total_listing')";
$row_count = $this->conn->exec($sql);
}
}else{
echo("Unable to insert or update record!");
}
}
return $row_count;
}
解决方案
您错过了查询的一个非常重要的部分UPDATE
,现在您的更新查询将更新表中的所有记录,您应该添加一条WHERE
语句,以便只更新您想要的字段。例如:
UPDATE sale_listings
SET total_listing = total_listing+1
WHERE sale_listing_id = 'current_sale_listing_id'
我也错过了您选择中的 where 语句以检查上一个列表是否存在,因此您的行数总是不正确的。这应该是:
SELECT sale_listing_id, property_id,
sale_listing_amount, discount_percent, total_listing
FROM sale_listings WHERE property_id = 'last_property_id'
还要查看SQL 注入,将变量直接插入查询绝不是一个好主意。
您使用 property_id 来检查/更新销售列表,但是每次列出新属性时,它都会生成一个新的唯一 ID。这会导致您的插入始终创建一个新属性,而不是更新旧属性,因为新生成的 ID 永远不会出现在销售列表中。我建议将地址添加到销售列表中,并where
在此列/这些列上执行条款以更新销售列表。
推荐阅读
- python - 调用外部程序停止我的脚本
- scala - 带空格的突触火花选择列
- python - 从 Flask 中的 post 请求运行类函数
- mule - 从 Payload 读取内容并在 Mule 电子邮件连接器中发送电子邮件
- android - 有没有办法根据应用程序主题更改启动画面之前出现的空白屏幕颜色?
- sql - 引号:发生了语法错误。SQLCODE=-201
- mysql - 服务器消失后如何在 MySQL 中继续导入 .sql 数据库
- parallel-processing - 为 MCMC(马尔可夫链蒙特卡罗)寻找最佳 mpirun 参数
- python - 如何使用 Python 取数据帧的特定不同行的平均值
- java - 如何在生成链接时创建指向尚不存在的特定页面的转发链接