need help with pdo

Discussion in 'PHP' started by pshaw, Mar 4, 2023.

  1. #1
    Hi, my first attempt at pdo, will someone tell me where I went astray?
    --------------------------------------------------------------------------
    the code:
    <?php
    $host = '127.0.0.1';
    $db  = 'homedb';
    $user = 'root';
    $pass = '';
    $charset = 'utf8mb4';
    
    $dsn = "mysql:host=$host;dbname=$db;charset=$charset";
    $options = [
      PDO::ATTR_ERRMODE  => PDO::ERRMODE_EXCEPTION,
      PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
      PDO::ATTR_EMULATE_PREPARES  => false,
    ];
    try {
      $pdo = new PDO($dsn, $user, $pass, $options);
    } catch (\PDOException $e) {
      throw new \PDOException($e->getMessage(), (int)$e->getCode());
    }
    echo "<center>";echo date('m/d/y');echo "</center>";
    
    $tenant=$_POST['tenant'];
    $unit=$_POST['unit'];
    $amtpaid=$_POST['amtpaid'];
    $hudpay=$_POST['hudpay'];
    $datepaid=$_POST['datepaid'];
    
    $amtpaid='amtpaid';
    $amtdue='amtdue';
    $hudpay='hudpay';
    $prevbal='prevbal';
    $latechg='latechg';
    $datepaid='datepaid';
    
    $stmt = $pdo->query('SELECT tenant, unit, amtpaid, amtdue, hudpay, prevbal, latechg, datepaid FROM paytbl');
    while ($row = $stmt->fetch())
    {
      echo $row['tenant'] . "\n";
    }
    
    /* if no pay or part pay, add $35 to latechg field and amount not paid to prevbal field */
    if ($amtpaid < $amtdue)
    { $latechg = $latechg + "35.00"; $prevbal = $amtdue - $amtpaid; }
    
    /* if payment = amtdue  */
    elseif ($amtpaid == $amtdue)
    { $prevbal = $prevbal - $prevbal;
    $latechg = $latechg  - $latechg; }
    
    /* *****************************unexpected ';'*******************************
    // if over-payment subtract over-payment from prevbal
    else ($amtpaid > $amtdue )
    { $prevbal = $amtpaid  - $amtdue;
    $latechg = $latechg  - $latechg; }
    ****************************************************************************** */
    /* Perform a query, check for error */
    $sql = "UPDATE paytbl SET amtpaid=?, prevbal=?, latechg=?, datepaid=? WHERE unit=?";
    prepared_query($conn, $sql, [$amtpaid, $prevbal, $latechg, $datepaid, $unit]);
    // $pdo->prepare($sql)->execute([$name, $id]);
    ?>
    PHP:
    -------------------------------------------------------------------------
    the result:
    PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES =>
    false, ]; try { $pdo = new PDO($dsn, $user, $pass, $options); } catch (\PDOException $e)
    { throw new \PDOException($e->getMessage(), (int)$e->getCode()); } echo "
    ";echo date('m/d/y');echo "
    "; $tenant=$_POST['tenant']; $unit=$_POST['unit']; $amtpaid=$_POST['amtpaid'];
    $hudpay=$_POST['hudpay']; $datepaid=$_POST['datepaid']; $amtpaid='amtpaid'; $amtdue='amtdue';
    $hudpay='hudpay'; $prevbal='prevbal'; $latechg='latechg'; $datepaid='datepaid';
    $stmt = $pdo->query('SELECT tenant, unit, amtpaid, amtdue, hudpay, prevbal, latechg, datepaid FROM
    paytbl'); while ($row = $stmt->fetch()) { echo $row['tenant'] . "\n"; }
    /* if no pay or part pay, add $35 to latechg field and amount not paid to prevbal field */
    if ($amtpaid < $amtdue) { $latechg = $latechg + "35.00"; $prevbal = $amtdue - $amtpaid; }
    /* if payment = amtdue */
    elseif ($amtpaid == $amtdue) { $prevbal = $prevbal - $prevbal; $latechg = $latechg - $latechg; }
    /* *****************************unexpected ';'*******************************
    // if over-payment subtract over-payment from prevbal else ($amtpaid > $amtdue )
    { $prevbal = $amtpaid - $amtdue; $latechg = $latechg - $latechg; }
    ****************************************************************************** */
    /* Perform a query, check for error */
    $sql = "UPDATE paytbl SET amtpaid=?, prevbal=?, latechg=?, datepaid=? WHERE unit=?";
    prepared_query($conn, $sql, [$amtpaid, $prevbal, $latechg, $datepaid, $unit]);
    // $pdo->prepare($sql)->execute([$name, $id]); ?>
    Code (markup):

     
    Solved! View solution.
    Last edited by a moderator: Mar 4, 2023
    pshaw, Mar 4, 2023 IP
  2. #2
    There are several errors in the code according to AI:

    1. The line $amtpaid='amtpaid'; and $datepaid='datepaid'; are reassigning the values of variables that were already assigned from the form inputs.

    2. The comment block has an unexpected semicolon in the else statement.

    3. The function prepared_query is not defined, so it will result in a fatal error.
    Here's the corrected code:
    <?php
    $host = '127.0.0.1';
    $db = 'homedb';
    $user = 'root';
    $pass = '';
    $charset = 'utf8mb4';

    $dsn = "mysql:host=$host;dbname=$db;charset=$charset";
    $options = [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES => false,
    ];
    try {
    $pdo = new PDO($dsn, $user, $pass, $options);
    } catch (\PDOException $e) {
    throw new \PDOException($e->getMessage(), (int)$e->getCode());
    }
    echo "<center>" . date('m/d/y') . "</center>";

    $tenant=$_POST['tenant'];
    $unit=$_POST['unit'];
    $amtpaid=$_POST['amtpaid'];
    $hudpay=$_POST['hudpay'];
    $datepaid=$_POST['datepaid'];

    /* if no pay or part pay, add $35 to latechg field and amount not paid to prevbal field */
    if ($amtpaid < $amtdue) {
    $amtdue = 0; // $amtdue is not defined in the code
    $latechg = $latechg + 35.00;
    $prevbal = $amtdue - $amtpaid;
    }
    /* if payment = amtdue */
    elseif ($amtpaid == $amtdue) {
    $prevbal = 0;
    $latechg = 0;
    }
    /* if over-payment subtract over-payment from prevbal */
    else { // removed semicolon from else statement
    $prevbal = $amtpaid - $amtdue;
    $latechg = 0;
    }

    /* Perform a query, check for error */
    $sql = "UPDATE paytbl SET amtpaid=?, prevbal=?, latechg=?, datepaid=? WHERE unit=?";
    $pdo->prepare($sql)->execute([$amtpaid, $prevbal, $latechg, $datepaid, $unit]);
    ?>
     
    kayceehr, Mar 4, 2023 IP
  3. pshaw

    pshaw Greenhorn

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    thanks for the help,
    this is the result:
    PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false, ]; try { $pdo = new PDO($dsn, $user, $pass, $options); } catch (\PDOException $e) { throw new \PDOException($e->getMessage(), (int)$e->getCode()); } echo "" . date('m/d/y') . ""; $tenant=$_POST['tenant']; $unit=$_POST['unit']; $amtpaid=$_POST['amtpaid']; $hudpay=$_POST['hudpay']; $datepaid=$_POST['datepaid']; /* if no pay or part pay, add $35 to latechg field and amount not paid to prevbal field */ if ($amtpaid < $amtdue) { $amtdue = 0; // $amtdue is not defined in the code $latechg = $latechg + 35.00; $prevbal = $amtdue - $amtpaid; } /* if payment = amtdue */ elseif ($amtpaid == $amtdue) { $prevbal = 0; $latechg = 0; } /* if over-payment subtract over-payment from prevbal */ else { // removed semicolon from else statement $prevbal = $amtpaid - $amtdue; $latechg = 0; } /* Perform a query, check for error */ $sql = "UPDATE paytbl SET amtpaid=?, prevbal=?, latechg=?, datepaid=? WHERE unit=?"; $pdo->prepare($sql)->execute([$amtpaid, $prevbal, $latechg, $datepaid, $unit]); ?>
     
    pshaw, Mar 4, 2023 IP