this program inserts data (correctly) into fields b4 update but I get the error message when I submit. Can someone tell me what is wrong with my code?

<html><head>
<!--when the paidamt is keyed in, the current date, pd & paidsum are autoinserted-->
<script>
function $_(IDS) { return document.getElementById(IDS); }
function calculate_paid() {
   var pd = document.getElementById("pd");
   var datepaid = document.getElementById("datepaid");
   var amtdue = document.getElementById("amtdue");
   var paidamt = document.getElementById("paidamt");
   var paidsum = document.getElementById("paidsum");
// ********************set up date**********************
  var dateNow = new Date();
  var dayNow = dateNow.getDate();
  var datePaid = (dateNow.getMonth()+1)+"/"+dateNow.getDate()+"/"+dateNow.getFullYear();
datepaid.value = datePaid;
// ***************end date setup*************************
// ********accumulate charges into paidsum & set up pd*** 
amtdue.value = parseFloat(amtdue.value);
paidsum.value = parseFloat(paidsum.value) + parseFloat(paidamt.value);
pd.value = "P";
// ********************* end of calcs ******************
}
</script>
<script type="text/javascript">
window.google_analytics_uacct = "UA-256751-2";
</script>
<script type="text/javascript">
window.google_analytics_uacct = "UA-256751-2";
</script>
</head><body>
<?php
mysql_connect(localhost,root,"");
mysql_select_db(oodb) or die( "Unable to select database");
if(!empty($_POST["submit"]))
{
$invnum = $_POST['invnum'];
$query="SELECT * FROM oocust Where invnum='$invnum'";
$result=mysql_query($query);
if(mysql_num_rows($result))
{
echo "<form action='#' method='post'><b>Invoice Payment :<br /><br />
<table cellspacing=0 cellpadding=0 border=1>
    <tr>
  <th>Inv#</th>
  <th>ord#</th>
  <th>Name</th>
  <th>Description</th>
  <th>Paid Amt</th>
  <th>Amt Due</th>
  <th>Date Paid</th>
  <th>Code</th>
  <th>Total</th>
      </tr>";
while($row = mysql_fetch_assoc($result))
{
 echo "<tr>
<td><input type='text' size=5 name='invnum' value='" . $row['invnum'] . "'></td>
<td><input type='text' size=5 name='ordernum' value='" . $row['ordernum'] . "'></td>
<td><input type='text' size=25 name='bname' value='" . $row['bname'] . "'></td>
<td><input type='text' size=25 name='descr' value='" . $row['descr'] . "'></td>
<td><input type='text' size=7 id='paidamt' name='paidamt' value='" . $row['paidamt'] ."' 
onBlur='calculate_paid(this)'></td>
<td><input type='text' size=7 id='amtdue' name='charges' value='" . $row['amtdue'] ."'></td>
<td><input type='text' size=10 id='datepaid' name='datepaid' value='" . $row['datepaid'] . "'
onBlur='calculate_paid(this)'></td>
<td><input type='text' size=1 id='pd' name='pd' value='" . $row['pd'] . "'
onBlur='calculate_paid(this)'></td>
<td><input type='text' size=7 name='paidsum' value='" . $row['paidsum'] . "'
onBlur='calculate_paid(this)'></td>   
</tr>"; 
}
echo "</table>
<input type='submit' name='update' value='Make Payment' />
</form>";
}
else{echo "No listing for invoice no. $invnum.<br />Please select another.<br />";}
}
if(!empty($_POST["update"]))
{
$sql = "UPDATE payments SET
pd = '" . mysql_real_escape_string($_POST['pd']) . "',
datepaid = '" . mysql_real_escape_string($_POST['datepaid']) . "',
paidamt = '" . mysql_real_escape_string($_POST['paidamt']) . "',
amtdue = '" . mysql_real_escape_string($_POST['amtdue']) . "',
paidsum = '" . mysql_real_escape_string($_POST['paidsum']) . "'
WHERE invnum='".$_POST["invnum"]."'";
// **********************************************
mysql_query($sql) or die("Update query failed.");
// ***********************************************
echo "Record for invoice ".$_POST["invnum"]." has been updated";
}
?>
<form method="post" action="#">
<br />
<input type="text" name="invnum"/> <p>
<input type="submit" name="submit" value="select invoice no."/>
</form>
<script type="text/javascript"><!--
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
//-->
</script>
<script type="text/javascript"><!--
try {
var pageTracker = _gat._getTracker("UA-256751-2");
pageTracker._trackPageview();
} catch(err) {}
//-->
</script>
<script type="text/javascript"><!--
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
//-->
</script>
<script type="text/javascript"><!--
try {
var pageTracker = _gat._getTracker("UA-256751-2");
pageTracker._trackPageview();
} catch(err) {}
//-->
</script>
</body></html>

