define variables as numeric

1Tsme1941

Member
Hi, I've been trying to solve this for far too long. Trying to define variables as numeric. My
database fields are numeric(0.00, 10.00 etc.). I can't say "$prevbal=0.00;" or "$prevbal=10.00;" etc.
as all the tutorials describe. I remember seeing reference to something like "(int)sc;" but relocate it.
My statement
"$due = $prevbal + $latechg + $secdep + $damage + $courtcost + $nsf;" produces "Warning: A non-numeric
value encountered in C: on line 108". I could use help.
My code:
<?php

$prevbal='prevbal';
$latechg="latechg";
$secdep="secdep";
$damage="damage";
$courtcost="courtcost";
$nsf="nsf";
$paidsum="paidsum";
$comments="comments";

$id="id";
$due="due";

// Attempt select query execution
$result = mysqli_query($conn,"SELECT * FROM payfile Where amtpaid =''");
$row= mysqli_fetch_array($result);

$due = $prevbal + $latechg + $secdep + $damage + $courtcost + $nsf; // line 108
$owed = $due + $amtdue; // line 109

/* if no payment or partial payment, add $10 to latechg field and amount not paid to prevbal field */
if ($amtpaid < $owed)
{ $latechg = $latechg + 10.00; $prevbal = $owed - $amtpaid; }
/* if payment = amtdue clear due */
if ($amtpaid == $owed)
{ $prevbal = 0.00; $latechg = 0.00; $secdep = 0.00; $damage = 0.00; $courtcost = 0.00; $nsf = 0.00; }
/* if over-payment subtract over-payment from prevbal field */
if ($amtpaid > $owed )
{ $prevbal = $amtpaid - $owed; $latechg = 0.00; $secdep = 0.00; $damage = 0.00; $courtcost = 0.00;
$nsf = 0.00; }
$amtpaid = $paidsum;
 

1Tsme1941

Member
Cromewell, as long as you have seen me asking the same question?

below is revised code:
<html>
<head>
<title>make payment and print receipt</title>
</head>
<body><center>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "prerentdb";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$receiptno=0;
$sql = "UPDATE numbers SET receiptno=receiptno+1 WHERE id=1";
if ($conn->query($sql) === TRUE) { echo "receiptno updated successfully"; }
else { echo "Error updating record: " . $conn->error; }
echo $receiptno; // **************** shows 0

$unit = $_POST['unit'];
$amtpaid = $_POST['amtpaid'];
$hudpay = $_POST['hudpay'];
$datepaid = $_POST['datepaid'];

$amtdue = 0.00;
$prevbal=0.00;
$latechg=0.00;
$secdep=0.00;
$damage=0.00;
$courtcost=0.00;
$nsf=0.00;
$paidsum=0.00;
$comments="";

$id="id";

// Attempt select query execution
$result = mysqli_query($conn,"SELECT * FROM payfile Where amtpaid =''");
$row= mysqli_fetch_array($result);

$due = $prevbal + $latechg + $secdep + $damage + $courtcost + $nsf;
$owed = $due + $amtdue;

/* if no payment or partial payment, add $10 to latechg field and amount not paid to prevbal field */
if ($amtpaid < $owed)
{ $latechg = $latechg + 10.00; $prevbal = $owed - $amtpaid; }
/* if payment = amtdue clear due */
if ($amtpaid == $owed)
{ $prevbal = 0.00; $latechg = 0.00; $secdep = 0.00; $damage = 0.00; $courtcost = 0.00; $nsf = 0.00; }
/* if over-payment subtract over-payment from prevbal field */
if ($amtpaid > $owed )
{ $prevbal = $amtpaid - $owed; $latechg = 0.00; $secdep = 0.00; $damage = 0.00; $courtcost = 0.00;
$nsf = 0.00; }
$amtpaid = $paidsum;

// Perform a query, check for error
$sql = "UPDATE payfile SET amtpaid=amtpaid, prevbal=prevbal, latechg=latechg, secdep=secdep,
damage=damage, courtcost=courtcost, nsf=nsf, paidsum=paidsum WHERE unit=$unit";
if ($conn->query($sql) === TRUE) { echo "payfile updated successfully"; }
else { echo "Error updating record: " . $conn->error; } // *** error
?>
<br>
<div class="containerBox">
<img class="img-responsive" src="apt-pic.jpg" height=200 width=500>
<div class='text-box'>
<p class='dataNumber'><input type="text" size = 65 STYLE="color: #000000;
background-color: #D4AAFF;" value="Company Name"><br>
</div></div>

