So i have a table and a calculate button. the calculate button is responsible to selecting data from specific tables and then the selected data will be displayed and added together along with the calculation. but something is wrong with the process because it is not calculating correctly.

table & button:

<form method="post" action="expenses.php">
            <center><table class="tform" border="1" rules="groups" cellpadding="6px" width="450px">
                <colgroup></colgroup>
                <thead>
                <tr>
                <th>Category</th>
                <th>RM</th>
                </tr>
                </thead>
                <tr>
                <td><center>House</center></td>
                <td><center><input  type="text" name="janhouse" id="janhouse" size="11" readonly value="<?php if(@$query){echo htmlentities(@$htotal['htotal']);} ?>"></center></td>
                </tr>
                <tr>
                <td><center>Personal</center></td>
                <td><center><input  type="text" name="janpersonal" id="janpersonal" size="11" readonly value="<?php if(@$query2){echo htmlentities(@$ptotal['ptotal']);} ?>"></center></td>
                </tr>
                <tfoot>
                <tr>
                <td><b><center>Total expenses for this month</center></b></td>
                <td><center><input  type="text" name="jantotal" maxlength="10" size="11" readonly value="<?php if(@$query4){echo htmlentities(@$etotal);} ?>"></center></td>
                </tr>
                </tfoot>
            </table></center>

        <center><input type="submit" value="Calculate" name="jancalc" id="calc" ></center>
        </form>

the calculate button php:

if(isset($_POST['jancalc']))
    {
        require "connect.php";
        $query=mysql_query("SELECT htotal FROM house");
        $htotal=mysql_fetch_array($query);

        $query2=mysql_query("SELECT ptotal FROM personal");
        $ptotal=mysql_fetch_array($query2);

        $query3=mysql_query("SELECT ttotal FROM transport");
        $ttotal=mysql_fetch_array($query3);

        if($htotal&&$ptotal&&$ttotal)
        {
            $etotal=($query + $query2 + $query3);
            $query4=mysql_query("INSERT INTO january (ejant) VALUES ('$etotal')");
        }
    }

the data from each table is displayed correctly, as of now is it displaying 9, 8, 6 in its respective text boxes. but the total is wrong although it is displayed. the displayed total is 21 when actually 9+8+6=23.

i dont know whats wrong. the coding im using is pretty simple and sorta the same one i used in other pages. help please.

Recommended Answers

All 8 Replies

try to use
$etotale=($htotal+$ptotal+$ttotal);
in place of
$etotal=($query + $query2 + $query3);

that gave me :

Notice: Array to string conversion in C:\xampp\htdocs\CashFlow\ejan.php on line 72

in the text box:

<br /><b>Warning</b>: htmlentities() expects parameter 1 to be string, array given in <b>C:\xampp\htdocs\CashFlow\ejan.php</b> on line <b>256</b><br />

and in my database in the specific column : Array

i am not sure what is the problem and what exactly you want but if you want total from database then you have to do like that
$ptotal=mysql_fetch_array($query2);
foreach($ptotal as $pp)
echo $pp

echo is only for just to check the value, try same for every value and then add all of them i hope you get result

In addition to AARTI suggestions, you can do this task with one single query:

insert into jannuary (ejant) select (htotal + ptotal + ttotal) as total from house, personal, transport;

Consider also to switch to PDO or MySQLi, since MySQL is deprecated and will be removed by the incoming versions of PHP.

the problem im having is that the calculation is not correct. the data from each table is displayed correctly, as of now is it displaying 9, 8, 6 in its respective text boxes. but the total is wrong although it is displayed. the displayed total is 21 when actually 9+8+6=23.

then why you are adding query output result and i am not seeing any text box that have this values i don't understand what you are done sorry i am helpless now

Member Avatar for diafol

wouldn't it be...

$etotal= $htotal[0]+$ptotal[0]+$ttotal[0];

Yeah I believe diafol is correct. Look at your own script above:

<?php if(@$query2){echo htmlentities(@$ptotal['ptotal']);} ?>

After you run this code:

 $query2=mysql_query("SELECT ptotal FROM personal");
 $ptotal=mysql_fetch_array($query2);

The value in $ptotal is not a number, but an array containing two key/value pairs:
0 => (value of ptotal)
'ptotal' => (value of ptotal)

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.