【问题标题】:Insert into one Table, while updating another & File Upload插入一个表,同时更新另一个表和文件上传
【发布时间】:2017-01-16 09:35:53
【问题描述】:

我正在尝试更新一个表中的数据,同时从 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 个文件的上传(扫描的许可证、扫描的许可更新、扫描的身份文件)。我找到了下面的代码来处理这个问题,但我不确定如何实现它。

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

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

【问题讨论】:

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


    【解决方案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();
            ?>
    

    【讨论】:

    • 感谢以上。我根据您的示例更新了代码,现在收到一条新的错误消息:可捕获的致命错误:无法将类 mysqli_stmt 的对象转换为字符串。错误指的是这一行:回显“错误:无法执行$q1。” mysqli_error($link);
    • MySQLi,主要是因为我对 PDO 一点也不熟悉。不过,据我了解,您不能像我尝试的那样在 MySQLi 上运行多个查询。
    • PDO 使用 bindValue,而 mysqli 使用 bind_param:php.net/manual/en/mysqli-stmt.bind-param.php
    • 检查您是否在“sdsssssssssd”中传递了正确的数据类型,即 i - integer,d - double,s - string, b - BLOB
    • 所有字段都是您列出的字符串。我尝试更改但仍然出现相同错误的唯一错误是在 outlet_number 上,它是一个整数,所以基本上我将“d”替换为“i”,并将它们尝试为“s”。
    【解决方案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);
    }
    }
    

    【讨论】:

    • 我是把这段代码放在 HTML 文件中,还是也放在 PHP“提交”文件中?我要把它放在哪里?
    • 如果你想在提交表单后上传文件,那么它也进入PHP提交。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    • 2017-08-01
    • 1970-01-01
    • 2013-08-15
    • 1970-01-01
    相关资源
    最近更新 更多