PHP在错误时从CSV跳过行更新MySQL

时间:2018-09-06 22:43:32

标签: php mysqli fgetcsv

因此,基本上,我有一个CSV文件,该文件将被上载但不存储在服务器上,PHP将从其中提取数据并相应地更新数据库。

我的问题是,如果数据库中已经找到一个条目,但我尝试跳过CSV中的行,但是它会在出现第一个错误时停止并且不会跳过。

第62行,我添加了一条评论,这是我试图实现这一目标的地方。

if(($ update == 1)&&($ update2 == 1))之后的ELSE语句中有一个继续语句,这意味着如果update和update2不等于= 1则跳过,否则我将想,但只是在找到第一个重复的序列号后才停止。

我们非常感谢您的帮助

    public function upload() {
    $this->data['token'] = $this->session->data['token'];
    $connect = mysqli_connect("localhost", "username", "password", "database");
    $this->load->model('setting/mail');
    if (isset($_POST["upload"])) {
        if ($_FILES['update_cases']['name']) {
            $filename = explode(".", $_FILES['update_cases']['name']);
            if (end($filename) == "csv") {
                $handle = fopen($_FILES['update_cases']['tmp_name'], "r");
                fgetcsv($handle);
                $this->load->model('sale/order');
                while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                    print "row start<br>";
                    $order_id = mysqli_real_escape_string($connect, $data[0]);
                    $product_sn = mysqli_real_escape_string($connect, $data[1]);
                    $customer_email = mysqli_real_escape_string($connect, $data[2]);
                    $status = mysqli_real_escape_string($connect, $data[13]);
                    $rma_number = mysqli_real_escape_string($connect, $data[17]);
                    $rma_type = mysqli_real_escape_string($connect, $data[18]);
                    $planned_product = mysqli_real_escape_string($connect, $data[19]);
                    $tur = mysqli_real_escape_string($connect, $data[20]);
                    $pi = mysqli_real_escape_string($connect, $data[21]);
                    $cir = mysqli_real_escape_string($connect, $data[22]);
                    $cmr = mysqli_real_escape_string($connect, $data[23]);
                    $waive_return = mysqli_real_escape_string($connect, $data[24]);
                    $replacement_tracking = mysqli_real_escape_string($connect, $data[26]);
                    $inventory = mysqli_real_escape_string($connect, $data[27]);
                    $replacement_sn = mysqli_real_escape_string($connect, $data[28]);
                    $replacement_sn2 = mysqli_real_escape_string($connect, $data[29]);
                    $qty_shipped = mysqli_real_escape_string($connect, $data[33]);
                    $date_shipped = mysqli_real_escape_string($connect, $data[35]);
                    $result1 = $this->model_sale_order->getOrderById($order_id);
                    $current_status = $result1['order_status'];
                    $rma_num = $result1['order_rma'];
                    $customer_id = $result1['cus_id'];
                    $regpro_id = $result1['regpro_id'];
                    $update = 0;
                    $update2 = 0;
                    $batch_data = array(
                        "order_id" => $order_id, 
                        "rpl_tracking" => $replacement_tracking, 
                        "qty_shipped" => $qty_shipped, 
                        "replacement_sn" => $replacement_sn, 
                        "replacement_sn2" => $replacement_sn2, 
                        "inventory" => $inventory, 
                        "rma_type" => $rma_type, 
                        "pi_num" => $pi, 
                        "tur_num" => $tur, 
                        "cir_num" => $cir, 
                        "cmr_num" => $cmr, 
                        "waive_return" => $waive_return, 
                        "update_status" => $status, 
                        "date_shipped" => $date_shipped, 
                        "pre_status" => $current_status, 
                        "comment" => $planned_product, 
                        "planned_product" => $planned_product
                    );
                    if ($qty_shipped !== 0) {
                        $this->load->model('catalog/product');
                        $this->load->model('catalog/regproduct');
                        // If Two replacement products
                        if ($qty_shipped == 2) {
                            //Check if Serial Number Already Exists (If exists, I want the script to skip this row and move onto the next row in the excel sheet)
                            $check_sn = $this->model_catalog_regproduct->checkSNBelong2($replacement_sn);
                            $check_sn2 = $this->model_catalog_regproduct->checkSNBelong2($replacement_sn2);
                            if ($check_sn) {
                                $update = 0;
                                $this->error['error_replacement_sn'] = "SN " . $replacement_sn . " in use!";
                            } else {
                                $update = 1;
                            }
                            if ($check_sn2) {
                                $update2 = 0;
                                $this->error['error_replacement_sn2'] = "SN " . $replacement_sn2 . " in use!";
                            } else {
                                $update2 = 1;
                            }
                            if (($update == 1) && ($update2 == 1)) {
                                $replacement_product = $this->model_catalog_product->getProductBySN($replacement_sn);
                                $replacement_product2 = $this->model_catalog_product->getProductBySN($replacement_sn2);
                                $defective_product_warranty = $this->model_catalog_regproduct->getRegproductById($customer_id, $regpro_id);
                                $warr_date = $defective_product_warranty['regpro_warr_date'];
                                $replacement_model = $replacement_product['m_type'];
                                $replacement_model2 = $replacement_product2['m_type'];
                                $replacement_family = $replacement_product['f_type'];
                                $replacement_family2 = $replacement_product2['f_type'];
                                $this->model_catalog_regproduct->addRegproductReplacement2($customer_id, $replacement_sn2, $replacement_family2, $replacement_model2, $warr_date);
                                $this->model_catalog_regproduct->addRegproductReplacement($customer_id, $replacement_sn, $replacement_family, $replacement_model, $warr_date);
                                $this->model_sale_order->confirmOrder3($this->user->getId(), $batch_data);
                                if (((int)$current_status) !== ((int)$status)) {
                                    if ((int)$status == 210) {
                                        if ($rma_type != "Standard") {
                                            $template = $this->model_setting_mail->getTemplateByLabel('RMA_PRODUCT_RECEIVED_ADVANCED');
                                        } elseif ($rma_type == "Standard") {
                                            $template = $this->model_setting_mail->getTemplateByLabel('RMA_PRODUCT_RECEIVED_STANDARD');
                                        }
                                    } elseif ((int)$status == 230) {
                                        if ($rma_type != "Standard") {
                                            $template = $this->model_setting_mail->getTemplateByLabel('RMA_REPLACEMENT_PRODUCT_SHIPPED_ADVANCED');
                                        } elseif ($rma_type == "Standard") {
                                            $template = $this->model_setting_mail->getTemplateByLabel('RMA_REPLACEMENT_PRODUCT_SHIPPED_STANDARD');
                                        }
                                    } elseif ((int)$status == 500) {
                                        $template = $this->model_setting_mail->getTemplateByLabel('RMA_CLOSED');
                                    }
                                    if ((int)$template['email_status'] == 1) {
                                        $subject = $template['email_subject'];
                                        $message = $template['email_content'];
                                        // Get Customer Email
                                        $this->load->model('sale/customer');
                                        $order_info = $this->model_sale_customer->getCustomerByEmail($customer_email);
                                        $customer_info = $this->model_sale_customer->getCustomer($order_info['cus_id']);
                                        $email = $customer_info['cus_username'];
                                        $this->load->model('sale/order');
                                        $result_tracking = $this->model_sale_order->getOrderById($order_id);
                                        $replacement_tracking = $result_tracking['order_return_tracking_num'];
                                        $message = str_replace('%FIRSTNAME%', $customer_info['cus_firstname'], $message);
                                        $message = str_replace('%LASTNAME%', $customer_info['cus_lastname'], $message);
                                        $message = str_replace('%RMA%', $rma_num, $message);
                                        $message = str_replace('%TRACKING%', $replacement_tracking, $message);
                                        $mail = new Mail();
                                        $mail->protocol = $this->config->get('mail_protocol');
                                        $mail->hostname = $this->config->get('smtp_host');
                                        $mail->username = $this->config->get('smtp_username');
                                        $mail->password = $this->config->get('smtp_password');
                                        $mail->port = $this->config->get('smtp_port');
                                        $mail->timeout = $this->config->get('smtp_timeout');
                                        $mail->setTo($email);
                                        $mail->setFrom($this->config->get('sender_email'));
                                        $mail->setSender($this->config->get('sender_name'));
                                        $mail->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'));
                                        $mail->setText(html_entity_decode($message, ENT_QUOTES, 'UTF-8'));
                                        try {
                                            $mail->send();
                                        }
                                        catch(Exception $e) {
                                            $this->error['warning'] = $e->getMessage();
                                        }
                                    }
                                }
                                $this->session->data['success'] = $this->language->get('text_success');
                                //$this->redirect($this->url->link('report/sale_return', 'token=' . $this->data['token'], 'SSL'));

                            } else {
                                print $update."<br>";
                                print $update2."<br>";
                                print "Errors<br>";
                                continue;
                            }
                            //row start
                            //0
                            //0
                            //Errors
                            //row start
                            //0
                            //0
                            //Errors
                            //row start
                            //row end
                            //row start
                            //row end
                            //row start
                            //row end
                        } else if ($qty_shipped == 1) {
                            // will do something else
                        } else if ($qty_shipped == 0) {
                            // will also do something else
                        }
                    } else if (!isset($qty_shipped)) {
                        // will also do something else
                    }
                    print "row end<br>";
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

如果要显示所有错误,则应使用一个数组,并将错误文本附加到该数组。然后在客户端部分使用foreach循环显示所有错误。

如果您使用单个变量,它将始终是您最近设置的变量。对于多个数据,您应该使用数组,或附加到以下字符串:@{ Layout = "_Layout"; } @await Component.InvokeAsync("Products", new { date = "2018-09-05" }) @section scripts { @await Html.PartialAsync("_DataTableScriptsPartial") } ,但在这种情况下,我建议使用数组。