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>&nbsp;</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>&nbsp;</p>
<?php
mysql_free_result($Recordset1);
?>

Recommended Answers

All 4 Replies

at your line number 13, add one line as shown below

$_POST['Belopp']=$avrundat;
Member Avatar for rajarajan2017
$passedValue=$_POST['Belopp'];

Then pass the $passedValue to your insert query to update in the databse.

at your line number 13, add one line as shown below

$_POST['Belopp']=$avrundat;

Thanks a lot! that solved my problem in a second:icon_smile:
Not sure why though, guess to read the calculated value befor insert to database.

This is how it ended up, both variables $faktura_forfaller (invoice pay day) and $avrundat (to pay) are calculated values.
I now understand that there are no need for hidden fields or other work arounds. The variables can be inserted into the database directly with:

GetSQLValueString($avrundat, "text"),
GetSQLValueString($faktura_forfaller, "date"),
<?PHP
$faktura_datum = $_POST["faktura_datum"];
$villkor = $_POST["faktura_villkor"];

$faktura_forfaller = date ('Y-m-d', strtotime ("$faktura_datum + $villkor days"));


$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 (faktura_datum, faktura_villkor, belopp, faktura_forfaller, spec1_belopp, spec2_belopp, spec3_belopp, spec4_belopp) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['faktura_datum'], "date"),
                       GetSQLValueString($_POST['faktura_villkor'], "text"),
		     GetSQLValueString($avrundat, "text"),
                       GetSQLValueString($faktura_forfaller, "date"),
                       GetSQLValueString($_POST['Belopp_1'], "text"),
                       GetSQLValueString($_POST['Belopp_2'], "text"),
                       GetSQLValueString($_POST['Belopp_3'], "text"),
                       GetSQLValueString($_POST['Belopp_4'], "text"));
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.