Keo Strife
Keo Strife

Reputation: 1446

Can't explain why "if else" in PHP doesn't work properly

Here's the form handling

if(isset($_POST['clear'])) {
    mysql_query("DELETE FROM md5dictionary");
}

if(isset($_POST['submit'])) {
    if(strlen($_POST['keywords']) < 1 || ctype_space($_POST['keywords']) == true) {
        $errors['blank'] = "<span class='error'> Please insert a word </span>";
    } else {
        $newword = $_POST['keywords'];
        $db_checkExist = mysql_query('SELECT * FROM md5dictionary WHERE keywords="' . $newword . '"');
        echo mysql_num_rows($db_checkExist) . "<br />";
        if(mysql_num_rows($db_checkExist) > 0) {
            echo "outside else " . mysql_num_rows($db_checkExist);
            $finalResult = "<span class='error'> Failed!!! </span>";
        } else {
            echo "inside else " . mysql_num_rows($db_checkExist);
            $md51 = md5($newword);
            $md52 = md5($md51);
            mysql_query('INSERT INTO md5dictionary(keywords, mdFive, mdFive2) VALUES ("' . $newword . '", "' . $md51 . '", "' . $md52 . '")');
            $finalResult = "<span class='success'> Thank you! Wanna add another one? </span>";
        }
    }
}

Here's the form

<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
    <fieldset> Input Form </fieldset>
    <label for="keywords"> Input new Keywords into dictionary: </label>
     <?php echo $finalResult; ?>
     <?php echo $errors['blank']; ?>
        <input name="keywords" type="text" />
        <input name="submit" type="submit" value="Add!!" />
        <input name="clear" type="submit" value="Clear Database" />
</form>
<h1> Datas: </h1>
<?php
    $result = mysql_query("SELECT * FROM md5dictionary");
    $rows = mysql_num_rows($result);
    for ($i = 0; $i < $rows; ++$i) {
        echo "Data " . $i . "<br />";
        echo "keywords: " . mysql_result($result, $i, "keywords") . "<br />";
        echo "md5: " . mysql_result($result, $i, "mdFive") . "<br />";
        echo "md5_2: " . mysql_result($result, $i, "mdFive2") . "<br />";
    }
?>

Here's the result: http://md5dictionary.hoangminhdat.com

Question: Why it keeps saying "Failed!" Why it has successfully insert information into my database? There should be no spelling mistake

I know it will be time-consuming go through my dumb question but plss, i can't explain it myself!!

Upvotes: 1

Views: 188

Answers (2)

Sebas
Sebas

Reputation: 21542

I'm not sure about what you mean when you say it inserts and it gives "fail". Do you actually mean "INSERT INTO md5dictionary..." is executed, AND you have the " Failed!!! " code showing?

If this is so, I recommend you investiguate and revise your form flows since it is most probable that calling itself, the form tries to run twice, the second time having null values.

This is what I coming up with.

Have a good day, S.

Upvotes: 0

cb0
cb0

Reputation: 8643

I've tested the weblink you provided.

If I insert a word then I get 'inside else' and it looks like it's inserted.

If I quickly enter this word again then I get 'failed'.

So I see no problem, isn't this what you want to achieve ? Otherwise please rethink your question and refine what is not working for you and when.

EDIT: If you want the successful message then you need another:

echo $finalResult;

after you defined $finalResult.

Upvotes: 2

Related Questions