Question

So my problem is (aside from being a noob) is that while in my studeis, im trying to check a couple of things for true/false and no matter how i set my code, if one parameter is met, it goes through as true even though the second parameter is false.

for example, i have a form im working on and i want to know if theres any data entered in the name/pass fields. if not, then redirect back to the form page.

heres whats happening:

html / pseudo:

<html and other header tags>
<form method="post" action="procfile.php">
  <table>
    <tr>
        <td>Username:</td>
        <td><input type="text" name="user" id="user"></td>
    </tr>
    <tr>
        <td>password:</td>
        <td><input type="text" name="password" id="password"></td>
    </tr>
        <tr>
        <td></td>
        <td><input type="submit" value="submit"></td>

    </tr>
</table>

</form>
</html>

as for the php, im checking for empty first, originally i had it like this:

<?php
if((empty($_POST['user']))&&(empty($_POST['password']))){
    header("Location:index.php"); /* Redirect browser */

exit;
}

?>

this doesnt work. i dont get syntax errors or anything...so, after a while i changed to longer version so from above it went to this.

<?php

if(empty($_POST['user'])){
    header("Location:index.php"); /* Redirect browser */


exit;
}  else if((empty($_POST['password']))){
    header("Location:index.php"); /* Redirect browser */


exit;
} 

?>

this also doesn't work. i took out the else if and did a bunch of if statements together one ontop of another so that if one fails, redirect, if it passes, go down to the next if statement...

this too didn't work.

the problem overall that im having is (i feel dumb!) is that, on all of these, when the condition of the first value is met, it skips the rest of the php below it and goes ahead to load the html. only happens on the first one only. If for example, i enter password only, it throws me back to index. if i enter username first and not the pass..it lets me through EVEN though, there are other if statements with their own rules. i placed the php ontop of everything to make SURE it was happening b4 anything was being sent but to no avail.

ideas? what am i doing wrong with these conditionals?

funny thing is when i test for the value of POST against the value of a result in a db(from another script im working on), like so:

if((!empty($user)) && (!empty($pass))) {
    //connect to db
    $connect2db = mysqli_connect('127.0.0.1', 'root', 'pass', 'db');
        if(!$connect2db){
            die("error connectin to Database" . mysqli_error);
        }

    $dbq = "SELECT * FROM accounts;";
    $dbqDoIt = mysqli_query($connect2db, $dbq)or die("error".mysqli_error($connect2db));

    $getNames = mysqli_fetch_array($dbqDoIt);
        //check if user acc. exists inside vS database.
        if(($name === $getNames['username']) && ($pass === $getNames['password'])){
//do something

}

when i check like this with not empty, it stays put UNTIL both conditions are met...when i check for empty (ive tried with !isset too) in the example above, it fails me miserably....

what am i doing wrong? please help.

thank you kindly in advance.

Was it helpful?

Solution

You need an OR here.

if(!$_POST['user'] || !$_POST['password']){
    header("Location: index.php"); /* Redirect browser */
    exit;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top