0

我的导入脚本工作正常。在 $field_csv['imageUrlStr'] 中有一些不同大小的图像 url,我需要获取 400x400 字符串,其中包含我的图像 url 的 url,然后将其插入数据库,但是当我使用当前代码时,同一行得到多次插入。

请帮我。

<?php

    set_time_limit(2400);  // time out limit

    // Database Connection

    // Check connection
    if (mysqli_connect_errno()) {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $table_name = "table";
    $csv_file   = "import.csv"; // Name of your CSV file
    $csvfile    = fopen($csv_file, 'r');
    $field_csv  = array();

    $i = 0;
    while (($csv_data = fgetcsv($csvfile, 0, ",")) !== FALSE) {
        if($i==0) { 
            $i++;
            continue;
        }  // to exclude first line in the csv file.

    $field_csv['productId']     =mysqli_real_escape_string( $mysqli, $csv_data[0] );  // 1
    $field_csv['title']         = mysqli_real_escape_string( $mysqli, $csv_data[1] ); // 2
    $field_csv['description']   = mysqli_real_escape_string( $mysqli, $csv_data[2] ); // 3
    $field_csv['imageUrlStr']   = mysqli_real_escape_string( $mysqli, $csv_data[3] ); // 4
    $field_csv['mrp']           = mysqli_real_escape_string( $mysqli, $csv_data[4] ); // 5
    $field_csv['price']         = mysqli_real_escape_string( $mysqli, $csv_data[5] ); // 6
    $field_csv['productUrl']    = mysqli_real_escape_string( $mysqli, $csv_data[6] ); // 7
    $field_csv['categories']    = mysqli_real_escape_string( $mysqli, $csv_data[7] ); // 8
    $field_csv['productBrand']  = mysqli_real_escape_string( $mysqli, $csv_data[8] ); // 9
    $field_csv['deliveryTime']  = mysqli_real_escape_string( $mysqli, $csv_data[9] ); // 10
    $field_csv['inStock']       = mysqli_real_escape_string( $mysqli, $csv_data[10] ); // 11
    $field_csv['codAvailable']  = mysqli_real_escape_string( $mysqli, $csv_data[11] ); // 12
    $field_csv['emiAvailable']  = mysqli_real_escape_string( $mysqli, $csv_data[12] ); // 13
    $field_csv['offers']        = mysqli_real_escape_string( $mysqli, $csv_data[13] ); // 14
    $field_csv['discount']      = mysqli_real_escape_string( $mysqli, $csv_data[14] ); // 15
    $field_csv['cashBack']      = mysqli_real_escape_string( $mysqli, $csv_data[15] ); // 16
    $field_csv['size']          = mysqli_real_escape_string( $mysqli, $csv_data[16] ); // 17
    $field_csv['color']         = mysqli_real_escape_string( $mysqli, $csv_data[17] ); // 18
    $field_csv['sizeUnit']      = mysqli_real_escape_string( $mysqli, $csv_data[18] ); // 19
    $field_csv['sizeVariants']  = mysqli_real_escape_string( $mysqli, $csv_data[19] ); // 20
    $field_csv['colorVariants'] = mysqli_real_escape_string( $mysqli, $csv_data[20] ); // 21
    $field_csv['styleCode']     = mysqli_real_escape_string( $mysqli, $csv_data[21] ); // 22

    // Error Part
    $array   = explode(",", $field_csv['imageUrlStr']);
    $pattern = "/400x400/";

    foreach($array as $value) {
        $productNewImage = (preg_match($pattern,$value)) ? $value : "";
        echo $productNewImage;

    $query = "INSERT INTO $table_name 
        SET productId = '" . $field_csv['productId'] 
        . "', title = '" . $field_csv['title'] 
        . "', description = '" . $field_csv['description'] 
        . "',imageUrlStr = '" . $productNewImage . "',mrp = '" . $field_csv['mrp'] 
        . "',price = '" . $field_csv['price'] 
        . "',productUrl = '" . $field_csv['productUrl'] 
        . "',categories = '" . $field_csv['categories'] 
        . "',productBrand = '" . $field_csv['productBrand'] 
        . "',deliveryTime = '" . $field_csv['deliveryTime'] 
        . "',inStock = '" . $field_csv['inStock'] 
        . "',codAvailable = '" . $field_csv['codAvailable'] 
        . "',emiAvailable = '" . $field_csv['emiAvailable'] 
        . "',offers = '" . $field_csv['offers'] 
        . "',discount = '" . $field_csv['discount'] 
        . "',cashBack = '" . $field_csv['cashBack'] 
        . "',size = '" . $field_csv['size'] 
        . "',color = '" . $field_csv['color'] 
        . "',sizeUnit = '" . $field_csv['sizeUnit'] 
        . "',sizeVariants = '" . $field_csv['sizeVariants'] 
        . "',colorVariants = '" . $field_csv['colorVariants'] 
        . "',styleCode = '" . $field_csv['styleCode'] 
        . "' ";

        mysqli_query($mysqli,$query);

        } 
    }

    error_reporting(E_ALL);

    fclose($csvfile);

    echo "CSV data successfully imported to table!!";

    // close connection
    $mysqli->close();
4

1 回答 1

1

您当前正在为从值中提取的每个图像 URL 插入一行imageUrlStr,无论它是否与400x400模式匹配:

$array=explode(",", $field_csv['imageUrlStr'] );
$pattern = "/400x400/";

// Loop through all extracted image URLs
foreach($array as $value) {
    // This would be a 400x400 matching URL or an empty string
    $productNewImage = (preg_match($pattern,$value)) ? $value : "";

    // Insert a row with image URL or an empty string
    $query = "INSERT INTO ... " // Truncated for brevity
    mysqli_query($mysqli,$query);  
}

例如,如果您将imageUrlStrhttp://foo.bar/200x200/image1.png,http://foo.bar/400x400/image2.png数据库中插入两行。一个带有空图像 URL,另一个带有400x400URL,所有其他列的值都相同。

你可能会更好地获得对爆炸值$productNewImage使用preg_grep调用:imageUrlStr

$array = explode(",", $field_csv["imageUrlStr"]);
$pattern = "/400x400/";

// Get (first) URL matching pattern, or an empty string
@list($productNewImage) = array_values(preg_grep($pattern, $array));
$productNewImage = mysqli_real_escape_string($mysqli, $productNewImage);

// Insert one row with the image URL or empty string
$query = "INSERT INTO ... " // Truncated for brevity
mysqli_query($mysqli, $query);  
于 2015-06-03T20:26:59.743 回答