Recommended Answers

All 10 Replies

Member Avatar for diafol

Jeez, you certainly don't believe in separating your php/html/js do you??!

1. Try to post to a different page (avoids the reload/refresh double submit)
2. Which form is giving you the problem?

Sorry, this is giving me a headache.

sorry I had trouble finding how to separate the two. Yeah I'm sorry 'bout your headache

Member Avatar for diafol

:} No to worry. Which form is causing the issue?

:} No to worry. Which form is causing the issue?

the below code:

<?php
mysql_connect(localhost,root,"");
mysql_select_db(oodb) or die( "Unable to select database");
if(!empty($_POST["submit"]))
{
$invnum = $_POST['invnum'];
$query="SELECT * FROM oocust Where invnum='$invnum'";
$result=mysql_query($query);
if(mysql_num_rows($result))
{
echo "<form action='#' method='post'><b>Invoice Payment :<br /><br />
<table cellspacing=0 cellpadding=0 border=1>
    <tr>
  <th>Inv#</th>
  <th>ord#</th>
  <th>Name</th>
  <th>Description</th>
  <th>Paid Amt</th>
  <th>Amt Due</th>
  <th>Date Paid</th>
  <th>Code</th>
  <th>Total</th>
      </tr>";
while($row = mysql_fetch_assoc($result))
{
 echo "<tr>
<td><input type='text' size=5 name='invnum' value='" . $row['invnum'] . "'></td>
<td><input type='text' size=5 name='ordernum' value='" . $row['ordernum'] . "'></td>
<td><input type='text' size=25 name='bname' value='" . $row['bname'] . "'></td>
<td><input type='text' size=25 name='descr' value='" . $row['descr'] . "'></td>
<td><input type='text' size=7 id='paidamt' name='paidamt' value='" . $row['paidamt'] ."' 
onBlur='calculate_paid(this)'></td>
<td><input type='text' size=7 id='amtdue' name='charges' value='" . $row['amtdue'] ."'></td>
<td><input type='text' size=10 id='datepaid' name='datepaid' value='" . $row['datepaid'] . "'
onBlur='calculate_paid(this)'></td>
<td><input type='text' size=1 id='pd' name='pd' value='" . $row['pd'] . "'
onBlur='calculate_paid(this)'></td>
<td><input type='text' size=7 name='paidsum' value='" . $row['paidsum'] . "'
onBlur='calculate_paid(this)'></td>   
</tr>"; 
}
echo "</table>
<input type='submit' name='update' value='Make Payment' />
</form>";
}
else{echo "No listing for invoice no. $invnum.<br />Please select another.<br />";}
}
if(!empty($_POST["update"]))
{
$sql = "UPDATE payments SET
pd = '" . mysql_real_escape_string($_POST['pd']) . "',
datepaid = '" . mysql_real_escape_string($_POST['datepaid']) . "',
paidamt = '" . mysql_real_escape_string($_POST['paidamt']) . "',
amtdue = '" . mysql_real_escape_string($_POST['amtdue']) . "',
paidsum = '" . mysql_real_escape_string($_POST['paidsum']) . "'
WHERE invnum='".$_POST["invnum"]."'";
// **********************************************
mysql_query($sql) or die("Update query failed.");
// ***********************************************
echo "Record for invoice ".$_POST["invnum"]." has been updated";
}
?>
Member Avatar for diafol

