Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, since it looks like you need to combine all of the conditionals with <code>||</code>, you can correct your program by writing it like this:</p> <pre><code>if ($f_naw['streetname'] == 1) { $streetname= $_POST['streetname']; //Used in INSERT query $cstreetname = $_POST['streetname'] == ''; //Used to check if field is empty $pstreetname = !preg_match($streetnameReg,$_POST['streetname']); //Used to check if it matches my regex } else { //These variables define variables if inputfields are not shown $streetname= ''; //No streetname means it's excluded in INSERT query $cstreetname = false; //Not needed in check $pstreetname = false; //Also not needed in check } if ($_POST['firstname'] == '' || $_POST['lastname'] == '' || $_POST['email'] == '' || $cstreetname || $cpostalcode || $chometown || $ctelnr || $csex || $cdateofbirth) { echo 'One of the fields is empty.'; header('refresh:3;url=index.php'); } </code></pre> <p>This would work, but it's unwieldy. A much better solution would be to use an array (let's name it <code>$errors</code> that gets dynamically populated with errors resulting from validating your fields. Like this:</p> <pre><code>$errors = array(); if ($f_naw['streetname'] == 1) { $streetname= $_POST['streetname']; //Used in INSERT query if ($streetname == '') { $errors[] = 'Streetname cannot be empty.'; // message is optional } if (!preg_match($streetnameReg,$streetname)) { $errors[] = 'Streetname is invalid.'; // message is optional } } </code></pre> <p>And then:</p> <pre><code>if ($errors) { echo 'There are errors with the data you submitted.'; header('refresh:3;url=index.php'); } </code></pre> <p>If you provided human-readable error messages you can also arrange for them to be displayed so that the user knows what they need to fix. And of course there are lots of variations of this technique you can use -- e.g. group the error messages by field so that you only show one error for each field.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload