I have a function to calculate some mathmatic, but it seems like it wont put into database...
Is there something i dont know or?
function overskud($odds, $indskud) {
$overskud1 = $indskud * $odds;
$overskud2 = $overskud1 - $indskud;
echo "$overskud2";
}
$overskud = overskud($odds, $indskud);
mysql_query("INSERT INTO ODDS_kupon (kamp,odds,indskud,overskud,resultat,af,dato_ar,dato_moned,dato_dag) VALUES ('$kamp', '$odds', '$indskud', '$overskud2', '0', '$profilid', '$datoar', '$datomoned', '$datodag')");
Your query uses the variable $overskud2
, but you did not define it (it only exists inside the function). I suppose you meant $overskud
.
And the function should return
its result, not echo
it (confused with shell scripting?).
You need to return
the result rather than echo
it:
function overskud($odds, $indskud) {
$overskud1 = $indskud * $odds;
$overskud2 = $overskud1 - $indskud;
return "$overskud2";
}
$overskud = overskud($odds, $indskud);
mysql_query("INSERT INTO ODDS_kupon (kamp,odds,indskud,overskud,resultat,af,dato_ar,dato_moned,dato_dag) VALUES ('$kamp', '$odds', '$indskud', '$overskud2', '0', '$profilid', '$datoar', '$datomoned', '$datodag')");
You may need to alter this to return
an array of values that includes $overskud2
in addition to $overskud
, since your INSERT
query uses that variable (but unless it is defined elsewhere in code not posted, it is not in scope due to being inside the function).
echo
simply outputs data to the page/buffer, return
actually passes the value back to whatever invoked it and allows for variable assignment via function output.
betr try this.. you should be returning something to the function call
function overskud($odds, $indskud) {
$overskud1 = $indskud * $odds;
$overskud2 = $overskud1 - $indskud;
return $overskud2;
}
$overskud = overskud($odds, $indskud);
mysql_query("INSERT INTO ODDS_kupon(kamp,odds,indskud,overskud,resultat,af,dato_ar,dato_moned,dato_dag) VALUES('$kamp','$odds', '$indskud', '$overskud', '0', '$profilid', '$datoar', '$datomoned', '$datodag')");