单击提交按钮时如何在php中的多个表中保存数据

时间:2016-07-09 03:17:15

标签: php mysql

我是新手,我正在使用此代码但我没有得到正确的结果...我想保存来自reservation.php的数据并传递给res2.php将其保存在数据库中,但我做了不能做对..

  

reservation.php

<form action="res2.php" method="post">
  <table align="center" id="tbl1">
    <th colspan="2">Summary</th>
      <tr>
        <td class="td1" style="font-weight:bold; width:130px;">First Name:</td>
        <td class="td2"><input type="text" id="Fname4" name="Fname4" style="width:100%;"></td>
      </tr>
      <tr>
        <td class="td1" style="font-weight:bold; width:130px;">Middle Name:</td>
        <td class="td2"><input type="text" id="Mname4" name="Mname4" style="width:100%;"></td>
      </tr>
      <tr>
        <td class="td1" style="font-weight:bold; width:130px;">Last Name:</td>
        <td class="td2"><input type="text" id="Lname4" name="Lname4" style="width:100%;"></td>
      </tr>
      <tr>
        <td class="td1" style="font-weight:bold; width:130px;">Address:</td>
        <td class="td2"><input type="text" id="Address4" name="Address4" style="width:100%;"></td>
      </tr>
      <tr>
        <td class="td1" style="font-weight:bold; width:130px;">Email Address:</td>
        <td class="td2"><input type="text" id="Email4" name="Email4" style="width:100%;"></td>
      </tr>
      <tr>
        <td class="td1" style="font-weight:bold; width:130px;">Mobile Number:</td>
        <td class="td2"><input type="text" id="Mobile4" name="Mobile4" style="width:100%;"></td>
      </tr>
      <tr>
        <td class="td1" style="font-weight:bold; width:130px;">Arrival:</td>
        <td class="td2"><input type="text" id="CheckIn4" name="CheckIn4" value="" style="width:100%;"></td>
      </tr>
      <tr>
        <td class="td1" style="font-weight:bold; width:130px;">Departure:</td>
        <td class="td2"><input type="text" id="CheckOut4" name="CheckOut4" style="width:100%;"></td>
      </tr>
      <tr>
        <td class="td1" style="font-weight:bold; width:130px;">Days:</td>
        <td class="td2"><input type="text" id="Days4" name="Days4" style="width:100%;"></td>
      </tr>
      <tr>
        <td class="td1" style="font-weight:bold; width:130px;">Room Type:</td>
        <td class="td2"><input type="text" id="RmType4" name="RmType4" style="width:100%;"></td>
      </tr>
      <tr>
        <td class="td1" style="font-weight:bold; width:130px;">Total Amount:</td>
        <td class="td2"><input type="text" id="TotalAmt4" name="TotalAmt4" style="width:100%;"></td>
      </tr>
      <tr>
        <td class="td1" style="width:230px;font-weight:bold; width:130px;">Down Payment:</td>
        <td class="td2"><input type="text" id="DownPay4" name="DownPay4" style="width:100%;"></td>
      </tr>                 
      <tr>
        <td class="td1" style="font-weight:bold; width:130px;">Balance:</td>
        <td class="td2"><input type="text" name="Balance4" id="Balance4" style="width:100%;"></td>
      </tr>
    </table>
    <div class="reminder"> 
      <input  id="submit" class="buttonSubmit" type="submit" value="SUBMIT"></br>
      <p class="note">Clicking the button means you have accepted the <a href="terms.php">Terms and Conditions</a></p>
    </div>
  </form>
  

res2.php

