ckir5951 Posted August 4, 2022 Share Posted August 4, 2022 Quote 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()); ?> Quote 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 Quote Link to comment https://forums.phpfreaks.com/topic/315139-how-to-define-variables-as-numeric/ Share on other sites More sharing options...
ginerjm Posted August 4, 2022 Share Posted August 4, 2022 It would be extremely helpful if you tied the messages to the exact line that is giving you the error AND it definitely helps if you code only one instruction per line. Quote Link to comment https://forums.phpfreaks.com/topic/315139-how-to-define-variables-as-numeric/#findComment-1598980 Share on other sites More sharing options...
dodgeitorelse3 Posted August 5, 2022 Share Posted August 5, 2022 (edited) $due = $prevbal + $latechg + $secdep + $damage + $courtcost + $nsf; // Warning: A non-numeric value encountered x 5 line 21 is equal to saying $due = "prevbal" + "latechg" + "secdep" + "damage" + "courtcost" + "nsf"; these are not numeric. Edited August 5, 2022 by dodgeitorelse3 Quote Link to comment https://forums.phpfreaks.com/topic/315139-how-to-define-variables-as-numeric/#findComment-1598981 Share on other sites More sharing options...
requinix Posted August 5, 2022 Share Posted August 5, 2022 53 minutes ago, dodgeitorelse3 said: $due = $prevbal + $latechg + $secdep + $damage + $courtcost + $nsf; // Warning: A non-numeric value encountered x 5 line 21 is equal to saying $due = "prevbal" + "latechg" + "secdep" + "damage" + "courtcost" + "nsf"; these are not numeric. As long as each one has the $ then it's a variable, and if undefined PHP will just treat it as null (and 0). The name-to-string thing applies only to constants, as in code like const PREVBAL = 1; const LATECHG = 2; const SECDEP = 3; const DAMGE = 4; // typo const COURTCOST = 5; const NSF = 6; $due = PREVBAL + LATECHG + SECDEP + DAMAGE + COURTCOST + NSF; and that behavior has actually been removed since PHP 8. Quote Link to comment https://forums.phpfreaks.com/topic/315139-how-to-define-variables-as-numeric/#findComment-1598982 Share on other sites More sharing options...
dodgeitorelse3 Posted August 5, 2022 Share Posted August 5, 2022 (edited) I stand corrected. My apologies. the code shown must be what is confusing me. $prevbal="prevbal"; $latechg="latechg"; $secdep="secdep"; $damage="damage"; $courtcost="courtcost"; $nsf="nsf"; $amtdue="amtdue"; $amtpaid="amtpaid"; Edited August 5, 2022 by dodgeitorelse3 Quote Link to comment https://forums.phpfreaks.com/topic/315139-how-to-define-variables-as-numeric/#findComment-1598983 Share on other sites More sharing options...
mac_gyver Posted August 5, 2022 Share Posted August 5, 2022 sorry for how this sounds, but this code looks like a collection of pieces pasted together that have almost nothing to do with each other, there's missing and mismatched variables, and even an obsolete mysql_error() call in it. is this part of a programming class or self learning? what learning resources have you used to get to this point? next, the most immediate problem, with the text strings being assigned to variables, resulting in the non numerical error messages when you try to add them together, appears like it was intended to be the result of fetching data from the SELECT query. this code isn't even executing that query, nor is it fetching the data from it. do you have a fundamental understanding of the steps needed to build, execute, and fetch data from a SELECT query? without this knowledge, it will be impossible for you to complete this assignment. also, this SELECT query needs a WHERE clause in it to match the unit you are interested in, like the UPDATE query has. have you actually defined what your input data needs to be, what processing you are going to perform on that data, and what result you are trying to produce? regardless of this being a class assignment or a real project, you should NOT update the values to keep track of amounts, since this does not provide an audit trail that would let you know if a programming mistake, accidental key press, or nefarious activity altered the values. you should instead insert a new row of data for every transaction that affects the amounts, just like your bank, credit card, utility, ... does this. here are some suggestions that will modernize and simplify the code - use the much simpler PDO extension. someone has already posted a link to a tutorial on a different help forum where you have posted this. use exceptions for database statement errors and in most cases simply let php catch the exception. this will let you remove any existing database statement error handling logic since it will no longer get executed upon an error, simplifying the code. name the database connection variable as to the type of connection it holds, such as $mysqli or $pdo, then use this variable name throughout the code. this is the cause of the undefined $msyqli variable error. your code is putting the database connection into a variable named $link, not $mysqli. if you are not getting this fundamental requirement of using the same variable for a particular purpose through out the code, you are not going to be able to complete this assignment. don't copy variables to other variables for nothing. just use the original variables that the data is in. Quote Link to comment https://forums.phpfreaks.com/topic/315139-how-to-define-variables-as-numeric/#findComment-1598988 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.