so i wanna echo a result from my database and i have successfully done that but im trying to place the result at a specific place on my page.
this is the code

if($result)
        {
                {
                    echo "<div class='total'>";
                    echo "<b>Your Total Income is $total</b>";
                    echo "</div>";
                }

        }

and it is appearing at the top left corner of the page but i want it to be at the centre next to a table. ive tried placing the code in my html code where i want it to be but 'undefined variable : result' error came out instead. so could anyone help me out? thanks

Show us the rest of the code. From this snippet it is impossible to tell what is wrong. Could be html tags mismatch.

 if ($result) 
    { 
     echo "<div class='total'>";
     echo "<b>Your Total Income is $total</b>";
     echo "</div>"; 
    } 
Member Avatar for diafol

Show us the rest of the code.

Show us the rest of the code. It seems $result doesn't exist. So show us the code. Don't be shy :)

this is the html code

        <form method="post" action="income.php">
        <div style="height:5px;">    
            <input type="submit" value="Save" class="save" name="jansave">
            <input type="submit" value="Select from previous Month" class="prev" name="janprev">
        </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>
        <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>
        </table>
        </form>
      </div>
    </div>

php code:

if(isset($_POST['jansave']))
{
    $basic = $_POST['basic'];
    $epf = $_POST['epf'];


    if($basic&&$epf)
    {
        require "connect.php";
        $total=($basic + $epf);
        $query=("INSERT INTO january (basic,epf) VALUES ('$basic','$epf')");
        $result=mysql_query($query);
        if($result)
        {
            echo ("<b>Your Total Income is $dtotal</b>");
        }
        else
        {
            echo "db sucks";
        }

    }
    else
    {
        echo ("<b>Please fill out the entire form.</b>");
    }
}

what i wanna do is display the 'if($result){ echo ("<b>Your Total Income is $dtotal</b>"); }' somewhere in the html code. Ive already tried and placed it where i want it to be displayed.

and this is the code i tried using in the html.

<div class="rm">
    <?php if($result)?>
    <h3>Your Total Income :</br> <?php echo "RM $total" ; ?></h3>
</div>

'your total income' did appear but then $result and $total gave error 'undefined variable'. any pointers?

echo ("<b>Your Total Income is $dtotal</b>"); <!--here you have a typo-->

$dtotal is undefined, $total is defined. If you fix that one typo it might fix the problem.

thanks for pointing that out but that isnt the problem though. :s

The first PHP snippet should be on top of the script and should not echo the variable but just assign a value to it. You will echo it later in the appropriate place in the html.

// initialize the error message variable
$error_message = '';

if(isset($_POST['jansave']))
{
    $basic = $_POST['basic'];
    $epf = $_POST['epf'];

    if($basic&&$epf)
    {
        require "connect.php";
        $total=($basic + $epf);
        $query=("INSERT INTO january (basic,epf) VALUES ('$basic','$epf')");
        $result=mysql_query($query);
        if($result)
        {
            $message = "<b>Your Total Income is $dtotal</b>";
        }
        else
        {
            $message = "db sucks";
        }

    }
    else
    {
        $message = "<b>Please fill out the entire form.</b>";
    }
}

Now here comes your html where you can echo the message whatever it is:

<div class="rm">
<h3><?php echo $message; ?></h3>
</div>

I hope this is what you are after (at least this is what I understood from your post). If you still have problems post the complete sript(s) from top to bottom and mark exactly where you want to put the string. However, make sure all the variables you want to echo out do exist otherwise you will get the error.

And, while we are at it, do not insert user supplied values ($_POST[...]) into the database unescaped. You are risking an injection of malicious code into you db server.

ok cool thanks.
this worked for me:

<div class="rm">
<?php if(@$janresult) ?>
<h3>Your Total Income :</br> <?php echo "RM ".@$total?></h3>
</div>
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.