How to define variables as numeric

Hi, I have some issues with my code. It’s to refresh account payfile at end of month. Comments are noted
in code (//).
#1) how to define variables as numeric(I’ve tried several examples(forums and manuals)
#2) how to add several variables together so they are known as numeric(they are all decimal 8,2 in the database)
#3) Please, someone explain the “Undefined variable: mysql”.

<?php
//Open a new connection to the MySQL server
$link = mysqli_connect("localhost", "root", "", "prerentdb"); 

// Check connection
if($link === false){ die("ERROR: Could not connect. " . mysqli_connect_error()); }
//MySqli Select Query
$sql = "select * FROM payfile";
echo "<center>";echo date('m/d/y');echo "<br />";
$due=0;
  $prevbal="prevbal";
  $latechg="latechg";
   $secdep="secdep";
   $damage="damage";
$courtcost="courtcost";
      $nsf="nsf";
   $amtdue="amtdue";
   $amtpaid="amtpaid";
  $paidsum="paidsum";

$due = $prevbal + $latechg + $secdep + $damage + $courtcost + $nsf; // Warning: A non-numeric value encountered x 5 line 21
$amtdue = $amtdue + $due;  Warning: // A non-numeric value encountered x 1 line 22

// if no payment or partial payment, add $10 to latechg field and amount not paid to prevbal field
if ($amtpaid < $amtdue)  // Notice: Undefined variable: amtpaid
	{ $latechg = $latechg + 10; $prevbal = $amtdue - $amtpaid;  }
// if payment = amtdue clear due
if ($amtpaid == $amtdue) // Notice: Undefined variable: amtpaid
	{ $prevbal = 0; $latechg = 0; }
// if over-payment subtract over-payment
// from prevbal field
if ($ampaid > $amtdue )  // Notice: Undefined variable: amtpaid
{ $amtdue = $amtpaid  - $amtdue;  $prevbal = 0; $latechg = 0;  }
$secdep = 0; $damage = 0; $courtcost = 0; $nsf = 0;

// refresh every record - give every record the below values
$amtpaid = '0.00';
$hudpay = '0.00';
$datepaid = ' ';
$paidsum = '0.00';
$comments = ' ';

// Perform a query, check for error
if (!$mysqli -> query("UPDATE payfile SET // Undefined variable: mysqli 
Fatal error: Uncaught Error: Call to a member function query() on null 
 prevbal='$prevbal',latechg='$latechg', hudpay='$hudpay', amtpaid='$amtpaid', 
datepaid='$datepaid', comment='$comment', paidsum='$paidsum' 
where unit = $unit"));
mysqli_query($sql) or die(mysql_error());
?>

these are error messages:

Warning: A non-numeric value encountered in C:\xampp\htdocs\property\refreshpayments.php on line 21

Warning: A non-numeric value encountered in C:\xampp\htdocs\property\refreshpayments.php on line 21

Warning: A non-numeric value encountered in C:\xampp\htdocs\property\refreshpayments.php on line 21

Warning: A non-numeric value encountered in C:\xampp\htdocs\property\refreshpayments.php on line 21

Warning: A non-numeric value encountered in C:\xampp\htdocs\property\refreshpayments.php on line 21

Warning: A non-numeric value encountered in C:\xampp\htdocs\property\refreshpayments.php on line 21

Warning: A non-numeric value encountered in C:\xampp\htdocs\property\refreshpayments.php on line 22

Notice: Undefined variable: ampaid in C:\xampp\htdocs\property\refreshpayments.php on line 32

Notice: Undefined variable: mysqli in C:\xampp\htdocs\property\refreshpayments.php on line 44

Fatal error: Uncaught Error: Call to a member function query() on null  on line 44

Regarding the non-numeric variables, you have it set to a string here on line 11:

$courtcost="courtcost"; 

And the other mysql errors probaby stems from the fact that you are using two different styles of PHP ( MySQLi Procedural to connect, and what looks like MySQLi Object-oriented to run the query).

When running a query, you need to use the connection variable ($link) and not an undefined variable ($mysqli).

You need to make sure all your database code is of the same type, and is configured correctly using the right variables. See this collection of articles to help you out:

I believe this is closer, the numeric errors are gone but the errors left are commented in the code. Thanks so much for your advice.

<?php
echo "<center>";echo date('m/d/y');echo "<br />";
//connect to database
$mysqli = new mysqli("localhost", "root", "", "prerentdb");

//check for errors
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: " . $mysqli->connect_error;
}

//MySqli Select Query
$res = $mysqli->query("SELECT  * FROM payfile");

//fetch query
$row = $res->fetch_assoc();
$due=0;
//display results
/* echo $row['amtdue'];
echo $row['amtpaid'];
echo $row['prevbal'];
echo $row['latechg'];
echo $row['secdep'];
echo $row['damage'];
echo $row['courtcost'];
echo $row['nsf'];
echo $row['paidsum'];
echo $row['comments']; */

if ($result = $mysqli->query($sql)) { // Undefined variable: sql& Warning:  Empty query
        while($obj = $result->fetch_object()){
           $due = $obj->prevbal + $obj->latechg + $obj->secdep + $obj->damage + $obj->courtcost + $obj->NSF;
           $amtdue = $obj->amtdue + due;
// do these need to be variables or can your code just use $obj?
           $prevbal =$obj->prevbal;
           $latechg = $obj->latechg;
           $secdep = $obj->secdep;
           $damage = $obj->damage;
           $courtcost = $obj->courtcost;
           $nsf = $obj->nsf;

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

// refresh every record - give every record the below values
$amtpaid = '0.00';
$hudpay = '0.00';
$datepaid = ' ';
$paidsum = '0.00';
$comments = ' ';

// Perform a query, check for error
if (!$mysqli -> query("UPDATE payfile SET  
 prevbal='$prevbal',latechg='$latechg', hudpay='$hudpay', amtpaid='$amtpaid', 
datepaid='$datepaid', comment='$comment', paidsum='$paidsum' 
where unit = $ unit"));
mysqli_query($sql) or die(mysql_error());
 
           
        }
    }
   $result->close(); // Fatal error:  Call to a member function close() on bool
?>
  1. You call mysqli->query($sql); but you don’t have a variable called $sql - the error message tells you this. Did you mean to execute a query here? You’ve already executed select * from payfile and then retrieved the first result into $row a few lines further back. Shouldn’t you now be looping through those results instead of running another query? If not, what was the point of the first query as you don’t do anything with the results?

  2. “Fatal error call to a member function close() on bool” is because your query didn’t run because of error 1, and therefore $result is false, not a results pointer. You can’t close something that wasn’t opened in the first place. I rarely close results objects anyway, because they will automatically be closed when the script ends. If you insist on closing it, close it inside your if() clause and it will only close if it was successfully opened.

  3. Do these need to be variables or can I just use obj? - there’s no point creating variables for the sake of it, so you could just use your object directly.

  4. mysqli_query($sql) or die (mysql_error()) - again, you’re trying to execute a query in $sql when you didn’t define that variable anywhere, so once you’ve fixed the first error you’ll get one here. Then you use the old, removed-from-the-latest-PHP mysql_error() even though the rest of your code is using mysqli. Do you really want the code to just drop out of the loop if one query fails part-way through? Surely you’d want it to continue processing the loop? As this is updating balances, that will lead to all sorts of problems when you need to re-run the code.

In this bit:

// Perform a query, check for error
if (!$mysqli -> query("UPDATE payfile SET  
 prevbal='$prevbal',latechg='$latechg', hudpay='$hudpay', amtpaid='$amtpaid', 
datepaid='$datepaid', comment='$comment', paidsum='$paidsum' 
where unit = $ unit"));

what’s the point of the if() clause surrounding this query? You don’t have an else or anything conditional dependent on whether the query executed. And where did $unit come from? I don’t see that anywhere in your code. I think you’d also need to specify it as $unit rather than as $ unit, but you can fix that when you change to Prepared Statements.

If you indented the code consistently it would be much easier to read. You also need to look at Prepared Statements instead of concatenating variables into the query like that.

I’m sure I’ve seen this code on here before.

2 Likes

It’s the ‘rental payment’ programming class exercise.

@ckguitarz, you should not update the amount in a table to do this. You should INSERT a new row of data for every +/- transaction that affects a value, then SUM() the amounts in an sql query to get the current balance at any point in time. At the end of the month, when you calculate any late charge for all of the records, you would INSERT a new row with the late charge amount for any unit (account) number where the payments are less than the amount due. Also, rather than to spread out the different types of amounts in different columns, you would have a single type_id column, and also a database table that holds the type definition id/name data.

1 Like

I quite agree, maintaining totals without any transactions is a bad thing in accounting, and maintaining both means that they eventually won’t agree. Just keep transactions and total them as you need to.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.