lundi 5 février 2018

How can I check if all variables meet the requirements to put data into file?

I need to check if all variables meet the requirements if they do proceed with file handling.

$fname = $_POST['fname'];
$lname = $_POST['lname'];
$s_id = $_POST['student_id'];
$tuition = $_POST['tuition'];
$payment = $_POST['payment'];

//Checking for Errors
if ($_SERVER["REQUEST_METHOD"] == "POST"){
    //Checking Values
    echo '<ul class="error">';
    if(strlen($fname)<2){
        echo '<li>'."First name must be 2 or more characters in length".'</li>';
    }
    if(strlen($lname)<3 || strlen($lname)>12){
        echo '<li>'."Last name must be between 3 and 12 characters in length".'</li>';
    }
    if(strlen($s_id)!=9){
        echo '<li>'."Student id must be exactly 9 characters in length".'</li>';
    }
    if($tuition < 2000 || $tuition > 10000){
        echo '<li>'."Tuition must be between 2000 and 10000".'</li>';
    }
    echo '</ul>';

This parts work as it should it reports what errors have been encountered. Following this I need to make sure that all values are correct if they are output the success message and put data into file.

//Success
    if($fname == true && $lname == true && $s_id == true && $tuition == true){
        echo '<ul class="success">';
        echo '<li>'."Payment Successful!".'</li>';
        echo '</ul>';

        //File Handling
        $line = array($fname, $lname, $s_id, $tuition, $payment); //Creates a line to append to the file.
        $handle = fopen("log.txt", "a+"); //Open for reading and writing; place the file pointer at the end of the file.
        fputcsv($handle, $line); //Puts the values into the file.
        fclose($handle); //Close the file.
    }
}

I'm having problem checking if all variable meet the requirements I'm using if($fname == true && $lname == true && $s_id == true && $tuition == true), but it seems to go though even with errors. What exactly am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire