插入一个表格,同时更新另一个表格文件上传

时间:2017-01-16 09:35:53

标签: php file-upload insert mysqli-multi-query

我正在尝试更新一个表中的数据,同时从PHP表单向第二个表中插入一个新行。

点击提交时,我收到一条错误消息:

  

未捕获错误:调用未定义的方法mysqli :: exec()

以下是我对PHP提交表单的查询:

    $link->exec("SET CHARACTER SET utf8");      // Sets encoding UTF-8

try {
    $link->beginTransaction();

    $q1 = $link->prepare("INSERT INTO 
                            liquor_licenses_2
                            (   username
                            ,   outlet_number
                            ,   license_date
                            ,   license
                            ,   scanned_license
                            ,   permit_renewal
                            ,   scanned_permit_renewal
                            ,   identity_document
                            ,   scanned_identity_document
                            ) 
                            VALUES 
                            (   :username
                            ,   :outlet_number
                            ,   :license_date
                            ,   :license
                            ,   :scanned_license
                            ,   :permit_renewal
                            ,   :scanned_permit_renewal
                            ,   :identity_document
                            ,   :scanned_identity_document
                            ");
    $q1->bindValue(':username', $username);
    $q1->bindValue(':outlet_number', $outlet_number);
    $q1->bindValue(':license_date', $license_date);
    $q1->bindValue(':liquor_license', $license);
    $q1->bindValue(':scanned_license', $scanned_license);
    $q1->bindValue(':permit_renewal', $permit_renewal);
    $q1->bindValue(':scanned_permit_renewal', $scanned_permit_renewal);
    $q1->bindValue(':identity_document', $identity_document);
    $q1->bindValue(':scanned_identity_document', $scanned_identity_document);
    $q1->execute();

    $q2 = $link->prepare("UPDATE 
                            outlet_details
                                SET 
                                username = :username
                            ,   outlet_number = :outlet_number                          
                            ,   mega_region = :mega_region 
                            ,   outlet_name = :outlet_name 
                            ,   address_0 = :address_0 
                            ,   address_1 = :address_1 
                            ,   address_2 = :address_2 
                            ,   address_3 = :address_3 
                            ,   address_4 = :address_4 
                            ,   contact_number_1 = :contact_number_1 
                            ,   contact_number_2 = :contact_number_2 
                        WHERE 
                            outlet_number = :outlet_number");
    $q1->bindValue(':username', $username);
    $q1->bindValue(':outlet_number', $outlet_number);                           
    $q2->bindValue(':mega_region', $mega_region);
    $q2->bindValue(':outlet_name', $outlet_name);
    $q2->bindValue(':address_0', $address_0);
    $q2->bindValue(':address_1', $address_1);
    $q2->bindValue(':address_2', $address_2);
    $q2->bindValue(':address_3', $address_3);
    $q2->bindValue(':address_4', $address_4);
    $q2->bindValue(':contact_number_1', $contact_number_1);
    $q2->bindValue(':contact_number_2', $contact_number_2);
    $q2->execute();


        $link->commit();
    } catch (Exception $e) {
        $link->rollback();
    }




    if(mysqli_query($link, $q1)){
        echo "Records added / updated successfully.";
    } else{
        echo "ERROR: Could not able to execute $q1. " . mysqli_error($link);
    }

    header("refresh:2;url=../outlet_capture.php"); 
    // close connection
    mysqli_close($link);
    ?>

我还想添加3个文件的上传(扫描许可证,scanning_permit_renewal,scanning_identity_document)。我找到了以下代码来处理这个问题,但我不确定如何实现它。

foreach($_FILES['file'] AS $key=>$file) {
    $filename = $file['tmp_name'];
    $size = $file['size'];
    $newfile = $_SERVER['DOCUMENT_ROOT'] . "/uploads/" . date("Ymd_his") . "_" . $filename;
    move_uploaded_file($filename, $newfile);
}

我需要将文件名存储在我的数据库中,以便我可以将其引用到已上传到服务器的文件中。

2 个答案:

答案 0 :(得分:1)

       $link->set_charset("utf8"); 
    try {
         $link->begin_transaction();  
        $q1 = $link->prepare("INSERT INTO liquor_licenses_2
                                (   username
                                ,   outlet_number
                                ,   license_date
                                ,   license
                                ,   scanned_license
                                ,   permit_renewal
                                ,   scanned_permit_renewal
                                ,   identity_document
                                ,   scanned_identity_document
                                ) 
                                VALUES 
                                (   ?,?,?,?,?,?,?,?,?)");
                                                   --^--(you missed closed bracket here)
        $q1->bind_param("sdsssssss", $username, $outlet_number, $license_date,$license,$scanned_license,$permit_renewal,$scanned_permit_renewal,$identity_document,$scanned_identity_document);
        $res1=$q1->execute();
        $q2 = $link->prepare("UPDATE 
                                outlet_details
                                    SET 
                                    username = ?
                                ,   outlet_number = ?                        
                                ,   mega_region = ?
                                ,   outlet_name = ?
                                ,   address_0 = ?
                                ,   address_1 = ?
                                ,   address_2 = ?
                                ,   address_3 = ?
                                ,   address_4 = ?
                                ,   contact_number_1 = ?
                                ,   contact_number_2 = ?
                            WHERE 
                                outlet_number = ?");
    $q2->bind_param("sdsssssssssd", $username, $outlet_number, $mega_region,outlet_name,$address_0,$address_1,$address_2,$address_3,$address_4,$contact_number_1,$contact_number_1,$outlet_number);

    $q2->execute();

            $link->commit();
        } catch (Exception $e) {
            $link->rollback();
        }

        if($res1){
            echo "Records added / updated successfully.";
        } else{
            echo "ERROR: Could not able to execute $q1. " . mysqli_error($link);
        }

        header("refresh:2;url=../outlet_capture.php"); 
        // close connection
       $link->close();
        ?>

答案 1 :(得分:0)

Testing <img class="chatEmote" tooltip="Testing the tooltip" data-direction="bottom" alt="Emote" src="https://emote.domain.com/emote73628"> Testing
Try this code for multiple file uploading: