代码之家  ›  专栏  ›  技术社区  ›  Marz_89

PHP Update MySQL from CSV跳过错误行

  •  0
  • Marz_89  · 技术社区  · 6 年前

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

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

    if(($update==1)&($update2==1))后面的ELSE语句中有一个continue,这意味着如果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 回复  |  直到 6 年前
        1
  •  1
  •   cagri    6 年前

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

    如果您使用单个变量,它将始终是您最近设置的变量。对于多个数据,应使用数组,或像这样附加到字符串: $string .= "appended string"; 但对于这种情况,我建议使用数组。