OK, I tidied up the code somewhat. Some people like it like this. It's a matter of personal preference.

...
?>

	<form action="#" method="post"><strong>Invoice Payment :</strong><br /><br />
	<table cellspacing="0" cellpadding="0" border="1">
		<tr>
		  <th>Inv#</th>
		  <th>ord#</th>
		  <th>Name</th>
		  <th>Description</th>
		  <th>Paid Amt</th>
		  <th>Amt Due</th>
		  <th>Date Paid</th>
		  <th>Code</th>
		  <th>Total</th>
		  </tr>
	<?php
	while($row = mysql_fetch_assoc($result)){
	extract($row);
	?>
		<tr>
			<td><input type="text" size="5" name="invnum" value="<?php echo $invnum;?>" /></td>
			<td><input type="text" size="5" name="ordernum" value="<?php echo $ordernum;?>" /></td>
			<td><input type="text" size="25" name="bname" value="<?php echo $bname;?>" /></td>
			<td><input type="text" size="25" name="descr" value="<?php echo $descr;?>" /></td>
			<td><input type="text" size="7" id="paidamt" name="paidamt" value="<?php echo $paidamt;?>" onBlur="calculate_paid(this);" /></td>
			<td><input type="text" size="7" id="amtdue" name="charges" value="<?php echo $amtdue;?>" /></td>
			<td><input type="text" size="10" id="datepaid" name="datepaid" value="<?php echo $datepaid;?>" onBlur="calculate_paid(this);" /></td>
			<td><input type="text" size="1" id="pd" name="pd" value="<?php echo $pd;?>" onBlur="calculate_paid(this);" /></td>
			<td><input type="text" size="7" name="paidsum" value="<?php echo $paidsum;?>" onBlur="calculate_paid(this);" /></td>   
		</tr> 
	<?php
	}
	?>
	</table>
	<input type="submit" name="update" value="Make Payment" />
	</form>
	<?php
	}else{
		echo "No listing for invoice no. $invnum.<br />Please select another.<br />";
	}
}
if(isset($_POST["update"])){
	$sql = "UPDATE payments SET
	pd = '" . mysql_real_escape_string($_POST['pd']) . "',
	datepaid = '" . mysql_real_escape_string($_POST['datepaid']) . "',
	paidamt = '" . mysql_real_escape_string($_POST['paidamt']) . "',
	amtdue = '" . mysql_real_escape_string($_POST['amtdue']) . "',
	paidsum = '" . mysql_real_escape_string($_POST['paidsum']) . "'
	WHERE invnum='".$_POST["invnum"]."'";
	// **********************************************
	mysql_query($sql) or die("Update query failed.");
	
	
	...
?>

your code got this message:

UPDATE payments SET pd = 'P', datepaid = '4/19/2011', paidamt = '169.60', amtdue = '', paidsum = '169.6' WHERE invnum='150'

paidsum dropped a zero and no update. Closer!

Member Avatar for diafol
<input type="text" size="7" id="amtdue" name="charges" value="<?php echo $amtdue;?>" />

This is because the id ia amtdue but the name is charges. Change charges to amtdue.

<input type="text" size="7" id="amtdue" name="charges" value="<?php echo $amtdue;?>" />

This is because the id ia amtdue but the name is charges. Change charges to amtdue.

I made the change and the results were the same. I put an id in the paidsum but the results were the same.? This poor noob is perplexed.

Member Avatar for diafol

Well it seems to be you update query. echo it out:

echo $sql;

and post it here

I'm not sure I understand what you're suggesting

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.