need help with echo/print

1Tsme1941

Member
Hi, I'm trying to print a rental receipt. I enter a tenant name, amtpaid and amtdue into form and
expect to display the tenant name, amtpaid and balance due(if any). I'm also trying to update the
receiptno in the numberstbl and display the new one. I've tried echo, print and can't get the
values to display. please help.

the receipt code:
<?php
$server = "localhost";
$user = "root";
$password = "";
$dbname = "numbersdb";

// Create connection
$connection = mysqli_connect($server, $user, $password, $dbname);
// Check connection
if (!$connection) {
die("Connection failed: " . mysqli_connect_error());
}

SET receiptno = $receiptno + 1 where id=$id";

if (mysqli_query($connection, $sql)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($connection);
}


$tenant=$_POST['tenant'];
$amtpaid=$_POST['amtpaid'];
$amtdue=$_POST['amtdue'];
$id="id";
$receiptno='receiptno';
$baldue = $amtdue - $amtpaid;

echo "receiptno is $receiptno";echo "<br />";
?>

<!DOCTYPE html><html>
<head>

<script type="text/javascript">
var monthNames = [ "January","February","March","April","May","June","July",
"August","September","October","November","December" ];
var today = new Date();
var date = monthNames[today.getMonth()] + " - " + today.getDate() + " - " + today.getFullYear();
</script>

</head>
<body><center><b>
<font size="+1">Rent Receipt</font><br>
Date: <script type="text/javascript">document.write(date);</script><p>
<img src="apt-pic.jpg" alt="apartment" height=250 width=800><br>

For:<SELECT name="options">
<option value="#990033" style="background-color: Violet;">Rent payment</option>
<option value="#003300" style="background-color: Aquamarine;">Background Check</option>
<option value="#6600cc" style="background-color: Pink;">Security Deposit Payment</option>
<option value="#003300" style="background-color: Aquamarine;">Damages Payment</option>
<option value="#990033" style="background-color: Violet;">Late Charges Payment</option>
<option value="#003300" style="background-color: Aquamarine;">Court Costs Payment</option>
<option value="#6600cc" style="background-color: Pink;">NSF Payment</option>
<option value="#990033" style="background-color: Violet;"> </option>
</SELECT><br>

<input type="text" size = 25 STYLE="color: #000000; background-color: #D4AAFF;" name="Name" value="Business Name -">
<input type="text" size = 25 STYLE="color: #000000; background-color: #D4D4FF;" name="Addy1" value="Business address -">
<input type="text" size = 25 STYLE="color: #000000; background-color: #D4AAFF;" name="Addy2" value="City, State, Zip"><br>

<b> tenant paying is: <?php echo $_POST["tenant"]; ?> -
Amount paid is: <?php echo $_POST["amtpaid"]; ?> -
echo "balance due is: $baldue";
</b><br>

<input type="text" size=85 name="sign" value="Management signature" STYLE="color:
#000000; font-weight: bold; background-color: #ffccff;" onFocus="this.value=''">
<input type="text" size=15 name="thanks" readonly value="We Thank You:" STYLE="color:
#000000; font-weight: bold; background-color: #ffccff;" onFocus="this.value=''"><br>
</b></center></body></html>

the result:
"; ?>
Rent Receipt
Date: July - 26 - 2022
tenant paying is: - Amount paid is: - echo "balance due is: $baldue";
 

1Tsme1941

Member
<?php
$link = mysqli_connect("localhost", "root", "", "numbersdb");
// Check connection
if($link === false){ die("ERROR: Could not connect. " . mysqli_connect_error()); }

$id="id";
$unit="unit";
$tenant="tenant";
$amtpaid='amtpaid';
$amtdue='amtdue';
$receiptno='receiptno';
// $due = $amtdue - $amtpaid;

$sql = "UPDATE numberstbl
SET receiptno = $receiptno + 1 where id=$id";
if(mysqli_query($link, $sql)){ echo "record was updated successfully."; }
else { echo "ERROR: Could not able to execute $sql. " . mysqli_error($link); }

echo "<center>";echo "Receiptno is $receiptno";echo "<br />"; ////////////////////////////////

?>

<!DOCTYPE html><html>
<head>
<title>rent receipt</title>

