繁体   English   中英

HTML表单插入在PHP和MySQL中不起作用

[英]HTML form insert not working in PHP and MySQL

这是从上一个问题( 带有insert / update的html / php / sql表单 )开始的,因此可以将其用作HTML表单的参考。 当客户入住时,其客户的活动帐户将更改为1,房间的room_vacant更改为0,并进行入住记录。

不仅如此,还需要使用所有这些信息来生成第一笔租金。 rent_occupancy_id是通过lastInsertId()的用户确定的,因为最后插入的ID将是入住ID。

无论入住的客户是否享受福利,都会生成rent_cost。 如果客户没有福利,则rent_cost =£126.57,但是如果他们没有,则rent_cost =£20.03。 date_lastpaid与客户入住记录的起始日期相同,并且date_due等于date_lastpaid之前的date_lastpaid

问题在于没有创建租金记录,而对其他记录等进行了所有其他更改。 任何帮助将非常感激。

<?php
require("dbconnect.php");

//CLIENT
$id = $_POST['id'];
//UPDATES client's to 1/Yes to say that they're now an active client in JRH, 
//where the selected client's ID is equal to :id
$stmt = $dbh->prepare("UPDATE client SET active = 1 WHERE client_id=:id");
$stmt->bindParam(':id', $id);
$stmt->execute();

//ROOM
$room_id = $_POST['room_id'];
$stmt = $dbh->prepare("UPDATE room SET room_vacant = 0 WHERE room_id = :room_id");
$stmt->bindParam(':room_id', $room_id);
$stmt->execute();

//OCCUPANCY
$id = $_POST['id'];
$room_id = $_POST['room_id'];
$start_date = $_POST['start_date'];
$stmt = $dbh->prepare("INSERT INTO occupancy (occupancy_client_id, occupancy_room_id, start_date) VALUES(:id, :room_id, :start_date)");
$stmt->bindParam(':id', $id);
$stmt->bindParam(':room_id', $room_id);
$stmt->bindParam(':start_date', $start_date);
$stmt->execute();

//Working out Rent Cost
$id = $_POST['id'];
$stmt=$dbh->prepare("SELECT * FROM client WHERE client_id=:id");
$stmt->bindParam(':id', $id);
$stmt->execute();
$row = $stmt->fetch();
//Works out whether a client has benefits or not,
//and then uses this information to generate a cost of the rent
if ($row['benefits'] == '0') {
$rent_cost = "126.57";
} else{
$rent_cost = "20.03";
}


//RENT INSERT
//
//As the occupancy record is being created above
//and the rent table needs a rent_occupancy_id to join,
//then we use lastInsertId() in order to get that occupancy id
$rent_occupancy_id = $dbh->lastInsertId();
$date_lastpaid = $_POST['start_date']; //As the client hasn't made a payment yet,
//we say that their last payment was the date they joined
$date_due = strtotime($date_lastpaid);
$date_due = strtotime('+1 week', $date_due);
$date_due = date('Y/m/d', $date_due);
$stmt = $dbh->prepare("INSERT INTO rent (rent_occupancy_id, rent_cost, date_lastpaid, date_due) VALUES (:rent_occupancy_id, :rent_cost, :date_lastpaid, :date_due)");
$stmt->BindParam(':rent_occupancy_id', $rent_occupancy_id);
$stmt->BindParam(':rent_cost', $rent_cost);
$stmt->BindParam(':date_lastpaid', $date_lastpaid);
$stmt->BindParam(':date_due', $date_due);
$stmt->execute();
?>

只需将bindParam()首字母更改为小写即可;

$stmt->bindParam(':rent_occupancy_id', $rent_occupancy_id);
$stmt->bindParam(':rent_cost', $rent_cost);
$stmt->bindParam(':date_lastpaid', $date_lastpaid);
$stmt->bindParam(':date_due', $date_due);
$stmt->execute();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM