CSV导入验证


CSV Import validation

我正在尝试为CSV文件设置导入/导出到MySQL。我有大部分信息,但我正试图验证这些信息。当我验证时,我不希望将任何记录导入MySQL。我目前拥有的代码只会在一个空字段后不导入任何记录。我通常不会问,但我被难住了。

<?php
include 'connection.php';
$empty_value_found = false;
$file = $_FILES['file']['tmp_name'];
$handle = fopen ($file,"r");
while(($fileop = fgetcsv($handle,1000,",")) !==false){
    $first = trim($fileop[0]);
    $last = trim($fileop[1]);
    $birthday = trim($fileop[2]);
    $age = trim($fileop[3]);
    $address = trim($fileop[4]);

    if (
        empty($first) 
        || empty($last) 
        || empty($birthday) 
        || empty($age) 
        || empty($address) 
    ) {
        $empty_value_found = true;
        echo "empty field please check";
        break; // stop our while-loop
    }
}
// now we check - if there no empty values
if (!$empty_value_found) {
// we can go through our file again and insert values,
// code is similar to what you have
    $sql = mysqli_query($conn,"INSERT INTO `mytable` (first, last, birthday, age, address) VALUES ('$first','$last','$birthday','$age','$address')");
    $getdata =  "SELECT * FROM mytable";
    $results = mysqli_query($conn,$getdata);
    if(mysqli_num_rows($results) >=1){
        echo "<table><tr><th>First</th><th>Last</th><th>Birthday</th><th>Age</th> <th>Address</th></tr>";
    }
    while($row = mysqli_fetch_assoc($results)){
        echo "<tr><td>" . $row["first"]. "</td><td>" . $row["last"]. "</td><td>" . $row["birthday"]. "</td><td>" . $row["age"]. "</td><td>" . $row["address"].  "</td></tr>";
    }
}
echo "</table>";
mysqli_close($conn);
?>

好的,让我们看看:

// here you get an array from csv-string
while(($fileop = fgetcsv($handle,1000,",")) !==false){
    // first: use trim function to remove spaces from left and right of a value
    $first = trim($fileop[0]);
    $last = trim($fileop[1]);
    $birthday = trim($fileop[2]);
    $age = trim($fileop[3]);
    $address = trim($fileop[4]);
    // now you have five values.
    // u want to insert them to database only if they are ALL not empty
    // use function empty to check if value is empty
    if (!empty($first) 
        && !empty($last) 
        && !empty($birthday) 
        && !empty($age) 
        && !empty($address) 
    ) {
        $sql = mysqli_query($conn,"INSERT INTO `mytable` (first, last, birthday, age, address) VALUES ('$first','$last','$birthday','$age','$address')");
        // other code here
    }
}

该脚本将插入非空值。但是它仍然会忽略带有空值的行。如果您想检查csv的所有行中的所有字段是否为空,那么您应该这样做:

// set a special flag
$empty_value_found = false;
while(($fileop = fgetcsv($handle,1000,",")) !==false){
    // first: use trim function to remove spaces from left and right of a value
    $first = trim($fileop[0]);
    $last = trim($fileop[1]);
    $birthday = trim($fileop[2]);
    $age = trim($fileop[3]);
    $address = trim($fileop[4]);
    // now you have five values.
    // if any of them is empty - we should NOT go further and stop our cycle
    if (empty($first) 
        || empty($last) 
        || empty($birthday) 
        || empty($age) 
        || empty($address) 
    ) {
        $empty_value_found = true;
        break; // stop our while-loop
    }
}
// now we check - if there no empty values
if (!$empty_value_found) {
    // we can go through our file again and insert values,
    // code is similar to what you have
}
如果你想检查