<?php
    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpassword = '';
    $conn = mysql_connect($dbhost,$dbuser,$dbpassword);
        if(!$conn){
            die('Could not connect:'.mysql_error());
        }
        echo 'Connected Successfully';
    mysql_select_db('dbmaurhotel',$conn);


   $rmType = mysql_real_escape_string($_POST['RmType4']);
   $mysql0 = "SELECT * FROM tblrooms WHERE RM_TYPE='".$rmType."'";
   $mydata = mysql_query($mysql0, $conn);
        while($record = mysql_fetch_array($mydata)){
            $rmId = $record['RM_ID'];
            $rmRate = $record['RM_RATE'];
            echo "<input type='hidden' name='rmId' id='rmId'value='$rmId'>";
            echo "<input type='hidden' name='rmRate' id='rmRate' value='$rmRate'>";
        }
        $mysql10 = "SELECT * FROM tblcustomer";
        $mydata = mysql_query($mysql10, $conn);
            while($record = mysql_fetch_array($mydata)){
                $custId = $conn->insert_id;
                echo "<input type='hidden' name='custId' id='custId' value='$custId'>";
                }
        $mysql11 = "SELECT * FROM tblcustomer";
            $mydata = mysql_query($mysql11, $conn);
            while($record = mysql_fetch_array($mydata)){
            $resId = $conn->insert_id;
            echo "<input type='hidden' name='resId' id='resId' value='$resId'>";
                        }

    $resId=mysql_real_escape_string($_POST['resId']);
    $custId=mysql_real_escape_string($_POST['custId']);
    $resId=mysql_real_escape_string($_POST['resId']);
    $custId=mysql_real_escape_string($_POST['custId']);
    $fname=mysql_real_escape_string($_POST['Fname4']);
    $mname=mysql_real_escape_string($_POST['Mname4']);
    $lname=mysql_real_escape_string($_POST['Lname4']);
    $address=mysql_real_escape_string($_POST['Address4']);
    $email=mysql_real_escape_string($_POST['Email4']);
    $mobile=mysql_real_escape_string($_POST['Mobile4']);
    $status="PENDING RESERVATION";

    $arrival=mysql_real_escape_string($_POST['CheckIn4']);
    $departure=mysql_real_escape_string($_POST['CheckOut4']);
    $rmId=mysql_real_escape_string($_POST['rmId']);
    $pending="YES";
    $reserved="NO";
    $occupied="NO";
    $days=mysql_real_escape_string($_POST['Days4']);
    $rmRate=mysql_real_escape_string($_POST['rmRate']);
    $totAmt=mysql_real_escape_string($_POST['TotalAmt4']);
    $resDate= date("Y-m-d H:i:s");
    $status= "PENDING";
    $last_id = $conn->insert_id;

    $payment =0.00;
    $deposit =0.00;
    $balance =mysql_real_escape_string($_POST['Balance4']);

    $mysql="INSERT INTO tblcustomer1(CUST_ID,FNAME,MNAME,LNAME,ADDRESS,EMAIL,MOBILE,STATUS) VALUES('last_id','$fname','$mname','$lname','$address','$email','$mobile','$status')";
    $mysql1="INSERT INTO tblreservation(RES_ID,RM_ID,CUST_ID,PENDING,RESERVED,OCCUPIED,DAYS,CHECK_IN,CHECK_OUT,RM_RATE,RES_DATE,STATUS) VALUES('$last_id','$rmId','$custId','$pending','$reserved','$occupied','$days','$arrival','$departure','$rmRate','$resDate','$status')";
    $mysql2="INSERT INTO tblsales(SALES_ID,RES_ID,PAYMENT,DEPOSIT,BALANCE) VALUE('$last_id','$resId','$payment','$deposit','$balance')";
    $result = mysql_query($mysql,$conn)or die(mysql_error());
    $result1 = mysql_query($mysql1,$conn)or die(mysql_error());
    $result2 = mysql_query($mysql2,$conn)or die(mysql_error());
        if($result) AND ($result1) AND ($result2){
            header('Location: resInfo.php');
        }
        else
             echo "ERROR";
        mysql_close($conn);
?>

我也希望tblcustomer中的CUST_ID在tblreservation和tblsales中是相同的。任何人都可以帮助我摆脱这个......请提前感谢。

0 个答案:

没有答案