Computer Hope

Software => Computer programming => Topic started by: guitarzRus on March 28, 2023, 02:24:51 PM

Title: someone help with mysqli update
Post by: guitarzRus on March 28, 2023, 02:24:51 PM
Howdy folks, my new code is below. I'm trying to update any records that have paid(amtpaid !=0, if
that's correct). I'm not sure how to code line 28-38 but I think it's wrong. The if statement at
line 45 is intended for records that have paid (amtpaid), as is everything. The duedate is not
advanced. I have tried to resolve several of these issues for some time. There is no error. Anyone
want to help?
----------------------------------------------------------------
the code:
<html>
   <head>
      <title>Refresh payment database file</title>
   </head>
   <body>
   <?php

$link = mysqli_connect("localhost", "root", "", "homedb");
 
// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

 //MySqli Select Query
  $sql = "select * FROM paytbl where amtpaid !=0";
$result = mysqli_query($link,$sql);
if (!$result) {
    printf("Error: %s\n", mysqli_error($link));
    exit();
}else{
   $results = array();
   while($row = mysqli_fetch_array($result)){
      $results[] = $row;
   }
 $id='id';
$amtpaid = (int)'amtpaid'; // line 28
$amtdue = (int)'amtdue';
$prevbal = (int)'prevbal';
$latechg = (int)'latechg';
$secdep = (int)'secdep';
$damage = (int)'damage';
$courtcost = (int)'courtcost';
$nsf = (int)'nsf';
$hudpay = (int)'hudpay';
$comments = 'comments';
$paiddate = 'paiddate'; // line 38
$due= 0;
// ----------------------------------------

$due = $amtdue + $prevbal + $latechg + $secdep + $damage + $courtcost + $nsf - $hudpay;
 
 /* if no pay or part pay, add $35 to latechg field and amount not paid to prevbal field */
    if ($amtpaid < $due) { $prevbal = $amtdue - $amtpaid; $latechg = $latechg + 35.00; // line 45
$amtpaid = 0; $secdep = 0; $damage = 0;  $courtcost = 0;
$nsf = 0; $hudpay = 0; $comments = ' ';   }
/* if over-payment subtract over-payment from prevbal */
    if ($amtpaid > $due)  { $prevbal = $amtpaid - $amtdue;
$amtpaid = 0; $secdep = 0; $damage = 0;  $courtcost = 0;
$nsf = 0; $hudpay = 0; $comments = ' ';   }

         // Perform a query, check for error
$sql = "UPDATE paytbl SET
amtpaid = '$amtpaid', duedate = DATE_ADD(duedate, INTERVAL 1 MONTH), prevbal = '$prevbal',
latechg = '$latechg', secdep = '$secdep', damage = '$damage', courtcost = '$courtcost', nsf = '$nsf', 
hudpay = '$hudpay', comments = '$comments' WHERE amtpaid !=0";
 
if(mysqli_query($link, $sql)){ echo "record was updated successfully."; }
else { echo "ERROR: Could not able to execute $sql. " . mysqli_error($link); }
 // Close connection
mysqli_close($link);
   }
?>
   </body></html>