Dan
Dan

Reputation: 35

PHP IF ELSE - If seems to be ignored?

i'm a bit of PHP noob, so sorry if this is a daft question, but I just can't figure this out by myself so any help would be greatly appreciated!

I am trying to create a modify page for an events web application. It seems to be working except when I try to validate the final if/else statement. This statement returns the value of $row[0] and checks if its == NULL. If NULL, it should return an echo 'this event does not exist' to the user, if there is a value, it presents the user with a matrix of text boxes that they can change the data in.

Currently it works fine for the else statement when there is data found, but doesnt recognise the original if when there is no data. Coupled with that, the footer at the bottom of the page disappears!

Here is the main body of the code, i have highlighted the problem area. I understand that there is probably a more effective and efficient way of doing it all, but please keep it simple as I'm still learning. Thanks again. Dan

<div class="round">
<div id="main" class="round">

<span class="bold">MODIFY EVENT</span><br /><br />
On this page you can modify or delete the events that are stored on the database.<br />
Be aware that you cannot undo the DELETE function.<br />
<br />
Find Event:<br />
<table>
<form name="form1" id="form1" method="post" action="
<?php echo $_SERVER["PHP_SELF"]; ?>" >
<tr><th>By Date:</td><td><input type="text" name="modifyDate" 
id="modifyDate" value="dd/mm/yy" /></td></tr>
<tr><th>By Name:</td><td><input type="text" name="modifyName" id="modifyName" 
value="" /></td></tr>
<tr><th>Find All:</th><td><input type="checkbox" name="modifyAll" 
id="modifyAll" /><td></tr>
<tr><td></td><td><input type="submit" name="submit" value="Search" /></td></tr>
</form>
</table>

<?PHP


if(!isset($_POST['modify'])){

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

    $moddate = $_POST['modifyDate'];

            If($moddate == "dd/mm/yy"){
        $date = "";
    }
    else{
        $newDate = str_replace("/",".",$moddate);
        $wholeDate = $newDate;
        $dateArray = explode('.', $wholeDate);
        $date = mktime(0,0,0,$dateArray[1],$dateArray[0],$dateArray[2]);
    }

    $name = $_POST['modifyName'];
    $all  = $_POST['modifyAll'];

    $host = "localhost";
    $user = "user";
    $password = "password";
    $db = "database";

    $con = mysql_connect($host,$user,$password) or die('Could not connect to Server');
    $dbc = mysql_select_db($db, $con) or die('Could not connect to Database');

    if($all != 'on'){
        $q = "SELECT * FROM events WHERE date = '$date' || title = '$name' ";
    }
    else{
        $q = "SELECT * FROM events";
    }

    $result = mysql_query($q);
    $row = mysql_fetch_array($result) or die(mysql_error());    

//THIS IS THE PROBLEM HERE!!!!

    if($row[0]==NULL){ 
        echo 'This event does not exist';
    }
    else{
        ?>

        <form name="form1" id="form1" method="post" action="
                    <?phpecho $_SERVER['PHP_SELF']; ?>" >
    <?PHP               

        $result = mysql_query($q) or die(mysql_error());

        while ($row = mysql_fetch_array($result)){

            $initialDate = date('d/m/y', $row['date']);
            $ID = $row['ID'];

            echo '<input type="text" name="inputEmail" id="inputEmail"  value="'.$initialDate.'" />';

            echo '<input type="checkbox" value=$ID name="toModify[]" style = "visibility: hidden;" /><br /><br />';

        }
        echo '<input type="submit" name="modify" value="Modify" />
                            <br /><br />';
    }
}
}
else{
    //modify database and return echo to user
}
?>


</div>
<div id="clear"></div>
</div>


</div>
</div>
<div id="footer" class="round shadow"></div>

</body>
</html>

Upvotes: 1

Views: 506

Answers (3)

exactlee
exactlee

Reputation: 53

You could check the number of result rows to see if there are any events:

$result = mysql_query($q);
$numrows = mysql_num_rows ($result);
if($numrows === 0){
...

Upvotes: 0

linus72982
linus72982

Reputation: 1393

Is the field in the table it's pulling $row[0] from set to NOT NULL? If it is, it will never be a null value. Try instead something like (if empty($row[0]))

Upvotes: 0

rockerest
rockerest

Reputation: 10518

mysql_fetch_array($result) returns false if there are no rows, so it's possible that even if there is nothing in the result, it's still returning a false and your if($row[0] == null) is evaluating to false, also.

In other words, you should be doing a more robust test on the return results from your query to catch fringe cases.

As others have mentioned or implied, here are some things you could / should be doing:

  • test for rows returned, not values in rows
  • test for existence of variable, not content of variable
  • check the database for errors returned

Upvotes: 4

Related Questions