Summarize the Problem:
I want to write a booking code. I had a little problem, that I wanted to insert a value into the booked
table that is in another table called house_info
.
The detail of booked
& house_info
database is below:
Booked Table
ID_Booking | House_Name | House_ID | House_No | House_Qty | House_Price |
1 | Rose House | 1 | RH01 | 1 | |
2 | Rose House | 1 | RH02 | 1 | |
House Info Table
House_ID | HouseState_Name | House_Qty | House_Price |
1 | Garden City | 8 | 40000|
2 | Electronic City | 10 | 1000000|
I want to insert the House_Price
value on the house_info
table into the House_Price
column on booked
table every time users input on the Booking Form.
Background you've already tried:
I already tried this using a trigger on booked
table like below:
Trigger on Booked Table (Before Insert)
IF NEW.House_ID= '1' THEN SET
NEW.House_Price = 40000;
ELSEIF NEW.House_ID= '2' THEN SET
NEW.House_Price = 1000000;
But I realize this is not dynamic because when the company want to change the price of each HouseState_Name
he needs to change it from the trigger. So I think what I needed is a query
from PHP
that can calls the value of each HouseState_Name
and hold it on an array
and place it or insert it when the Book Query passed (I hope my logic is true, I'm sorry if it's false).
I already tried to search too for the query
's to use. But I didn't know how am I going to use the query.
Some Codes:
Booking.php
require 'Connection.php';
//Check Connection
if ($conn->connect_error){
die("Connection Failed: ". $conn->connect_error);
}
//Check for Submit
if (filter_has_var(INPUT_POST, 'Submit')) {
//Get Form Data
$CustomerEmail= htmlspecialchars($_POST["Email"], ENT_QUOTES);
$House_Name= htmlspecialchars($_POST["HouseName"], ENT_QUOTES);
$House_ID = htmlspecialchars($_POST["HouseID "], ENT_QUOTES);
$House_No = htmlspecialchars($_POST["HouseNo "], ENT_QUOTES);
//Validate the data fields
if (!empty($CustomerEmail) && !empty($House_Name)) {
//Passed
if (filter_var($CustomerEmail, FILTER_VALIDATE_EMAIL) === false) {
//Failed
$msg = 'Please use a valid email';
header("location: ../GardenCity.php?error=PleaseUseValidEmail");
} else {
//Passed
echo "Creating a Booking.<br>";
//Inserting the Booking Data into Database
$sql = "INSERT INTO `booked`(`ID_Booking`, `CustomerEmail`, `House_Name`, `House_ID`, `House_No`)
VALUES (NULL, '$CustomerEmail', '$House_Name', '$House_ID ', '$House_No', '', '')";
if ($conn->query($sql) === TRUE) {
header("location: ../GardenCity.php");
} else {
echo "Error: " . $sql . "<br><br>" . $conn->error;
}
}
} else {
header("location: ../GardenCity.php?error=EmptyFields");
}
}
$conn -> close();
Expected Results:
Before Update the price
Database Looks
ID_Booking | House_Name | House_ID | House_No | House_Qty | House_Price |
1 | Rose House | 1 | RH01 | 1 | |
2 | Rose House | 1 | RH02 | 1 | |
3 | Rose House | 1 | RH03 | 1 | 40000|
House_ID | HouseState_Name | House_Qty | House_Price |
1 | Garden City | 7 | 40000|
2 | Electronic City | 10 | 1000000|
After Update the price
Database Looks
ID_Booking | House_Name | House_ID | House_No | House_Qty | House_Price |
1 | Rose House | 1 | RH01 | 1 | |
2 | Rose House | 1 | RH02 | 1 | |
3 | Rose House | 1 | RH03 | 1 | 40000|
4 | Rose House | 1 | RH04 | 1 | 200000|
House_ID | HouseState_Name | House_Qty | House_Price |
1 | Garden City | 6 | 200000|
2 | Electronic City | 10 | 1000000|
I hope this is well explained. Please let me know if there's any confusing statements or questions. I will say many thanks to you all if this is answered because I'm so stuck at this and my brain won't work.