Forum Moderators: coopster

Message Too Old, No Replies

please correct the errors of this php script

please correct the errors of this php script

         

shams

5:08 am on May 14, 2006 (gmt 0)

10+ Year Member



hi,
i have a mysql table of patients, they have types new, replase and reserve, the new will collect his drugs for 8 months the relpase for 12 and the reserve for 18 months, the $total will show as the sum of collected of any $drug by the patients. noe this script should echo the sum of drugs we need for the rest of months for that $type and $drug:

<?php
$drug = $_POST['drug'];
$type = $_POST['type'];
$newtot = ($total * 8) - $total;
$relpasetot = ($total * 12)-$total;
$reservetot = ($total * 18)-$total;

mysql_connect("localhost","root") or die(mysql_error());
mysql_select_db("mydb") or die(mysql_error());
$result = mysql_query("SELECT SUM($drug) as total FROM treatment");
$total = mysql_result($result, 0, 'total');
if($type == 'new') {
echo "You need ".$newtot." " ."tab of" .$drug;
}
else {
if($type == 'relapse') {
echo "You need ".$relpasetot." " ."tab of" .$drug;
} else { echo "You need ".$reservetot." " ."tab of" .$drug;
}
}

?>

the ouput of this script is 0 which not correct, any one can help plese?

Habtom

5:23 am on May 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The value of $total is extracted after you went through the calculations.

//$total = mysql_result($result, 0, 'total');

Try to get the value of the $total first to use it on the formulas you created.

Habtom