Parse error: syntax error

Discussion in 'PHP' started by pshaw, Jan 6, 2023.

  1. #1
    The objective is to add past month's payments to payment history table, refresh payment table at end of month for tenants who have paid and advance datedue 1 month,
    code:
    <?php
    $servername = "localhost";
    $username = "root";
    $password = " ";
    $dbname = "homedb";
    
    try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
      // set the PDO error mode to exception
      $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      $sql = "INSERT INTO payhist (tenant,unit,amtpaid,amtdue.datedue,prevbal,latechg,secdep,damage,
    courtcost,nsf,hudpay,datepaid,comments,phone,cell)
      VALUES ('$tenant','$unit','$amtpaid','$amtdue','$datedue','$prevbal','$latechg','$secdep','$damage',
    '$courtcost','$nsf','$hudpay','$datepaid','$comments','$phone','$cell')";
      // use exec() because no results are returned
      $conn->exec($sql);
      echo "New record created successfully";
    } catch(PDOException $e) {
    echo $sql . "<br>" . $e->getMessage();
    }
    
    $conn = null;
    
    $due='due';
    $tenant='tenant';
    $unit='unit';
    $amtdue = (int)'amtdue';
    $amtpaid = (int)'amtpaid';
    $datedue='datedue';
    $prevbal = (int)"prevbal";
    $latechg = (int)'latechg';
    $secdep = (int)'secdep';
    $damage = (int)'damage';
    $courtcost = (int)'courtcost';
    $nsf = (int)'nsf';
    $hudpay = (int)'hudpay';
    $phone = 'phone';
    $cell = 'cell';
    
    //MySqli Select Query
    $sql = "select * FROM paytbl";
    $result = mysqli_query($conn,$sql);
    if (!$result) {
      printf("Error: %s\n", mysqli_error($conn));
      exit();
    }else{
       $results = array();
       while($row = mysqli_fetch_array($result)){
         $results[] = $row;
       }
    
    $due = $amtdue + $prevbal + $latechg + $secdep + $damage + $courtcost + $nsf - $hudpay;
    
    // if datepaid > duedate (datediff), = latechg
    { $latechg = $latechg + 35.00; }
    
    // if no payment or partial payment, add $35 to latechg field and amount not paid to prevbal field
    if ($amtpaid < $due)
    { $latechg = $latechg + 35.00; $prevbal = $due - $amtpaid;  }
    
    
    // if payment = amtdue clear due
    if ($amtpaid == $due)
    { $prevbal = 0.00; $latechg = 0.00; }
    
    // if over-payment subtract over-payment from prevbal field
    if ($amtpaid > $due)
    { $prevbal = $amtpaid  - $amtdue;  $latechg = 0.00;}  
    
    // refresh every record - give every record the below values
    $amtpaid = 0.00; $secdep = 0.00; $damage = 0.00; $courtcost = 0.00; $nsf = 0.00;
    $hudpay = 0.00; $datepaid = ' '; $comments = ' ';
    
    
    $sql = "UPDATE paytbl SET
    amtpaid = '$amtpaid', datedue = DATE_ADD(datedue, INTERVAL 1 month ),
    secdep = '$secdep', damage = '$damage', courtcost = '$courtcost', nsf = '$nsf',
    hudpay = $hudpay, datepaid = '$datepaid', comments = '$comments' WHERE amtpaid >0"; {
      if(mysqli_query($conn, $sql)){ echo "records were updated successfully."; }
    else { echo "ERROR: unable to execute $sql. " . mysqli_error($conn); }
      }
        }
      ?>
    PHP:
    result:
    Parse error: syntax error, unexpected '$conn' (T_VARIABLE) in C:\xampp\htdocs\property\refresh2.php on line 8
     
    Last edited by a moderator: Jan 6, 2023
    pshaw, Jan 6, 2023 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,816
    Likes Received:
    4,536
    Best Answers:
    123
    Trophy Points:
    665
    #2
    It looks like the connection to the database never actually works. Learn how to debug. Check your database credentials.

    I'd prefer to see this
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    PHP:
    looking like
    $conn = new PDO("mysql:host={$servername};dbname={$dbname}", $username, $password);
    PHP:
    or even
    
    $details = "mysql:host={$servername};dbname={$dbname}";
    $conn = new PDO($details, $username, $password);
    PHP:
    because $details can then be added to the error handling.
     
    sarahk, Jan 6, 2023 IP
    Vooler likes this.