我有 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 injection , 将变量直接插入到查询中绝不是一个好主意。
您使用 property_id 来检查/更新销售列表,但是每次列出新属性时,它都会生成一个新的唯一 ID。这会导致您的插入始终创建新属性而不是更新旧属性,因为新生成的 ID 永远不会出现在销售列表中。我建议将地址添加到销售列表中,并在此列/这些列上执行 where
子句以更新销售列表。
关于php - 在没有找到记录时插入数据,否则使用 PHP Mysql 使用最后插入的 ID 更新前一条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52761552/