How to set variables as numeric – PHP – SitePoint Forums

0

Hello, I have some problems with my code. This is to update the account payment file at the end of the month. Comments are noted
in code (//).
#1) how to define variables as numeric (I tried several examples (forums and manuals)
#2) how to add multiple 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”.

";echo date('m/d/y');echo "
"; $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:xampphtdocspropertyrefreshpayments.php on line 21 Warning: A non-numeric value encountered in C:xampphtdocspropertyrefreshpayments.php on line 21 Warning: A non-numeric value encountered in C:xampphtdocspropertyrefreshpayments.php on line 21 Warning: A non-numeric value encountered in C:xampphtdocspropertyrefreshpayments.php on line 21 Warning: A non-numeric value encountered in C:xampphtdocspropertyrefreshpayments.php on line 21 Warning: A non-numeric value encountered in C:xampphtdocspropertyrefreshpayments.php on line 21 Warning: A non-numeric value encountered in C:xampphtdocspropertyrefreshpayments.php on line 22 Notice: Undefined variable: ampaid in C:xampphtdocspropertyrefreshpayments.php on line 32 Notice: Undefined variable: mysqli in C:xampphtdocspropertyrefreshpayments.php on line 44 Fatal error: Uncaught Error: Call to a member function query() on null on line 44


As for the non-numeric variables, you’ve set it to a string here on line 11:

$courtcost="courtcost"; 

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

When executing a query, you must use the connection variable ($link) and not an undefined variable ($mysqli).

You need to make sure that all the code in your database is of the same type and is set up correctly using the correct variables. Check out this collection of articles to help you:

I believe it’s closer, the numeric errors are gone but the remaining errors are commented out in the code. Thank you very much for your advice.

";echo date('m/d/y');echo "
"; //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 so. Did you mean to run a query here? You have already run select * from payfile then retrieved the first result in $row a few lines later. Shouldn’t you now iterate through these results instead of running another query? If not, what was the use of the first query since you don’t do anything with the results?

  2. “Fatal error call to a close() member function on bool” is because your query did not execute due to error 1, and so $result is false, not a result pointer. You can’t close something that wasn’t opened in the first place. I rarely close result objects anyway, as they will be automatically 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 opened successfully.

  3. Do these have to be variables or can I just use obj ? – there’s no point in creating variables for fun, so you can just use your object directly.

  4. mysqli_query($sql) or die (mysql_error()) – again you are trying to run a query in $sql when you haven’t set that variable anywhere, so once you fix the first error, you’ll get one here. Then you are using the old PHP removed from the latest mysql_error() even if the rest of your code uses mysqli. Do you really want the code to break out of the loop if a query fails halfway? Surely you want it to continue processing the loop? Since it’s updating balances, it will cause all sorts of problems when you have to run the code again.

In this piece:

// 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 is the point of if() clause surrounding this request? You do not have else or anything conditional on whether the query was executed. And where does $unit comes from? I don’t see this anywhere in your code. I think you would also need to specify it as $unit rather than like $ unitbut you can solve this problem when you switch 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 in the query like this.

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



2 likes

This is the “rental payment” programming exercise.

@ckguitarz you don’t have to update the amount in an array to do this. You need to INSERT a new row of data for each +/- transaction that affects a value, then SUM() the amounts in a sql query to get the current balance at any time. At the end of the month, when calculating late fees for all records, you will INSERT a new row with the late fee amount for any unit number (account) where the payments are less than the amount due. Also, rather than splitting the different amount types into different columns, you would have a single type_id column, along with a database table containing the id/name type definition data.



1 like

I totally agree, keeping totals without any transactions is a bad thing in accounting, and keeping both means they won’t agree in the end. Simply keep transactions and total them as needed.



1 like

Share.

Comments are closed.