Insert into one Table, while updating another & File Upload

纵饮孤独 提交于 2019-12-25 09:16:47

问题


I am trying to update data in one table, and at the same time to insert a new row into a second table from a PHP form.

When hitting submit I get an error message saying:

Uncaught Error: Call to undefined method mysqli::exec()

Below is my query on the PHP submit form:

    $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);
    ?>

I also want to add to this the uploading of 3 files (scanned license, scanned_permit_renewal, scanned_identity_document). I have found the below code to handle this, but I am not sure how to implement it.

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);
}

I need the file name to be stored in my DB so that I can then reference it to the file that has been uploaded to the Server.


回答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();
        ?>



回答2:


Try this code for multiple file uploading:  

if (isset($_FILES["file"]["name"])) {
foreach($_FILES['file']['tmp_name'] as $key => $tmp_name)
{
    $file_name = $key.$_FILES['file']['name'][$key];
    $file_size =$_FILES['file']['size'][$key];
    $file_tmp =$_FILES['file']['tmp_name'][$key];
    $file_type=$_FILES['file']['type'][$key];  
    $new_file = $_SERVER['DOCUMENT_ROOT'] . "/uploads/" . date("Ymd_his") . "_" . $file_name;
    //echo $new_file;
    move_uploaded_file($file_tmp,$new_file);
}
}


来源:https://stackoverflow.com/questions/41673236/insert-into-one-table-while-updating-another-file-upload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!