So basically, I have a CSV file that will be uploaded but not stored on the server and PHP will pull data from it and update a database accordingly.
My Issue is that I am trying to skip the row in the CSV if there is already an entry found in the database but it stops on the first error and does not skip.
Line 62, I added a comment which is where I am trying to get this accomplished.
the ELSE statement after the if (($update == 1) && ($update2 == 1)) has a continue in it, meaning if update and update2 do not == 1 then skip, or so I would have thought but it just stops after the first duplicate serial number is found.
Any help is GREATLY appreciated
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>";
}
}
}
}
}
If you want to show all errors, you should use an array, and append the error text to that array. Then use foreach loop on client part to display all errors.
If you use a single variable it will always be what you set it to most recently. For multiple data, you should use an array, or append to string like this: $string .= "appended string";
but for this case I recommend using arrays.