I have a page with with the following php code:
<?php
include ('dbconnect.php');
// get values from the form
function getPosts()
{
$posts = array();
$posts[0] = $_POST['bookingid'];
$posts[1] = $_POST['bookingname'];
$posts[2] = $_POST['cabintype'];
$posts[3] = $_POST['lengthofstay'];
$posts[4] = $_POST['guests_description'];
$posts[5] = $_POST['startdate'];
$posts[6] = $_POST['enddate'];
$posts[7] = $_POST['other_information'];
return $posts;
}
if (isset($_POST['bookingdetails'])) {
$data = getPosts();
$insert_Query = "insert into bookings(Bookingid,Bookingname,Cabin_Type,
lengthofstay,Details_about_guests,Booking_start_date,Booking_end_date,
Other_relavant_information)
values ('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]',
'$data[6]','$data[7]')";
$result = mysqli_query($db, $insert_Query);
if ($result){
echo "<p>Total price is \$$total</p>\n";
Booked</p> </font>";
}else{
echo "<p> Something went Wrong! Contact Your Administrator </p>" .
mysqli_error ($db);
}
}
$Luxury_Cabin= 1200;
$Contemporary_Cabin= 1000;
$Original_Cabin= 800;
$Total= "";
if(isset($_POST['bookingdetails']) && $_POST['luxurycabin']=='1')
{
$num=(int)$_POST['lengthofstay'];
if($num>=0)
{
$Total=($Luxury_Cabin*$num);
}
}
if(isset($_POST['bookingdetails']) && $_POST['contemporarycabin']=='2')
{
$num=(int)$_POST['lengthofstay'];
if($num>=0)
{
$Total=($Contemporary_Cabin*$num);
}
}
if(isset($_POST['bookingdetails']) && $_POST['originalcabin']=='4')
{
$num=(int)$_POST['lengthofstay'];
if($num>=0)
{
$Total=($Original_Cabin*$num);
}
}
echo "<p>Total price is \$$total</p>\n";
?>
And the HTML Code is like the following:
<form method = "post" action = "" >
<tr>
<th>Bookingid </th>
<td><input type = "text" name = "bookingid" /> </td>
</tr>
<tr>
<th>Booking Name </th>
<td><input type = "text" name = "bookingname" /> </td>
</tr>
<tr>
<th>Cabin Type </th>
<td><input type = "text" name = "cabintype" /> </td>
</tr>
<tr>
<th>Luxury Cabin</th>
<td><input type="radio" name="luxurycabin" value="1" />Yes</td>
<td><input type="radio" name="luxurycabin" value="0"
checked="checked"/>No</td>
</tr>
<tr>
<th>Contemporary Cabin</th>
<td><input type="radio" name="contemporarycabin" value="2" />Yes</td>
<td><input type="radio" name="contemporarycabin" value="3"
checked="checked"/>No</td>
</tr>
<tr>
<th>Original Cabin</th>
<td><input type="radio" name="originalcabin" value="4" />Yes</td>
<td><input type="radio" name="originalcabin" value="5"
checked="checked"/>No</td>
</tr>
<tr>
<th>Length of Stay </th>
<td><input type = "text" name = "lengthofstay" id="lengthofstay"/> </td>
</tr>
<tr>
<th>Details About Guests </th>
<td><textarea cols = "30" rows = "5" type = "text" name
="guests_description" /></textarea> </td>
</tr>
<tr>
<th>Booking Start Date </th>
<td><input type = "text" name = "startdate" /> </td>
</tr>
<tr>
<th>Booking End Date </th>
<td><input type = "text" name = "enddate" /> </td>
</tr>
<tr>
<th>Other Relavant Information </th>
<td><textarea cols = "30" rows = "5" type = "text" name
="other_information" /></textarea> </td>
</tr>
<tr>
<th> </th>
<td> <input type = "submit" value = "Create Booking!" name =
"bookingdetails" /> </td>
</tr>
</table>
</form>
What occurs here is that when the "bookingdetails" button is pressed all of the data entered into the form are sent to the database. What is also done is a multiplication calculation which uses the radio buttons and the number entered into the lenghtofstay field. The result is then echoed on the page itself using:
<?php echo "<p>Total price is \£$total</p>\n"; ?>
So what I need your assistance with is how do I get the dynamically generated calculation result into my database table with a column called Booking_Price?
Just swap your code around so all the calculation stuff happens before the database insert and then you have your value to insert in $total
. Modify your insert query to include it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.