<script type="text/javascript">
var monthNames = [ "January","February","March","April","May","June","July",
"August","September","October","November","December" ];
var today = new Date();
var date = monthNames[today.getMonth()] + " - " + today.getDate() + " - " + today.getFullYear();
</script>
</head>
<body><center>
Date:<script type="text/javascript">document.write(date);</script><p>
<img src="apt-pic.jpg" alt="apartment" height=250 width=800><br>

<h3>Friendly Apartments -
1624 Hogan Rd -
704-456-9876 -
Happy Hollow. NC, 28054</h3>

From:<input type=text size=85><p>

For:<SELECT name="options">
<option value="#990033" style="background-color: Violet;">Rent payment</option>
<option value="#003300" style="background-color: Aquamarine;">Background Check</option>
<option value="#6600cc" style="background-color: Pink;">Security Deposit Payment</option>
<option value="#003300" style="background-color: Aquamarine;">Damages Payment</option>
<option value="#990033" style="background-color: Violet;">Late Charges Payment</option>
<option value="#003300" style="background-color: Aquamarine;">Court Costs Payment</option>
<option value="#6600cc" style="background-color: Pink;">NSF Payment</option>
<option value="#990033" style="background-color: Violet;"> </option>
</SELECT><br>

<b> Tenant paying is: <?php echo $_POST["tenant"]; ?> -
Amtdue is: <?php echo $_POST["amtdue"]; ?> -
Amount paid is: <?php echo $_POST["amtpaid"]; ?>
</b><br>

<input type="text" size=75 name="sign" value="Sign here" STYLE="color:
#000000; font-weight: bold; background-color: #ffccff;" onFocus="this.value=''">
<input type="text" size=25 name="thanks" readonly value="We Thank You:" STYLE="color:
#000000; font-weight: bold; background-color: #ffccff;" onFocus="this.value=''"><br>
</b></center></body></html>


everything works perfectly except this doesn't display:
echo "<center>";echo "Receiptno is $receiptno";echo "<br />";
 

Okedokey

Well-Known Member
You have multiple syntax errors, however the only way I see this producing the output your getting is if the database has a $receiptno variable (at line 15) has an invalid number in the database or a bad database config. Is line 15 correct? Do you use any debugging or development tools?

1659010409695.png
 

Cromewell

Administrator
Staff member
You have multiple syntax errors
My linter didn't show anything with the most recent version, but errors like this:
PHP:
$receiptno='receiptno';
.....
$sql = "UPDATE numberstbl
SET receiptno = $receiptno + 1 where id=$id";
Won't get caught unless the code is run.


Assuming the whole post is a single file you are echoing html, then starting an html document. Which is likely why you cannot see the 'Receiptno is' bit.
PHP:
...
echo "<center>";echo "Receiptno is $receiptno";echo "<br />"; ////////////////////////////////

?>

<!DOCTYPE html><html>
<head>
...
 

Okedokey

Well-Known Member
My linter didn't show anything with the most recent version, but errors like this:
PHP:
$receiptno='receiptno';
.....
$sql = "UPDATE numberstbl
SET receiptno = $receiptno + 1 where id=$id";
Won't get caught unless the code is run.


Assuming the whole post is a single file you are echoing html, then starting an html document. Which is likely why you cannot see the 'Receiptno is' bit.
PHP:
...
echo "<center>";echo "Receiptno is $receiptno";echo "<br />"; ////////////////////////////////

?>

<!DOCTYPE html><html>
<head>
...
Yeah, i ran CodeRunner. Gave me others, but they were not pertinent to the problem. Also, isn't it simply failing on the receiptno iteration from the mysql database?
 

Cromewell

Administrator
Staff member
Also, isn't it simply failing on the receiptno iteration from the mysql database?
The problem is mysql is weird.
Code:
> select 'abc' + 1;
'abc' + 1
1
You would think the query should fail, but it probably works and sets the row to a receiptno of 1.
 

1Tsme1941

Member
friends, none of these answers make any sense to me. Everything works including the update of receiptno except that:
the receipt number is not displayed on the receipt( my one concern)
 

Cromewell

Administrator
Staff member
friends, none of these answers make any sense to me. Everything works including the update of receiptno except that:
the receipt number is not displayed on the receipt( my one concern)
I think the reason is this:
Assuming the whole post is a single file you are echoing html, then starting an html document. Which is likely why you cannot see the 'Receiptno is' bit.
 