<STYLE TYPE="text/css">
.containerBox {
position: relative;
display: inline-block;
}

.text-box {
position: absolute;
height: 30%;
text-align: center;
width: 100%;
margin: auto;
top: 0;
bottom: 0;
right: 0;
left: 0;
font-size: 30px;
}

.img-responsive {
display: block;
max-width: 100%;
height: 120px;
margin: auto;
padding: auto;
}

.dataNumber {
margin-top: auto;
}
</STYLE>
<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 = 50 STYLE="color: #000000; background-color: #D4AAFF;" name="Name" value="Business Name"><br>
<input type="text" size = 50 STYLE="color: #000000; background-color: #D4D4FF;" name="Addy1" value="Business address"><br>
<input type="text" size = 50 STYLE="color: #000000; background-color: #D4AAFF;" name="Addy2" value="City, State, Zip"><br>

<b> tenant paying: <?php echo $_POST["unit"]; ?> -
Amount paid: <?php echo $_POST["amtpaid"]; ?> -
Date paid: <?php echo $_POST["datepaid"]; ?> -
Amount due: <?php echo $prevbal; ?><br> // **** shows 530.00 - should be 0.00
<input type="text" size = 75 STYLE="color: #000000; background-color: #D4AAFF;" name="sign" value="Sign here">
<input type="text" size = 25 STYLE="color: #000000; background-color: #D4AAFF;" name="thanks" value="We Thank You"><br>
</b></center></body></html>

below is printout:
receiptno updated successfully0Error updating record: Unknown column 'apt1' in 'where clause'
tenant paying: apt1 - Amount paid: 530.00 - Date paid: 2021-07-10 - Amount due: 530
 

1Tsme1941

Member
below is printout:
receiptno updated successfully 0 Error updating record: Unknown column 'apt1' in 'where clause'
tenant paying: apt1 - Amount paid: 530.00 - Date paid: 2021-07-10 - Amount due: 530
...you read it, right?

$receiptno=0;
$sql = "UPDATE numbers SET receiptno=receiptno+1 WHERE id=1";
if ($conn->query($sql) === TRUE) { echo "receiptno updated successfully"; }
else { echo "Error updating record: " . $conn->error; }
echo $receiptno; // **************** shows 0
updates but...you read it, right?

$sql = "UPDATE payfile SET amtpaid=amtpaid, prevbal=prevbal, latechg=latechg, secdep=secdep,
damage=damage, courtcost=courtcost, nsf=nsf, paidsum=paidsum WHERE unit=$unit";
if ($conn->query($sql) === TRUE) { echo "payfile updated successfully"; }
else { echo "Error updating record: " . $conn->error; } // *** error
doesn't update...you read it, right?

<b> tenant paying: <?php echo $_POST["unit"]; ?> -
Amount paid: <?php echo $_POST["amtpaid"]; ?> -
Date paid: <?php echo $_POST["datepaid"]; ?> -
Amount due: <?php echo $prevbal; ?><br> // **** shows 530.00 - should be 0.00
you read it, right?
 

Cromewell

Administrator
Staff member
Is this a test? "you read it, right?" is in there 4 times. No, I do not read your code in its entirety. I start at the error assuming I can find what part you are asking about. Then, I work my way back to the spot that is causing it, again assuming that I can find it. Keep in mind I do not run any of your code. I do not have the database you are using, so there is little point.
echo $receiptno; // **************** shows 0
Just before this, you set receiptno to 0. So this makes sense.
else { echo "Error updating record: " . $conn->error; } // *** error
The error is here:
$sql = "UPDATE payfile SET amtpaid=amtpaid, prevbal=prevbal, latechg=latechg, secdep=secdep,
damage=damage, courtcost=courtcost, nsf=nsf, paidsum=paidsum WHERE unit=$unit";
In your sql query, your variable substitution is wrong. The only one that works as intended is $unit. As long as all these fields are numeric, it is fine, but keep in mind this kind of insertion to sql is dangerous, you should be using bind values and allowing the SQL library/database engine to fill them in and escape them properly

$result = mysqli_query($conn,"SELECT * FROM payfile Where amtpaid =''");
$row= mysqli_fetch_array($result);
You never use this variable.
 

