So as the title entails, my if else statemnt in php won't execute. i mean i would undrstand if the 'if' didn't execute but it didn't even read the else statement. so when i submit nothing happens.

<?php
session_start();

if(isset($_POST['jansave']))
{
    $basic = $_POST['basic'];
    $allowance = $_POST['allowance'];
    $bonus = $_POST['bonus'];
    $epf = $_POST['epf'];
    $asb = $_POST['asb'];
    $royalty = $_POST['royalty'];
    $sub = $_POST['sub'];
    $div = $_POST['div'];
    $others = $_POST['others'];

    if($basic&&$allowance&&$bonus&&$epf&&$asb&&$royalty&&$sub&&$div&&$others)
    {
        require "connect.php";

        $total=($basic + $allowance + $bonus + $epf + $asb + $royalty + $sub + $div + $others);
        $query=("INSERT INTO january VALUES ('$basic', '$allowance', '$bonus', '$epf', '$asb', '$royalty', '$sub', '$div', '$others', '$total')");
        $result=mysql_query($query);
        if($result)
        {
            echo "<b>Your Total Income is $total</b>";
        }
    }
    else
    {
        echo ('<script type="text/javascript">alert("Sorry!");</script>');
    }

}

?>

html code:

<form method="post" action="income.php">
        <div style="height:0px;"><input type="submit" value="Save" class="save" name="jansave"></div>
        <table border="1" rules="groups" cellpadding="10px;" class="tableincome">
        <thead>
            <th>Monthly Salary</th>
            <th></th>
            <th>RM</th>
        </thead>
        <tr>
            <td>Basic Salary</td>
            <td></td>
            <td><center><input type="text" name="basic" size="11" placeholder="0" value="<?php if(isset($_POST['basic'])){echo htmlentities($_POST['basic']);} ?>"></center></td>
        </tr>
        <tr>
            <td>Fixed Allowance</td>
            <td></td>
            <td><center><input type="text" name="allowance" size="11" placeholder="0" value="<?php if(isset($_POST['allowance'])){echo htmlentities($_POST['allowance']);} ?>"></center></td>
        </tr>
        <tr>
            <td>Bonus</td>
            <td></td>
            <td><center><input type="text" name="bonus" size="11" placeholder="0" value="<?php if(isset($_POST['bonus'])){echo htmlentities($_POST['bonus']);} ?>"></center></td>
        </tr>
        <thead>
            <th>OTHER SOURCE OF INCOME</th>
            <th></th>
            <th>RM</th>
        </thead>
        <tr>
            <td>EPF</td>
            <td></td>
            <td><center><input type="text" name="epf" size="11" placeholder="0" value="<?php if(isset($_POST['epf'])){echo htmlentities($_POST['epf']);} ?>"></center></td>
        </tr>
        <tr>
            <td>ASB</td>
            <td></td>
            <td><center><input type="text" name="asb" size="11" placeholder="0" value="<?php if(isset($_POST['asb'])){echo htmlentities($_POST['asb']);} ?>"></center></td>
        </tr>
        <tr>
            <td>Dividen/Interest(AHB/TABUNG HAJI/etc)</td>
            <td></td>
            <td><center><input type="text" name="div" size="11" placeholder="0" value="<?php if(isset($_POST['div'])){echo htmlentities($_POST['div']);} ?>"></center></td>
        </tr>
        <tr>
            <td>Royalty</td>
            <td></td>
            <td><center><input type="text" name="royalty" size="11" placeholder="0" value="<?php if(isset($_POST['royalty'])){echo htmlentities($_POST['royalty']);} ?>"></center></td>
        </tr>
        <tr>
            <td>Subletting/Rent</td>
            <td></td>
            <td><center><input type="text" name="sub" size="11" placeholder="0" value="<?php if(isset($_POST['sub'])){echo htmlentities($_POST['sub']);} ?>"></center></td>
        </tr>
        <tr>
            <td>Others(Unit Trust/Emas/IPO/etc)</td>
            <td></td>
            <td><center><input type="text" name="others" size="11" placeholder="0" value="<?php if(isset($_POST['others'])){echo htmlentities($_POST['others']);} ?>"></center></td>
        </tr>
        </table>
        </form>

could someone please point out my utterly stupid mistake that i did? coz the php code was fine yesterday then i had to redo some coding and today it aint working.

Recommended Answers

All 13 Replies

are you sureif(isset($_POST['jansave'])) is true?
if not the script exit whitout output
is 'jansave' the name of your submit-button?

start with var_dump($POST) to check

Member Avatar for diafol

Are you talking about this if...

if($basic&&$allowance&&$bonus&&$epf&&$asb&&$royalty&&$sub&&$div&&$others)

? I believe that's the one with the else.

Anyway, do you really want such an annoying thing as a js alert? Can't you just post a message to the screen?

The problem seems be related to your DB connection and/or query. When I test out your code removing the DB related code, the If..else block executes as expected. If I fill in all of the values in your form, the if produces true. If i leave out any of the input elements, it returns false.

pzuurveen: yea 'jansave' is the name. i did $var_dump and its okay, i mean it prints wtv value i put in the text field.

