Software > Computer programming

need help with echo

(1/1)

guitarzRus:
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 "
";
?>

<!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>
<font size="+1">Rent Receipt</font>

Date: <script type="text/javascript">document.write(date);</script><p>
<img src="apt-pic.jpg" alt="apartment" height=250 width=800>


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>


<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">
 
 
tenant paying is: <?php echo $_POST["tenant"]; ?> -
Amount paid is: <?php echo $_POST["amtpaid"]; ?> -
echo "balance due is: $baldue";



<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=''">

</center></body></html>

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

DaveLembke:
echo "receiptno is $receiptno";echo "
";

looks like your not going to get the value of $receiptno to display because its within " " so your probably seeing $receiptno vs the actual value.

This below might fix to show the variable value as its now not considered part of string within " "

echo "receiptno is " $receiptno;
echo "
";

If this works for you you will want to do same to other areas where you want the value of the variable to display to user. Unable to test it at my end and just debugging what you got by reading it and that caught my attention as incorrect.

Same issue goes for these instructions to have the variable to display in the echo outside of the " ":

echo "balance due is: $baldue";

and

tenant paying is: - Amount paid is: - echo "balance due is: $baldue";

BC_Programmer:
Dave: No- PHP will interpret variable names when the string is enclosed by double quotes.

It doesn't work for baldue because that's not in the PHP code:


--- Code: ---Amount paid is: <?php echo $_POST["amtpaid"]; ?> -
echo "balance due is: $baldue";

--- End code ---

It needs to be done similar to the Amount paid part.


Though it's unclear where the $_POST variables are expected to come from. From the example the amtpaid post variable is not set so whatever form is using this PHP for it's submission is not passing it in.



--- Code: ---echo "receiptno is $receiptno";echo "
";
?>

--- End code ---



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

--- End quote ---

This line looks like it is part of the assignment to $sql for the query text, but the rest of the line is missing? And at this point, $receiptno is going to be unassigned as far as I can tell, so is receiptno supposed to be the field in the table? Where is it coming from? further down it gets assigned the string 'receiptno'? I don't understand the purpose that.

If the intent is to update the existing record's receipt number and add one and get the result, you can use a RETURNING clause on the sql.


--- Code: ---$sql = "UPDATE numberstbl SET receiptno=receiptno+1 where id=$id RETURNING receiptno"

--- End code ---

you would them use the fetch_object of the returned value from mysqli to get the result, something like this:


--- Code: ---$result = mysqli_query($connection, $sql);
if ($result) {
    $receiptno = $result->fetch_object()->receiptno;
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . mysqli_error($connection);
}

--- End code ---

Navigation

[0] Message Index

Go to full version