CSV导入验证

I am trying to set up an import/export to MySQL for a CSV file. I have most of it, however I am trying to validate the information. When I validate I want none of the records to be imported to MySQL. The code I currently have will only not import any records after an empty field. I wouldn't normally ask but I am stumped.

<?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);

?>

Okay, let's see:

// 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
    }
}

This script will insert values which are not empty. But still it will ignore rows with empty values. If you want to check if all fields in all rows of your csv are not empty, then you should do this:

// 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
}

So if you want to check