diafol: yeap thats the if. yeah it kinda is annoying huh?? will c what i can do

any ideas?

alright. will check it out now. thanks JorgeM

Member Avatar for diafol

I'm with Jorge here. The only thing I can think of is that the query is being run, but it's not returning a valid resource ($result).

Try this...

    if($result)
    {
        echo "<b>Your Total Income is $total</b>";
    }else{
        echo "bad mysql stuff";
    }

i really have no clue here guys. i've run the system from registration page and all the db related stuff is good until this particular part. and the db related coding are practically the same. :( help this is my final year project

Again, the problem appears to be related to the DB portion of your code. Here is your code with the DB components removed. It works as expected...

<?php
session_start();
if(isset($_POST['jansave']))
{
    $basic = $_POST['basic'];
    $allowance = $_POST['allowance'];
    $bonus = $_POST['bonus'];
    $epf = $_POST['epf'];
    $asb = $_POST['asb'];
    $royalty = $_POST['royalty'];
    $sub = $_POST['sub'];
    $div = $_POST['div'];
    $others = $_POST['others'];
    if($basic&&$allowance&&$bonus&&$epf&&$asb&&$royalty&&$sub&&$div&&$others)
    {
        $total=($basic + $allowance + $bonus + $epf + $asb + $royalty + $sub + $div + $others);

            echo ("<b>Your Total Income is $total</b>");    
    }
    else
    {
        echo ("<b>Please fill out the entire form.</b>");
    }
}
?>
<!doctype html>
<html>
<head>
        <title>Demo</title>
        </head>
        <body>
        <form method="post" action="income.php">
        <div style="height:0px;"><input type="submit" value="Save" class="save" name="jansave"></div>
        <table border="1" rules="groups" cellpadding="10px;" class="tableincome">
        <thead>
            <th>Monthly Salary</th>
            <th></th>
            <th>RM</th>
        </thead>
        <tr>
            <td>Basic Salary</td>
            <td></td>
            <td><center><input type="text" name="basic" size="11" placeholder="0" value="<?php if(isset($_POST['basic'])){echo htmlentities($_POST['basic']);} ?>"></center></td>
        </tr>
        <tr>
            <td>Fixed Allowance</td>
            <td></td>
            <td><center><input type="text" name="allowance" size="11" placeholder="0" value="<?php if(isset($_POST['allowance'])){echo htmlentities($_POST['allowance']);} ?>"></center></td>
        </tr>
        <tr>
            <td>Bonus</td>
            <td></td>
            <td><center><input type="text" name="bonus" size="11" placeholder="0" value="<?php if(isset($_POST['bonus'])){echo htmlentities($_POST['bonus']);} ?>"></center></td>
        </tr>
        <thead>
            <th>OTHER SOURCE OF INCOME</th>
            <th></th>
            <th>RM</th>
        </thead>
        <tr>
            <td>EPF</td>
            <td></td>
            <td><center><input type="text" name="epf" size="11" placeholder="0" value="<?php if(isset($_POST['epf'])){echo htmlentities($_POST['epf']);} ?>"></center></td>
        </tr>
        <tr>
            <td>ASB</td>
            <td></td>
            <td><center><input type="text" name="asb" size="11" placeholder="0" value="<?php if(isset($_POST['asb'])){echo htmlentities($_POST['asb']);} ?>"></center></td>
        </tr>
        <tr>
            <td>Dividen/Interest(AHB/TABUNG HAJI/etc)</td>
            <td></td>
            <td><center><input type="text" name="div" size="11" placeholder="0" value="<?php if(isset($_POST['div'])){echo htmlentities($_POST['div']);} ?>"></center></td>
        </tr>
        <tr>
            <td>Royalty</td>
            <td></td>
            <td><center><input type="text" name="royalty" size="11" placeholder="0" value="<?php if(isset($_POST['royalty'])){echo htmlentities($_POST['royalty']);} ?>"></center></td>
        </tr>
        <tr>
            <td>Subletting/Rent</td>
            <td></td>
            <td><center><input type="text" name="sub" size="11" placeholder="0" value="<?php if(isset($_POST['sub'])){echo htmlentities($_POST['sub']);} ?>"></center></td>
        </tr>
        <tr>
            <td>Others(Unit Trust/Emas/IPO/etc)</td>
            <td></td>
            <td><center><input type="text" name="others" size="11" placeholder="0" value="<?php if(isset($_POST['others'])){echo htmlentities($_POST['others']);} ?>"></center></td>
        </tr>
        </table>
        </form>
        </body>
</html>

Here is a working demo of this sample code (minus the DB component)... >> http://itg.somee.com/dw/dw-467335/

When I test out your rule eliminating the DB related rule, the If..else block carries out as expected.

haih.. yeah its a db problem. and i really don't know how to fix it. but thanks for the input guys.

so since now its a db prob ill just mark this discussion as solved.

just to let anyone know i solved the problem and it was actually the naming of the variable. i mean i had to change

$div = $_POST['div'];

to

$dividend = $_POST['dividend'];

and it worked. im not really sure y. maybe it conflicted with the <div>'s i already had.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.