&& does not work for required form fields

I want the user to fill in all the fields provided on the form and not need empty entries, and to prevent this from happening, I inserted a lot of && to check if the form fields have some input defined by the user, but the form still accepts empty fields. Please, help.

if (isset($_POST['submit']))
{

    if (isset($_POST['pin']) &&
        isset($_POST['amount']) &&
        isset($_POST['purpose']) &&
        isset($_POST['email'])&&
        isset($_POST['pwd'])&&
        isset($_POST['fname']) &&
        isset($_POST['mname']) &&
        isset($_POST['lname']) &&
        isset($_POST['address']) &&
        isset($_POST['city']) &&
        isset($_POST['state']) &&
        isset($_POST['estatus']) &&
        isset($_POST['yincome']) &&
        isset($_POST['dob']))

    {
        $pin = get_post($conn, 'pin');
        $amount = get_post($conn, 'amount');
        $purpose = get_post($conn, 'purpose');
        $email = get_post($conn, 'email');
        $pwd = get_post($conn, 'pwd');
        $fname = get_post($conn, 'fname');
        $mname = get_post($conn, 'mname');
        $lname = get_post($conn, 'lname');
        $address = get_post($conn, 'address');
        $city = get_post($conn, 'city');
        $state = get_post($conn, 'state');
        $estatus = get_post($conn, 'estatus');
        $yincome = get_post($conn, 'yincome');
        $dob = get_post($conn, 'dob');


        $query = "INSERT INTO borrowers VALUES" .
            "('$pin', '$amount', '$purpose', '$email', '$pwd', '$fname', '$mname', '$lname', '$address', '$city', '$state', '$estatus', '$yincome', '$dob')";
        $result = $conn->query($query);
        if (!$result) echo "INSERT failed: $query<br>" . $conn->error . "<br><br>";
    }
}
+4
source share
1 answer

You should replace issetwith emptybecause it issetchecks to see if the variable exists, and not populated. See below:

Before:

isset($_POST['pin'])

After:

!empty($_POST['pin'])

One more thing: use the prepared statement in your sql query. This will avoid sql injection. See this link:

http://php.net/manual/en/book.pdo.php

+3

Source: https://habr.com/ru/post/1650401/


All Articles