1Tsme1941

Member
I'm asking why my code doesn't work as planned.
No my friend, no test. just noting you were posting your answer without reading the first line which
we both know tells us why the update doesn't work. I'm asking.
Line 2 shows that my html form includes:
tenant paying(unit) is "apt1", amount paid is "530.00".
Below I try to describe what is going on. I'd like to know how to code update confirmation to display if the update is not good????
$receiptno=0;
***** update is successful *****
v
$sql = "UPDATE numbers SET receiptno=receiptno+1 WHERE id=1";
if ($conn->query($sql) === TRUE) { echo "receiptno updated successfully"; }
else { echo "Error updating record: " . $conn->error; }
echo $receiptno; ***** I get "0" - without this I get "undefined variable ??? *******

$unit = $_POST['unit'];
$amtpaid = $_POST['amtpaid']; >>>>>>>>>>>>> \
$hudpay = $_POST['hudpay']; \
$datepaid = $_POST['datepaid']; \
\
// Attempt select query execution \
$result = mysqli_query($conn,"SELECT * FROM payfile Where amtpaid =''");***You never use this variable ****
$row= mysqli_fetch_array($result);

$due = $prevbal + $latechg + $secdep + $damage + $courtcost + $nsf;
$owed = $due + $amtdue;

if ($amtpaid == $owed) ***** this is true - = 530.00 ********
***** below, $prevbal = 0.00 - line 2 shows this as 530.00 ??? ******
{ $prevbal = 0.00; $latechg = 0.00; $secdep = 0.00; $damage = 0.00; $courtcost = 0.00; $nsf = 0.00; }
$amtpaid = $paidsum;

// Perform a query, check for error
$sql = "UPDATE payfile SET amtpaid=amtpaid, prevbal=prevbal, latechg=latechg, secdep=secdep,
damage=damage, courtcost=courtcost, nsf=nsf, paidsum=paidsum WHERE unit=$unit";
if ($conn->query($sql) === TRUE) { echo "payfile updated successfully"; }
else { echo "Error updating record: " . $conn->error; } ****line 1 Unknown column 'apt1' in 'where
clause' - variables are wrong??? *****
Amount due: <?php echo $prevbal; ?><br> ****** prevbal == 0.00 *********
 

Cromewell

Administrator
Staff member
****line 1 Unknown column 'apt1' in 'where
clause' - variables are wrong??? *****
The only one that works as intended is $unit. As long as all these fields are numeric, it is fine
Unknown column 'apt1' in where clause. So, lets breakdown the whole query.
"UPDATE payfile SET amtpaid=amtpaid, prevbal=prevbal, latechg=latechg, secdep=secdep,
damage=damage, courtcost=courtcost, nsf=nsf, paidsum=paidsum WHERE unit=$unit"
You are updating a table named payfile, by setting various columns to themselves, my assumption here is you are intending to use the variables you have of the same name, which is not happening.

You would need to start each one with $ for that to happen.

The next thing is in the event any of them are varchar/text of some kind, would need to be quoted, and technically escaped to prevent a quote in the variable value from breaking it. If they are numeric, it would work without quoting.

Finally, your where clause. unit=$unit. This is the only spot in the entire query where you are substituting one of your variables in. However, as it is a text type, it is missing quotes.

Your query looks like this to your database:
UPDATE payfile SET amtpaid=amtpaid, prevbal=prevbal, latechg=latechg, secdep=secdep,
damage=damage, courtcost=courtcost, nsf=nsf, paidsum=paidsum WHERE unit=apt1
Which, if worked, would actually change nothing

If you were to paste this into your database manager query window and run it, it would fail with the same error. It needs to be
UPDATE payfile SET amtpaid=amtpaid, prevbal=prevbal, latechg=latechg, secdep=secdep,
damage=damage, courtcost=courtcost, nsf=nsf, paidsum=paidsum WHERE unit='apt1'

However, as I have mentioned, substitution this way this is unsafe. Say $unit had a value like " ' ", now your query looks like:
UPDATE payfile SET amtpaid=amtpaid, prevbal=prevbal, latechg=latechg, secdep=secdep,
damage=damage, courtcost=courtcost, nsf=nsf, paidsum=paidsum WHERE unit=' ' '
Which will fail, and leaves it open to someone potentially submitting something like " '; drop table payfile;--".

