插入到一个表中,同时更新另一个&上传文件 [英] Insert into one Table, while updating another & File Upload

查看:187
本文介绍了插入到一个表中,同时更新另一个&上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图更新一个表中的数据,同时从PHP表单插入到第二个表中。
$ b 当打到提交我收到一条错误消息:
$ b


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


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

  $ link-> exec(SET CHARACTER SET utf8); //设置编码UTF-8 

尝试{
$ link-> beginTransaction();
$ b $ 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
,:许可证
,::scanned_license
,::permit_renewal
,:sca nned_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();
$ b $ 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();




$ b if(mysqli_query($ link,$ q1)){
echo成功添加/更新记录 ;
} else {
echo错误:无法执行$ q1。 mysqli_error($链接);
}

header(refresh:2; url = .. / outlet_capture.php);
//关闭连接
mysqli_close($ link);
?>

我还想添加3个文件(扫描的许可证,scanned_permit_renewal,scanned_identity_document)的上传。

  foreach($ _ FILES [''文件'] AS $ key => $ file){
$ filename = $ file ['tmp_name'];
$ size = $ file ['size'];
$ newfile = $ _SERVER ['DOCUMENT_ROOT']。 / uploads /。日期(Ymd_his)。 _。 $文件名;
move_uploaded_file($ filename,$ newfile);

$ / code>

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

解决方案

  $ link- > set_charset( UTF8); 
尝试{
$ 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
(?,?,?,?,?,?,? ,?,?));
- ^ - (您在此处遗漏了括号)
$ 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成功添加/更新记录;
} else {
echo错误:无法执行$ q1。 mysqli_error($链接);
}

header(refresh:2; url = .. / outlet_capture.php);
//关闭连接
$ link-> close();
?>


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.

解决方案

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

这篇关于插入到一个表中,同时更新另一个&上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