Hi all! My first post in Daniweb.
I'm making a invoice script with textfield to input some numbers (Belopp_1 , Belopp_2, Belopp_3 and Belopp_4) among other stuff. These numbers ar inserted ia a MySql database wthout problems.
Tere are also some calculation made in the script to add the numbers together, show total, add tax and take the decimals (if any) away. In the end there is a To pay value (Att betala) calculated that I want to insert into the database.
I'm echo the To pay value in a textfield "Belopp".
<input name="Belopp" type="text" value="<?PHP echo $avrundat; ?>">
And tries to insert it the same way as the other textfields without success.
A very simplified test for this here http://teeview.se/test_matain.php
The calculation is fine and the insert are fine exept for the calculated value. if I print whatever value in the To pay textfield "Belopp" it will be inserted int the database. So it seemes that the isert to database is done before the calculation and I cant figure a way around it.
The table at the bottom in the test page shows what is in the database after submit the form.
<?PHP
$Belopp_1 = $_POST["Belopp_1"];
$Belopp_2 = $_POST["Belopp_2"];
$Belopp_3 = $_POST["Belopp_3"];
$Belopp_4 = $_POST["Belopp_4"];
$summa = ($Belopp_1 + $Belopp_2 + $Belopp_3 + $Belopp_4);
$moms = ($summa * 0.25);
$total = ($summa + $moms);
$avrundat = floor($total);
$utjamning = ($total - $avrundat);
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form")) {
$insertSQL = sprintf("INSERT INTO test (belopp, spec1_belopp, spec2_belopp, spec3_belopp, spec4_belopp) VALUES (%s, %s, %s, %s, %s)",
GetSQLValueString($_POST['Belopp'], "text"),
GetSQLValueString($_POST['Belopp_1'], "text"),
GetSQLValueString($_POST['Belopp_2'], "text"),
GetSQLValueString($_POST['Belopp_3'], "text"),
GetSQLValueString($_POST['Belopp_4'], "text"));
mysql_select_db($database_connect, $connect);
$Result1 = mysql_query($insertSQL, $connect) or die(mysql_error());
}
mysql_select_db($database_connect, $connect);
$query_Recordset1 = "SELECT * FROM test ORDER BY id DESC";
$Recordset1 = mysql_query($query_Recordset1, $connect) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<html>
<head>
<title>Matte</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<strong>Inmatning & Uträkning</strong>
<form name="form" method="POST" action="<?php echo $editFormAction; ?>">
<p>
<label for="Belopp_1"></label>
<label for="Belopp_1">1</label>
<input type="text" name="Belopp_1" id="Belopp_1">
</p>
<p>
<label for="Belopp_2">2</label>
<input type="text" name="Belopp_2" id="Belopp_2">
</p>
<p>
<label for="Belopp_3">3</label>
<input type="text" name="Belopp_3" id="Belopp_3">
</p>
<p>
<label for="Belopp_4">4</label>
<input type="text" name="Belopp_4" id="Belopp_4">
<br />
<br />
<input type="submit" value="submit" name="submit">
</p>
<p>
<label for="Belopp">Att betala</label>
<input name="Belopp" type="text" value="<?PHP echo $avrundat; ?>">
</p>
<input type="hidden" name="MM_insert" value="form">
</form>
<p>
<?
echo "Belopp 1 = " .$Belopp_1."<br />";
echo "Belopp 2 = " .$Belopp_2."<br />";
echo "Belopp 3 = " .$Belopp_3."<br />";
echo "Belopp 4 = " .$Belopp_4."<br />";
echo "Att betala = " .$avrundat."<br />"; ?>
</p>
<p> </p>
<p>Inmatat i databasen</p>
<table width="800" border="1">
<tr>
<td>ID</td>
<td>Att Betala</td>
<td>Inmatning 1</td>
<td>inmatning 2</td>
<td>Inmatning 3</td>
<td>Inmatning 4</td>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_Recordset1['id']; ?></td>
<td><?php echo $row_Recordset1['belopp']; ?></td>
<td><?php echo $row_Recordset1['spec1_belopp']; ?></td>
<td><?php echo $row_Recordset1['spec2_belopp']; ?></td>
<td><?php echo $row_Recordset1['spec3_belopp']; ?></td>
<td><?php echo $row_Recordset1['spec4_belopp']; ?></td>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>
<p> </p>
<?php
mysql_free_result($Recordset1);
?>