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". the code: <?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()); ?> PHP: 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
There's a lot to work through there. PHP is very flexible and doesn't have strict variable typing like other languages do. Lets take a peak at the first errors $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 PHP: all the values that get added together are strings do due will be a string. I'm wondering if $prevbal="prevbal"; PHP: is meant to be picking up a value from the SQL request... however you create the SQL for the request, you never actually go to the database to retrieve the data.
I'm not sure if I understood, "I'm wondering if $prevbal="prevbal"; PHP: is meant to be picking up a value from the SQL request... however you create the SQL for the request, you never actually go to the database to retrieve the data." but I made these changes: $sql = "select amtdue,amtpaid,paidsum,prevbal,latechg,secdep,damage,courtcost,nsf, FROM payfile"; $due=0; $prevbal="prevbal"; $latechg="latechg"; $secdep="secdep"; $damage="damage"; $courtcost="courtcost"; $nsf="nsf"; $amtdue="amtdue"; var_dump(is_int($amtdue)); // this showed that it is not a number otherwise, no change.
You have $amtdue = "amtdue"; PHP: that means create a variable we'll call amtdue and give it this string "amtdue". You never actually send this to the database $sql = "select amtdue,amtpaid,paidsum,prevbal,latechg,secdep,damage,courtcost,nsf, FROM payfile"; PHP: you need to run something like if ($result = $mysqli->query($sql)) { 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; // now you have to do something with them before the loop ends and it gets a new record } } $result->close(); PHP: FWIW MySQL probably won't like that comma after the nsf in $sqll
I so appreciate the advice. the numeric errors are gone but you see the comments below: <?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()); // now you have to do something with them before the loop ends and it gets a new record } } $result->close(); // Fatal error: Call to a member function close() on bool ?> PHP:
I 'm trying to recode to prepared and got kind of balled up. My one error displayed is commented ( //) in the code on line 18. Am I getting closer? <?php echo "<center>";echo date('m/d/y');echo "<br />"; $servername = "localhost"; $username = "root"; $password = ""; $dbname = "prerentdb"; // Create connection $mysqli = new mysqli($servername, $username, $password, $dbname); // Check connection if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: " . $mysqli->connect_error; exit(); } $sql = "SELECT * FROM payfile WHERE id=?"; $stmt = prepared_query($conn, $sql, [$id]); // *** line 18 Fatal error: Uncaught Error: Call to undefined function prepared_query() *** $payfile = $stmt->get_result()->fetch_all(MYSQLI_ASSOC); $due=0; while($obj = fetch_object()){ $due = $obj->prevbal + $obj->latechg + $obj->secdep + $obj->damage + $obj->courtcost + $obj->NSF; $amtdue = $obj->amtdue + due; // 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 = ' '; // prepare and bind $stmt = $mysqli->prepare("UPDATE payfile SET prevbal=?, latechg=?, hudpay=?, amtpaid=?, datepaid=?, comment=?, paidsum=? WHERE unit=?"); $stmt->bind_param("iiiissi", $prevbal, $latechg, $hudpay, $amtpaid, $datepaid, $comment, $paidsum); // set parameters and execute $prevbal = "prevbal"; $latechg = "latechg"; $hudpay = "hudpay"; $amtpaid = "amtpaid"; $datepaid = "datepaid"; $comment = "comment"; $paidsum = "paidsum"; $unit = "unit"; if ($stmt->execute()) { echo "updated successfully"; } else { echo "Failed to update Item"; } //var_dump($stmt); $stmt->close(); $mysqli->close(); } ?> PHP:
I haven't used mysqli much but it looks like you're on the right track but you've still got a lot of old and new code in there... don't be afraid of white space, your code will be more readable when you come back to make a change months or years later if each statement is on it's own line how does this look? <?php echo "<center>";echo date('m/d/y');echo "<br />"; $servername = "localhost"; $username = "root"; $password = ""; $dbname = "prerentdb"; // Create connection $mysqli = new mysqli($servername, $username, $password, $dbname); // Check connection if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: " . $mysqli->connect_error; exit(); } // you don't specify where the id is coming from, //I'm going to assume it's from a get parameeter in the url $unit = filter_input(INPUT_GET, 'unit'); $sql = "SELECT * FROM payfile WHERE id=?"; //$stmt = prepared_query($conn, $sql, [$id]); // *** line 18 Fatal error: Uncaught Error: Call to undefined function prepared_query() *** // that's because its not a prepared query and the call is wrong $stmt = $mysqli->prepare($sql); $stmt->bind_param($unit); // we don't want an associative array $payfile = $stmt->get_result()->fetch_all(); $due=0; while($obj = fetch_object()){ // is this add up right? $due = $obj->prevbal + $obj->latechg + $obj->secdep + $obj->damage + $obj->courtcost + $obj->NSF; $amtdue = $obj->amtdue + due; // if no payment or partial payment, add $10 to latechg field and amount not paid to prevbal field $latechg = 0; $prevbal = 0; if ($obj->amtpaid < $obj->amtdue) { $latechg += 10; $prevbal = $obj->amtdue - $obj->amtpaid; } // if over-payment subtract over-payment // from prevbal field if ($amtpaid > $amtdue ) { $amtdue = $amtpaid - $amtdue; } // $nsf doesn't get used or saved $secdep = $damage = $courtcost = $nsf = 0; // refresh every record - give every record the below values $amtpaid = '0.00'; $hudpay = '0.00'; $datepaid = ' '; $comment = ' '; $paidsum = '0.00'; // prepare and bind $stmt = $mysqli->prepare("UPDATE payfile SET prevbal=?, latechg=?, hudpay=?, amtpaid=?, datepaid=?, comment=?, paidsum=? WHERE unit=?"); $stmt->bind_param($prevbal, $latechg, $hudpay, $amtpaid, $datepaid, $comment, $paidsum, $unit); if ($stmt->execute()) { echo "updated successfully"; } else { echo "Failed to update Item"; } //var_dump($stmt); $stmt->close(); } $mysqli->close(); ?> PHP: FYI eventually you'll want to create functions to keep the code super tidy, but for now we just want it to run.
In trying to resolve previous issues and further researching, new code produced new messages. The instruction for preparing the "select" obviously were not right? <?php echo "<center>";echo date('m/d/y');echo "<br />"; $servername = "localhost"; $username = "root"; $password = ""; $dbname = "prerentdb"; // Create connection $mysqli = new mysqli($servername, $username, $password, $dbname); // Check connection if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: " . $mysqli->connect_error; exit(); } // Select some data with unit as criteria $statement = $mysqli->prepare("select unit, $amtpaid, $datepaid, $prevbal, $latechg, $hudpay, $comment, $paidsum from payfile where unit = ?"); $statement->bind_param("s", $unit); // Fill our parameters $unit = $_POST["unit"]; $amtpaid = $_POST["amtpaid"]; $datepaid = $_POST["datepaid"]; $prevbal = $_POST["prevbal"]; $latechg = $_POST["latechg"]; $hudpay = $_POST["hudpay"]; $comment = $_POST["comment"]; $paidsum = $_POST["paidsum"]; $statement->execute(); // Execute the statement. $result = $statement->get_result(); // Binds the last executed statement as a result. $stmt = $mysqli->prepare($sql); $stmt->bind_param($unit); //Warning: Wrong parameter count for mysqli_stmt::bind_param() line 19 $payfile = $stmt->get_result()->fetch_all(); //Fatal error: Uncaught Error: Call to a member function fetch_all() on bool line 21 $due=0; while($obj = fetch_object()){ $due = $obj->prevbal + $obj->latechg + $obj->secdep + $obj->damage + $obj->courtcost + $obj->NSF; $amtdue = $obj->amtdue + due; // if no payment or partial payment, add $10 to latechg field and amount not paid to prevbal field $latechg = 0; $prevbal = 0; if ($obj->amtpaid < $obj->amtdue) { $latechg += 10; $prevbal = $obj->amtdue - $obj->amtpaid; } // if over-payment subtract over-payment // from prevbal field if ($amtpaid > $amtdue ) { $amtdue = $amtpaid - $amtdue; } // $nsf doesn't get used or saved $secdep = $damage = $courtcost = $nsf = 0; // refresh every record - give every record the below values $amtpaid = '0.00'; $hudpay = '0.00'; $datepaid = ' '; $comment = ' '; $paidsum = '0.00'; // prepare and bind $stmt = $mysqli->prepare("UPDATE payfile SET prevbal=?, latechg=?, hudpay=?, amtpaid=?, datepaid=?, comment=?, paidsum=? WHERE unit=?"); $stmt->bind_param($prevbal, $latechg, $hudpay, $amtpaid, $datepaid, $comment, $paidsum, $unit); if ($stmt->execute()) { echo "updated successfully"; } else { echo "Failed to update Item"; } //var_dump($stmt); $stmt->close(); } $mysqli->close(); ?> PHP: result: Notice: Undefined variable: amtpaid, datepaid, prevbal, latechg, hudpay, comment, paidsum on line 16 How to define here? Fatal error: Uncaught Error: Call to a member function fetch_all() on bool line 18 how to resolve?
Your original select statement was fine, why the change to mention the column names? You now have $statement = $mysqli->prepare("select unit, $amtpaid, $datepaid, $prevbal, $latechg, $hudpay, $comment, $paidsum from payfile where unit = ?"); PHP: Because you've put $amtpaid in there that's going to fail because there is no variable yet with that name. If you need to have the column names in there (and sometimes it's better) do it like this $statement = $mysqli->prepare("select `unit`, `amtpaid`, `datepaid`, `prevbal`, `latechg`, `hudpay`, `comment`, `paidsum` from `payfile` where `unit` = ?"); PHP:
I took out the statement with "id" only get the fatal error and don't know how to resolve? <?php echo "<center>";echo date('m/d/y');echo "<br />"; $servername = "localhost"; $username = "root"; $password = ""; $dbname = "prerentdb"; // Create connection $mysqli = new mysqli($servername, $username, $password, $dbname); // Check connection if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: " . $mysqli->connect_error; exit(); } $unit = filter_input(INPUT_GET, 'unit'); $sql = "SELECT * FROM payfile WHERE unit=?"; $stmt = $mysqli->prepare($sql); // $stmt->bind_param($id); $payfile = $stmt->get_result()->fetch_all(); $due=0; while($obj = fetch_object()){ // is this add up right? $due = $obj->prevbal + $obj->latechg + $obj->secdep + $obj->damage + $obj->courtcost + $obj->NSF; $amtdue = $obj->amtdue + due; // if no payment or partial payment, add $10 to latechg field and amount not paid to prevbal field $latechg = 0; $prevbal = 0; if ($obj->amtpaid < $obj->amtdue) { $latechg += 10; $prevbal = $obj->amtdue - $obj->amtpaid; } // if over-payment subtract over-payment // from prevbal field if ($amtpaid > $amtdue ) { $amtdue = $amtpaid - $amtdue; } // $nsf doesn't get used or saved $secdep = $damage = $courtcost = $nsf = 0; // refresh every record - give every record the below values $amtpaid = '0.00'; $hudpay = '0.00'; $datepaid = ' '; $comment = ' '; $paidsum = '0.00'; // prepare and bind $stmt = $mysqli->prepare("UPDATE payfile SET prevbal=?, latechg=?, hudpay=?, amtpaid=?, datepaid=?, comment=?, paidsum=? WHERE unit=?"); $stmt->bind_param($prevbal, $latechg, $hudpay, $amtpaid, $datepaid, $comment, $paidsum, $unit); if ($stmt->execute()) { echo "updated successfully"; } else { echo "Failed to update Item"; } //var_dump($stmt); $stmt->close(); } $mysqli->close(); ?>