以一种形式提交两个数据表

时间:2017-06-15 15:26:33

标签: php jquery mysql forms

我有一张包含两张桌子的表格。每个表都能添加表行。这意味着使用isset $ _POST提交表单时使用“for”对吗?我关心的是如何在单个数据库表中提交包含两个表(它们都具有函数添加表行)的单个表单。两个表都包含略有不同的属性,但在同一个数据库表中。我正在搜索一个星期,但还没找到解决方案。我只能提交一个包含一个表格的表格(其中包含添加表格行)。如果有两个表但是插入到同一个数据库表中怎么样?

if(isset($_POST['submitbtn'])){
        //others
        $merchant   = $_POST['merchant'];
        $remark = $_POST['remark'];
        $docno  = $_POST['docno'];
        $category = $_POST['category'];
        $claim_amount   = $_POST['claim_amount'];

        //airfare
        $airline    = $_POST["airline"];
        $origin = $_POST["origin"];
        $destination = $_POST["destination"];
        $descript = $_POST["desc"];
        $docno_air = $_POST["docno_air"];
        $category_air = $_POST["category_air"];
        $claim_amount_fare  = $_POST["claim_amount_fare"];

        $email = $ColUser['Email'];
        $euser  = $_SESSION['UserId'];
        $reportName = $_POST['reportname'];
        $start_date = date("Y-m-d");
        $status="open";
        $purpose = $_POST["purpose"];

        //get approval
        $getapproval = "SELECT * FROM `approve_by` WHERE `user_id` = '".$_SESSION['UserId']."'";
        $approvalget = mysqli_query($mysqli,$getapproval);
        $appid = mysqli_fetch_assoc($approvalget);
        $app1 = $appid['approval_1'];
        $app2 = $appid['approval_2'];
        $app3 = $appid['approval_3'];

        //generate travel id
        $travelprefix= "SELECT * FROM `prefix` WHERE `description` = 'Travel'";
        $result_travel = mysqli_query($mysqli, $travelprefix);
        $travel_info = mysqli_fetch_assoc($result_travel);

        $travel_pre = $travel_info['prefix'];
        $travel_old_num = $travel_info['running_number'] + 1;
        $travel_new = sprintf("%05d", $travel_old_num);
        $travel_date_append = date('mY');
        $travel_number = $travel_pre."-".$travel_date_append."-".$travel_new;

        //generate report id
        $reportprefix= "SELECT * FROM `prefix` WHERE `description` = 'REPORT'";
        $result_report = mysqli_query($mysqli, $reportprefix);
        $report_info = mysqli_fetch_assoc($result_report);

        $report_pre = $report_info['prefix'];
        $report_old_num = $report_info['running_number'] + 1;
        $report_new = sprintf("%05d", $report_old_num);
        $report_date_append = date('mY');
        $report_number = $report_pre."-".$report_date_append."-".$report_new;

    if (!empty($_POST['airline']))
    {

    for ($i = 0; $i < count($_POST["airline"]); $i++){

        $airline    = $_POST["airline"][$i];
        $origin = $_POST["origin"][$i];
        $destination = $_POST["destination"][$i];
        $descript = $_POST["desc"][$i];
        $docno_air = $_POST["docno_air"][$i];
        $category_air = $_POST["category_air"][$i];
        $claim_amount_fare  = $_POST["claim_amount_fare"][$i];

        //$upload_dir = 'upload';
        //$targetPath = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . $upload_dir . DIRECTORY_SEPARATOR;
        $targetPaths="upload/";
        $filefare = $targetPaths.rand(1000,100000)."-".$_FILES['bill_image_air']['name'][$i];
        $file_loc = $_FILES['bill_image_air']['tmp_name'][$i];
        $file_basename = substr($filefare, 0, strripos($filefare, '.'));
        //$flink='http://localhost/new_exp/'.$file;
        move_uploaded_file($file_loc,$filefare);

        $mrecordprefix= "SELECT * FROM `prefix` WHERE `description` = 'Category'";
        $mresult_record = mysqli_query($mysqli, $mrecordprefix);
        $mrecord_info = mysqli_fetch_assoc($mresult_record);

        $mrecord_pre = $mrecord_info['prefix'];
        $mrecord_old_num = $mrecord_info['running_number'] + 1;
        $mrecord_new = sprintf("%05d", $mrecord_old_num);
        $mrecord_date_append = date('mY');
        $mrecord_number = $mrecord_pre."-".$mrecord_date_append."-".$mrecord_new;

        $save_running_records = "UPDATE `prefix` SET `running_number`=? WHERE `description` = 'Category'";
        $save_running_report = "UPDATE `prefix` SET `running_number`=? WHERE `description` = 'Report'";
        $save_running_travel = "UPDATE `prefix` SET `running_number`=? WHERE `description` = 'Travel'";
        $save_new_record = "INSERT INTO `report`(`reportname`, `report_ref`, `UserId`, `CategoryId`, `travel_record`, `remark`, `claim_amount`, `bill_image`, `bill_image_type`, `docno`, `origin`, `airline`, `destination`, `email_from`, `approval_1`, `approval_2`, `approval_3`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        $save_count = "INSERT INTO `categorycount`(`CategoryId`, `amount`, `userid`, `categoryno`) VALUES (?, ?, ?, ?)";

        $stmt6 = $mysqli->prepare($save_running_records);
        $stmt4 = $mysqli->prepare($save_running_report);
        $stmt2 = $mysqli->prepare($save_running_travel);        
        $stmt5 = $mysqli->prepare($save_new_record);
        $stmt7 = $mysqli->prepare($save_count);


        $stmt6->bind_param('s', $mrecord_old_num);
        $stmt4->bind_param('s', $report_old_num);
        $stmt2->bind_param('s', $travel_old_num);
        $stmt5->bind_param('ssiisssbssssssiii', $reportName, $report_number, $euser, $category_air, $travel_number, $descript, $claim_amount_fare, $filefare, $filefare, $docno_air, $origin, $airline, $destination, $email, $app1, $app2, $app3);
        $stmt7->bind_param('isis', $category_air, $claim_amount_fare, $euser, $mrecord_number);

        if ($stmt5->execute() == false){
            echo 'Fifth query failed: ' . $mysqli->error;
                } else {
                    if ($stmt2->execute() == false){
                    echo 'gl B query failed: ' . $mysqli->error;    
                    } else {
                        if ($stmt4->execute() == false){
                            echo 'gl B query failed: ' . $mysqli->error;
                        } else {
                            if ($stmt7->execute() == false) {
                                echo 'gl B query failed: ' . $mysqli->error;
                            } else {
                                    if($stmt6->execute() == false){
                                        echo 'gl C query failed: ' . $mysqli->error;
                                    }
                                $stmt6->close();
                                }
                            $stmt7->close();
                            }
                        $stmt4->close();
                        }
                    $stmt2->close();    
                }
            $stmt5->close();
        }
    }

airfare table image others expenses table image

我现在的问题是如何处理其他费用标签中的表是否为空?因为当我尝试提交表格并填写所有输入,除了其他费用标签表,我让它为空。当我提交表单时,其他费用选项卡表中的空行也插入数据库表中。

0 个答案:

没有答案