0

I am having difficulty with this piece of code, I have added the code for the url pieces to the $query_params array, and if there is any empty values in any of the fields, to fill with the string 'Empty', and then to save/append to the file. The problem is if some variables are missing in the form or there is empty values, it does not add the 'Empty' string to that particular part of the record that is saved to file.
Here is the code:

<?php
include 'curr.php';
$url = curPageURL();
$query_str = parse_url($url, PHP_URL_QUERY);
$query = parse_str($query_str, $query_params);
#authenticate the user name and password
if (in_array("password", $query_params) && in_array("usrer1", $query_params)) {
    for( $i=0; $i < 30; $i++ )
        if(empty($query_params[i])) { 
            $query_params[i] = 'Empty'; 
            }
    $file = new SplFileObject("file.csv", "a");
    $written = $file->fputcsv($query_params);
    echo "TRUE";
}
else{
    echo "UNAUTHORIZED";
}
?>

Any help would be appreciated.

1
  • Can you please post the HTML for your form? Also, where are you getting $query_params from? It just appears in your code. Commented Aug 26, 2014 at 10:45

1 Answer 1

1

You should have a list of all possible keys and do something like this:

$possible_keys = array('password', 'usrer1', 'c', 'd');
foreach($possible_keys as $key) {
    if (!isset($query_params[$key]) || empty($query_params[$key]))
        $query_params[$key] = 'Empty';
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, thanks for the help. I have used the code listed above but when I omit fields from the url it does not fill that part of the list with the string 'empty', it moves the values to the left of the list

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.