mysql LOAD DATA INFILE排除不受欢迎的行

时间:2017-12-02 07:34:32

标签: php mysql

我尝试导入所有产品行并排除不必要的行。我的下面的代码正在运行,但它正在导入我想要排除的行。我现在正在尝试使用LOAD DATA INFILE来选择我要导入的行。有什么帮助吗?

if(!empty($_FILES['file']['name']) && in_array($_FILES['file']['type'],$file_mimes)){
        if(is_uploaded_file($_FILES['file']['tmp_name'])){
            $csv_file = fopen($_FILES['file']['tmp_name'], 'r');

                fgetcsv($csv_file, 1000, ",");
                fgetcsv($csv_file, 1000, ",");
                fgetcsv($csv_file, 1000, ",");
                fgetcsv($csv_file, 1000, ",");
                fgetcsv($csv_file, 1000, ",");
                fgetcsv($csv_file, 1000, ",");
                fgetcsv($csv_file, 1000, ",");
                fgetcsv($csv_file, 1000, ",");
                fgetcsv($csv_file, 1000, ",");
                fgetcsv($csv_file, 1000, ",");
                fgetcsv($csv_file, 1000, ",");


                while(($emp_record = fgetcsv($csv_file, 1000, ",")) !== FALSE){
                    $sql_query = "SELECT * FROM ppmp_supply_order WHERE order_stock_no = '".$emp_record[1]."'";
                    $resultset = mysqli_query($con, $sql_query) or die("database error:". mysqli_error($con));

                    if(mysqli_num_rows($resultset)) {
                        $sql_update = "UPDATE ppmp_supply_order set 
                                            prefix = '".$emp_record[0]."',
                                            order_stock_no='".$emp_record[1]."', 
                                            order_unit='".$emp_record[2]."' , 
                                            order_name='".$emp_record[3]."' , 
                                            order_current_price='".$emp_record[5]."' , 
                                            order_unit_price='".$emp_record[6]."' ,
                                            order_qty='".$emp_record[7]."',
                                                WHERE order_stock_no = '".$emp_record[1]."'";
                        mysqli_query($con, $sql_update) or die("database error:". mysqli_error($con));
                    } else{
                        $mysql_insert = "INSERT INTO ppmp_supply_order (prefix, order_stock_no, order_unit, order_name, order_current_price, order_unit_price, order_qty )
                                            VALUES('".$emp_record[0]."','".$emp_record[1]."', '".$emp_record[2]."', '".$emp_record[3]."', '".$emp_record[5]."', '".$emp_record[6]."', '".$emp_record[7]."')";
                        mysqli_query($con, $mysql_insert) or die("database error:". mysqli_error($con));
                    }

                }
                fclose($csv_file);

This is what i want as an output

This is my result so far

This is the csv file im trying to import

1 个答案:

答案 0 :(得分:0)

无法告诉LOAD DATA INFILE忽略随机行。可以告诉它跳过一定数量的标题行,但这就是全部。

如果您需要忽略某些行,可以将数据导入临时表,然后使用INSERT ... SELECT将所需的行复制到最终表中。