1Tsme1941

Member
Thank you Cromewell, this is the code up to "html".. This is the ? (the same code) I've asked You (Cromewell) and several other forums for over a year in so many ways My confusion with your answer is the "?>" after the echo and b4 the "html". Doesn't that indicate that I'm echoing php? .Maybe I'm just misunderstanding your
answers. Would you please try again?

<?php
$link = mysqli_connect("localhost", "root", "", "numbersdb");
// Check connection
if($link === false){ die("ERROR: Could not connect. " . mysqli_connect_error()); }

$id="id";
$unit="unit";
$tenant="tenant";
$amtpaid='amtpaid';
$amtdue='amtdue';
$receiptno='receiptno';
// $due = $amtdue - $amtpaid;

$sql = "UPDATE numberstbl
SET receiptno = $receiptno + 1 where id=$id";
if(mysqli_query($link, $sql)){ echo "record was updated successfully."; }
else { echo "ERROR: Could not able to execute $sql. " . mysqli_error($link); }
echo "<center>";echo "Receiptno is $receiptno";echo "<br />";
?>

<!DOCTYPE html><html>
 

Cromewell

Administrator
Staff member
Maybe it will make more sense if we look at the output your script produces.
HTML:
<center>Receiptno is receiptno<br />
<!DOCTYPE html><html>
<head>
<title>rent receipt</title>

<script type="text/javascript">
var monthNames = [ "January","February","March","April","May","June","July",
"August","September","October","November","December" ];
var today = new Date();
var date = monthNames[today.getMonth()] + " - " + today.getDate() + " - " + today.getFullYear();
</script>
</head>
<body><center>
Date:<script type="text/javascript">document.write(date);</script><p>
<img src="apt-pic.jpg" alt="apartment" height=250 width=800><br>

<h3>Friendly Apartments -
1624 Hogan Rd -
704-456-9876 -
Happy Hollow. NC, 28054</h3>

From:<input type=text size=85><p>

For:<SELECT name="options">
<option value="#990033" style="background-color: Violet;">Rent payment</option>
<option value="#003300" style="background-color: Aquamarine;">Background Check</option>
<option value="#6600cc" style="background-color: Pink;">Security Deposit Payment</option>
<option value="#003300" style="background-color: Aquamarine;">Damages Payment</option>
<option value="#990033" style="background-color: Violet;">Late Charges Payment</option>
<option value="#003300" style="background-color: Aquamarine;">Court Costs Payment</option>
<option value="#6600cc" style="background-color: Pink;">NSF Payment</option>
<option value="#990033" style="background-color: Violet;"> </option>
</SELECT><br>

<b> Tenant paying is: tenant -
Amtdue is: amtdue -
Amount paid is: amtpaid
</b><br>

<input type="text" size=75 name="sign" value="Sign here" STYLE="color:
#000000; font-weight: bold; background-color: #ffccff;" onFocus="this.value=''">
<input type="text" size=25 name="thanks" readonly value="We Thank You:" STYLE="color:
#000000; font-weight: bold; background-color: #ffccff;" onFocus="this.value=''"><br>
</b></center></body></html>
The echo is adding content before the <html> tag. It is likely that the browser is discarding that.
 

1Tsme1941

Member
echo "<center>";echo "Receipt # is $receiptno";echo "<br />";

Actually read my posts and look at what you ask for. Instead of displaying "receipt # is 1362" it reads "receipt # is receiptno1659282114972.pngtno"
 
Last edited:

johnb35

Administrator
Staff member
I got a very stupid question. Why don't you just buy some decent invoicing software, I'm thinking it would make your job a hell of a lot easier. I use Express Invoice by NCH.
 
Last edited:

1Tsme1941

Member
You're right in the description of your ?. "invoiving"? I think an administrator would frown on you discouraging someone from learning to code.
 

johnb35

Administrator
Staff member
Sorry, was on my phone when I replied, that is now fixed. I'm not discouraging you from learning to code. But I still suggest you find an actual coding forum to help you, you would get a much faster reply. We only have a couple people here that know how to do it and they aren't logged in much.
 
Top