简体   繁体   English

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

[英]Saving Dynamically Generated PHP Calculations to A MySQL Database

I have a page with with the following php code: 我有一个页面,其中包含以下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";
?>

And the HTML Code is like the following: 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>

What occurs here is that when the "bookingdetails" button is pressed all of the data entered into the form are sent to the database. 这里发生的是,当按下“ bookingdetails”按钮时,输入到表单中的所有数据都被发送到数据库。 What is also done is a multiplication calculation which uses the radio buttons and the number entered into the lenghtofstay field. 还完成了乘法计算,该计算使用单选按钮和在lenghtofstay字段中输入的数字。 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? 因此,我需要您的帮助的是如何通过名为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 . 只需交换您的代码,以便所有计算工作都在数据库插入之前发生,然后您就可以在$total插入值。 Modify your insert query to include it. 修改您的插入查询以包括它。

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

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