将动态生成的PHP计算保存到MySQL数据库

时间:2017-10-14 22:11:10

标签: php html mysqli

我有一个包含以下php代码的页面:

<?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";
?>

HTML代码如下:

<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>

这里发生的是&#34;预订细节&#34;按下按钮将输入到表单中的所有数据都发送到数据库。还做的是乘法计算,它使用单选按钮和输入到lenghtofstay字段中的数字。然后使用以下结果在页面上回显结果:

<?php echo "<p>Total price is \£$total</p>\n"; ?> 

所以我需要你的帮助是如何使用名为Booking_Price的列将动态生成的计算结果输入到我的数据库表中?

1 个答案:

答案 0 :(得分:0)

只需交换代码,以便所有计算内容在数据库插入之前发生,然后将值插入$total。修改您的插入查询以包含它。