You should be using bound values. Such as this taken straight from the php manual (https://www.php.net/manual/en/mysqli-stmt.bind-param.php)
PHP:
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'world');

$stmt = mysqli_prepare($link, "INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($stmt, 'sssd', $code, $language, $official, $percent);

$code = 'DEU';
$language = 'Bavarian';
$official = "F";
$percent = 11.2;

mysqli_stmt_execute($stmt);
 

1Tsme1941

Member
Thanks for the info. In recoding I left out the single quotes in " WHERE unit='$unit'"; ".
This corrected the update error. I must have misunderstood your suggestions re. the variables
there as my corrections didn't work. A couple of other quetions: re.

$receiptno=0;
$sql = "UPDATE numbers SET receiptno=receiptno+1 WHERE id=1";
if ($conn->query($sql) === TRUE) { echo "receiptno updated successfully"; }
else { echo "Error updating record: " . $conn->error; }
echo $receiptno;
As coded, the echo displays "0" If I remove "$receiptno=0;" I get undefined variable.

Amount due: <?php echo $prevbal; ?> displays "530.00", should be "0.00".
My entire code below shows this 4th from the bottom:
------------------------------------------------------------
code:
<html>
<head>
<title>make payment and print receipt</title>
</head>
<body><center>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "prerentdb";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$receiptno=0;
$sql = "UPDATE numbers SET receiptno=receiptno+1 WHERE id=1";
if ($conn->query($sql) === TRUE) { echo "receiptno updated successfully"; }
else { echo "Error updating record: " . $conn->error; }
echo $receiptno;

$unit = $_POST['unit'];
$amtpaid = $_POST['amtpaid'];
$hudpay = $_POST['hudpay'];
$datepaid = $_POST['datepaid'];

$amtdue = 0.00;
$prevbal=0.00;
$latechg=0.00;
$secdep=0.00;
$damage=0.00;
$courtcost=0.00;
$nsf=0.00;
$paidsum=0.00;
$comments="";

$id="id";

// Attempt select query execution
$result = mysqli_query($conn,"SELECT * FROM payfile Where amtpaid =''");
$row= mysqli_fetch_array($result);

$due = $prevbal + $latechg + $secdep + $damage + $courtcost + $nsf;
$owed = $due + $amtdue;

/* if no payment or partial payment, add $10 to latechg field and amount not paid to prevbal field */
if ($amtpaid < $owed)
{ $latechg = $latechg + 10.00; $prevbal = $owed - $amtpaid; }
/* if payment = amtdue clear due */
elseif ($amtpaid == $owed)
{ $prevbal = 0.00; $latechg = 0.00; $secdep = 0.00; $damage = 0.00; $courtcost = 0.00; $nsf = 0.00; }
/* if over-payment subtract over-payment from prevbal field */
elseif ($amtpaid > $owed )
{ $prevbal = $amtpaid - $owed; $latechg = 0.00; $secdep = 0.00; $damage = 0.00; $courtcost = 0.00;
$nsf = 0.00; }
$amtpaid = $paidsum;

// Perform a query, check for error
$sql = "UPDATE payfile SET amtpaid='$amtpaid', prevbal='$prevbal', latechg='$latechg',secdep='$secdep',
damage='$damage', courtcost='$courtcost',nsf='$nsf', paidsum='$paidsum' WHERE unit='$unit'";
if ($conn->query($sql) === TRUE) { echo "payfile updated successfully"; }
else { echo "Error updating record: " . $conn->error; }
?>
<br>
<div class="containerBox">
<img class="img-responsive" src="apt-pic.jpg" height=200 width=500>
<div class='text-box'>
<p class='dataNumber'><input type="text" size = 65 STYLE="color: #000000;
background-color: #D4AAFF;" value="Company Name"><br>
</div></div>

<STYLE TYPE="text/css">
.containerBox {
position: relative;
display: inline-block;
}

.text-box {
position: absolute;
height: 30%;
text-align: center;
width: 100%;
margin: auto;
top: 0;
bottom: 0;
right: 0;
left: 0;
font-size: 30px;
}

.img-responsive {
display: block;
max-width: 100%;
height: 120px;
margin: auto;
padding: auto;
}

.dataNumber {
margin-top: auto;
}
</STYLE>
<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 = 50 STYLE="color: #000000; background-color: #D4AAFF;" name="Name" value="Business Name"><br>
<input type="text" size = 50 STYLE="color: #000000; background-color: #D4D4FF;" name="Addy1" value="Business address"><br>
<input type="text" size = 50 STYLE="color: #000000; background-color: #D4AAFF;" name="Addy2" value="City, State, Zip"><br>

<b> tenant paying: <?php echo $_POST["unit"]; ?> -
Amount paid: <?php echo $_POST["amtpaid"]; ?> -
Date paid: <?php echo $_POST["datepaid"]; ?> -
Amount due: <?php echo $prevbal; ?><br>
<input type="text" size = 75 STYLE="color: #000000; background-color: #D4AAFF;" name="sign" value="Sign here">
<input type="text" size = 25 STYLE="color: #000000; background-color: #D4AAFF;" name="thanks" value="We Thank You"><br>
</b></center></body></html>
-------------------------------------------------------------------
result displayed:
receiptno updated successfully 0 payfile updated successfully
tenant paying: apt1 - Amount paid: 530.00 - Date paid: 2021-07-10 - Amount due: 530
 

Cromewell

Administrator
Staff member
As coded, the echo displays "0" If I remove "$receiptno=0;" I get undefined variable.
Without that $receiptno = 0;, the first occurrence of $receiptno is the echo. Presumably this should be coming from a user submitted value?


Amount due: <?php echo $prevbal; ?> displays "530.00", should be "0.00".
I assume the 530 is coming from here:
elseif ($amtpaid > $owed )
{ $prevbal = $amtpaid - $owed; $latechg = 0.00; $secdep = 0.00; $damage = 0.00; $courtcost = 0.00;
$nsf = 0.00; }
amtpaid is coming from a $_POST value, and is surely greater than owed, which is 0.
$due = $prevbal + $latechg + $secdep + $damage + $courtcost + $nsf;
$owed = $due + $amtdue;


$sql = "UPDATE payfile SET amtpaid='$amtpaid', prevbal='$prevbal', latechg='$latechg',secdep='$secdep',
damage='$damage', courtcost='$courtcost',nsf='$nsf', paidsum='$paidsum' WHERE unit='$unit'";
This may error out, it depends if these other columns (amtpaid, prevbal, latechg, secdep, damage, courtcost, nsf, paidsum) are text types or numeric. If they are numeric, quotes are not needed.

This would avoid the issue of quoting or not entirely, and make your code safer. It can be done in oo style, which is nearly identical.
PHP:
# each ? is a placeholder for a variable that will be provided later
$sql = "UPDATE payfile SET amtpaid=?, prevbal=?, latechg=?,secdep=?,
damage=?, courtcost=?, nsf=?, paidsum=? WHERE unit=?";
$stmt = myslqi_prepare($conn, $sql);
# the dddddddds bit tells it what kind of value you are providing
# 1 character per variable. i, d, s, b for integer, double, string, blob
mysqli_stmt_bind_param($stmt, 'dddddddds', $amtpaid, $prevbal, $latechg, $secdep, $damage, $courtcost, $nsf, $paidsum, $unit);
if (mysqli_stmt_execute($stmt) === true) {
  echo "payfile updated successfully";
}

# edit: turns out, there is a nicer way, you can supply the values directly
# in the execute, instead of needing the bind_param bit with the dddddddds above
if (mysqli_execute($stmt, [$amtpaid, $prevbal, $latechg, $secdep, $damage, $courtcost, $nsf, $paidsum, $unit] === true) {
  echo "payfile updated";
}
 
Last edited:

1Tsme1941

Member
thanks again for your help.

1Tsme1941 said:
As coded, the echo displays "0" If I remove "$receiptno=0;" I get undefined variable.
Without that $receiptno = 0;, the first occurrence of $receiptno is the echo. Presumably this should
be coming from a user submitted value?

*** no, receiptno is a field in Numbers table that, after
updating, is to be displayed on the receipt. This was my major issue all along. I could believe that
declaring a variable as 0 or 0.00($receiptno = 0;) could mean it was numeric and not change it to that.


1Tsme1941 said:
Amount due: <?php echo $prevbal; ?> displays "530.00", should be "0.00".
I assume the 530 is coming from here:

*** yes

1Tsme1941 said:
$due = $prevbal + $latechg + $secdep + $damage + $courtcost + $nsf;
$owed = $due + $amtdue;

elseif ($amtpaid > $owed )
{ $prevbal = $amtpaid - $owed; $latechg = 0.00; $secdep = 0.00; $damage = 0.00; $courtcost = 0.00;
$nsf = 0.00; }
amtpaid is coming from a $_POST value, and is surely greater than owed, which is 0.

*** in this case, $due = 0 and $amtdue = 530.00 so $owed = 530.00 **

1Tsme1941 said:
$sql = "UPDATE payfile SET amtpaid='$amtpaid', prevbal='$prevbal', latechg='$latechg',secdep='$secdep',
damage='$damage', courtcost='$courtcost',nsf='$nsf', paidsum='$paidsum' WHERE unit='$unit'";

This may error out, it depends if these other columns (amtpaid, prevbal, latechg, secdep, damage,
courtcost, nsf, paidsum) are text types or numeric. If they are numeric, quotes are not needed.

*** as has been posted, those columns in the table ARE numeric ***
 

1Tsme1941

Member
This is my attempt at PDO; made no change.
<html>
<head>
<title>make payment and print receipt</title>
</head>
<body><center>

<?php
$host = '127.0.0.1';
$db = 'prerentdb';
$user = 'root';
$pass = '';
$charset = 'utf8mb4';

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try {
$pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
/* ---------------------------------------------------------------- */
$sql = "UPDATE users SET receiptno = ? WHERE id = ?";
$pdo->prepare($sql)->execute([$receiptno, $id]);
echo $receiptno;
/* ---------------------------------------------------------------- */
$unit = $_POST['unit'];
$amtpaid = $_POST['amtpaid'];
$hudpay = $_POST['hudpay'];
$datepaid = $_POST['datepaid'];

$amtdue = 0.00;
$prevbal=0.00;
$latechg=0.00;
$secdep=0.00;
$damage=0.00;
$courtcost=0.00;
$nsf=0.00;
$paidsum=0.00;
$comments="";

$id="id";
/* ---------------------------------------------------------------- */
// Attempt select query execution
$sql = 'SELECT * FROM payfile WHERE amtpaid = ?';
$stmt = $pdo->prepare('SELECT * FROM payfile WHERE amtpaid = ?');
$stmt->execute([$amtpaid]);
$user = $stmt->fetch();
/* ---------------------------------------------------------------- */
$due = $prevbal + $latechg + $secdep + $damage + $courtcost + $nsf;
$owed = $due + $amtdue;

/* if no payment or partial payment, add $10 to latechg field and amount not paid to prevbal field */
if ($amtpaid < $owed)
{ $latechg = $latechg + 10.00; $prevbal = $owed - $amtpaid; }
/* if payment = amtdue clear due */
elseif ($amtpaid == $owed)
{ $prevbal = 0.00; $latechg = 0.00; $secdep = 0.00; $damage = 0.00; $courtcost = 0.00; $nsf = 0.00; }
/* if over-payment subtract over-payment from prevbal field */
elseif ($amtpaid > $owed )
{ $prevbal = $amtpaid - $owed; $latechg = 0.00; $secdep = 0.00; $damage = 0.00; $courtcost = 0.00;
$nsf = 0.00; }
$amtpaid = $paidsum;
/* ---------------------------------------------------------------- */
$sql = "UPDATE payfile SET amtpaid=?, prevbal=?, latechg=?,secdep=?,
damage=?, courtcost=?, nsf=?, paidsum=? WHERE unit=?";
$pdo->prepare($sql)->execute([$receiptno, $id]);
/* ---------------------------------------------------------------- */
?>

<br>
<div class="containerBox">
<img class="img-responsive" src="apt-pic.jpg" height=200 width=500>
<div class='text-box'>
<p class='dataNumber'><input type="text" size = 65 STYLE="color: #000000;
background-color: #D4AAFF;" value="Company Name"><br>
</div></div>

<STYLE TYPE="text/css">
.containerBox {
position: relative;
display: inline-block;
}

.text-box {
position: absolute;
height: 30%;
text-align: center;
width: 100%;
margin: auto;
top: 0;
bottom: 0;
right: 0;
left: 0;
font-size: 30px;
}

.img-responsive {
display: block;
max-width: 100%;
height: 120px;
margin: auto;
padding: auto;
}

.dataNumber {
margin-top: auto;
}
</STYLE>
<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 = 50 STYLE="color: #000000; background-color: #D4AAFF;" name="Name" value="Business Name"><br>
<input type="text" size = 50 STYLE="color: #000000; background-color: #D4D4FF;" name="Addy1" value="Business address"><br>
<input type="text" size = 50 STYLE="color: #000000; background-color: #D4AAFF;" name="Addy2" value="City, State, Zip"><br>

<b> tenant paying: <?php echo $_POST["unit"]; ?> -
Amount paid: <?php echo $_POST["amtpaid"]; ?> -
Date paid: <?php echo $_POST["datepaid"]; ?> -
Amount due: <?php echo $prevbal; ?><br>
<input type="text" size = 75 STYLE="color: #000000; background-color: #D4AAFF;" name="sign" value="Sign here">
<input type="text" size = 25 STYLE="color: #000000; background-color: #D4AAFF;" name="thanks" value="We Thank You"><br>
</b></center></body></html>
 

Cromewell

Administrator
Staff member
In the switch to PDO you are now using variable binding, which is a much better way to build your queries.

But this issue will still remain, you are not getting receiptno from anywhere. So you either need to initialize it to a static value or fetch it from somewhere - the database, a submitted value from a form, etc
*** no, receiptno is a field in Numbers table that, after
updating, is to be displayed on the receipt. This was my major issue all along. I could believe that
declaring a variable as 0 or 0.00($receiptno = 0;) could mean it was numeric and not change it to that.
This is the first time you are referencing $receiptno. So you will get a warning/error
$pdo->prepare($sql)->execute([$receiptno, $id]);
echo $receiptno;
 

1Tsme1941

Member
I tried the following:

// Getting the receiptno based on id
$stmt = $pdo->prepare("SELECT receiptno FROM numbers WHERE id=?");
$stmt->execute([$id]);
$name = $stmt->fetchColumn();

then got: Undefined variable: id on line 28, Undefined variable: receiptno on line 76 and line 77

How to code this to increment receiptno - $sql = "UPDATE users SET receiptno = ? WHERE id = ?";
$pdo->prepare($sql)->execute([$receiptno, $id]);
-------------------------------------------------------------------------
the code:
<html>
<head>
<title>make payment and print receipt</title>
</head>
<body><center>

<?php
$host = '127.0.0.1';
$db = 'prerentdb';
$user = 'root';
$pass = '';
$charset = 'utf8mb4';

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try {
$pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
/* ---------------------------------------------------------------- */
// Getting the receiptno based on id
$stmt = $pdo->prepare("SELECT receiptno FROM numbers WHERE id=?");
$stmt->execute([$id]); // 28
$name = $stmt->fetchColumn();
/* ---------------------------------------------------------------- */

$unit = $_POST['unit'];
$amtpaid = $_POST['amtpaid'];
$hudpay = $_POST['hudpay'];
$datepaid = $_POST['datepaid'];

$amtdue = 0.00;
$prevbal=0.00;
$latechg=0.00;
$secdep=0.00;
$damage=0.00;
$courtcost=0.00;
$nsf=0.00;
$paidsum=0.00;
$comments="";

$id="id";
/* ---------------------------------------------------------------- */
// Attempt select query execution
$sql = 'SELECT * FROM payfile WHERE amtpaid = ?';
$stmt = $pdo->prepare('SELECT * FROM payfile WHERE amtpaid = ?');
$stmt->execute([$amtpaid]);
$user = $stmt->fetch();
/* ---------------------------------------------------------------- */
$due = $prevbal + $latechg + $secdep + $damage + $courtcost + $nsf;
$owed = $due + $amtdue;

/* if no payment or partial payment, add $10 to latechg field and amount not paid to prevbal field */
if ($amtpaid < $owed)
{ $latechg = $latechg + 10.00; $prevbal = $owed - $amtpaid; }
/* if payment = amtdue clear due */
elseif ($amtpaid == $owed)
{ $prevbal = 0.00; $latechg = 0.00; $secdep = 0.00; $damage = 0.00; $courtcost = 0.00; $nsf = 0.00; }
/* if over-payment subtract over-payment from prevbal field */
elseif ($amtpaid > $owed )
{ $prevbal = $amtpaid - $owed; $latechg = 0.00; $secdep = 0.00; $damage = 0.00; $courtcost = 0.00;
$nsf = 0.00; }
$amtpaid = $paidsum;
/* ---------------------------------------------------------------- */
$sql = "UPDATE payfile SET amtpaid=?, prevbal=?, latechg=?,secdep=?,
damage=?, courtcost=?, nsf=?, paidsum=? WHERE unit=?";
$pdo->prepare($sql)->execute([$amtpaid, $prevbal, $latechg, $secdep, $damage, $courtcost, $nsf,
$paidsum, $unit]);
/* ---------------------------------------------------------------- */
$sql = "UPDATE users SET receiptno = ? WHERE id = ?";
$pdo->prepare($sql)->execute([$receiptno, $id]); // 76
echo $receiptno; // 77
/* ---------------------------------------------------------------- */
?>

<br>
<div class="containerBox">
<img class="img-responsive" src="apt-pic.jpg" height=200 width=500>
<div class='text-box'>
<p class='dataNumber'><input type="text" size = 65 STYLE="color: #000000;
background-color: #D4AAFF;" value="Company Name"><br>
</div></div>

<STYLE TYPE="text/css">
.containerBox {
position: relative;
display: inline-block;
}

.text-box {
position: absolute;
height: 30%;
text-align: center;
width: 100%;
margin: auto;
top: 0;
bottom: 0;
right: 0;
left: 0;
font-size: 30px;
}

.img-responsive {
display: block;
max-width: 100%;
height: 120px;
margin: auto;
padding: auto;
}

.dataNumber {
margin-top: auto;
}
</STYLE>
<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 = 50 STYLE="color: #000000; background-color: #D4AAFF;" name="Name" value="Business Name"><br>
<input type="text" size = 50 STYLE="color: #000000; background-color: #D4D4FF;" name="Addy1" value="Business address"><br>
<input type="text" size = 50 STYLE="color: #000000; background-color: #D4AAFF;" name="Addy2" value="City, State, Zip"><br>

<b> tenant paying: <?php echo $_POST["unit"]; ?> -
Amount paid: <?php echo $_POST["amtpaid"]; ?> -
Date paid: <?php echo $_POST["datepaid"]; ?> -
Amount due: <?php echo $prevbal; ?><br>
<input type="text" size = 75 STYLE="color: #000000; background-color: #D4AAFF;" name="sign" value="Sign here">
<input type="text" size = 25 STYLE="color: #000000; background-color: #D4AAFF;" name="thanks" value="We Thank You"><br>
</b></center></body></html>
------------------------------------------------------------------
this is result:
Notice: Undefined variable: id in C:\xampp\htdocs\property\paymeno.php on line 28

Notice: Undefined variable: receiptno in C:\xampp\htdocs\property\paymeno.php on line 76

Notice: Undefined variable: receiptno in C:\xampp\htdocs\property\paymeno.php on line 77

tenant paying: apt1 - Amount paid: 530.00 - Date paid: 2021-07-10 - Amount due: 530
 

Cromewell

Administrator
Staff member
// Getting the receiptno based on id
$stmt = $pdo->prepare("SELECT receiptno FROM numbers WHERE id=?");
$stmt->execute([$id]);
$name = $stmt->fetchColumn();

then got: Undefined variable: id on line 28, Undefined variable: receiptno on line 76 and line 77

How to code this to increment receiptno - $sql = "UPDATE users SET receiptno = ? WHERE id = ?";
$pdo->prepare($sql)->execute([$receiptno, $id]);
In the first case, receiptno as selected from your table would be in $name. So you probably want $receiptno = $stmt->fetchColumn();

If you are looking to increment from what you just selected, $receiptno++;

But you may actually be looking for auto increment in mysql, https://dev.mysql.com/doc/refman/8.0/en/example-auto-increment.html
 

1Tsme1941

Member
I changed to:

// Getting the receiptno based on id
$stmt = $pdo->prepare("SELECT receiptno FROM numbers WHERE id=?");
$stmt->execute([$id]); // line 28
$receiptno = $stmt->fetchColumn();

then got: Undefined variable: id on line 28
no updates

looking for syntax for updating "receiptno + 1". haven't found
 

Cromewell

Administrator
Staff member
You have simply moved the issue to $id. It is not coming from anywhere, that is the first time you are using it.
looking for syntax for updating "receiptno + 1". haven't found
It depends on where exactly you are trying to increment it. I gave you a couple options above. To write back to the database, simple select it out first, as you are doing, then $receiptno++ , then update the row with the incremented value. You could even 'update table set col1 = $receiptno + 1'
 
Top