Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Generic User Avatar

need help with pdo


  • Please log in to reply
1 reply to this topic

#1 guitarzRwe

guitarzRwe

  •  Avatar image
  • Members
  • 6 posts
  • OFFLINE
  •  
  • Local time:10:57 PM

Posted 04 March 2023 - 03:39 PM

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]);
?>
-------------------------------------------------------------------------
the resut:
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]); ?>



BC AdBot (Login to Remove)

 


#2 axe0

axe0

  •  Avatar image
  • Malware Response Team
  • 1,948 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Netherlands
  • Local time:04:57 AM

Posted 12 March 2023 - 01:10 PM

Looks like you're reusing $amtpaid and comparing a string with a POST value that presumably is an int or double as a result of reusing $amtpaid. You're reusing a few other variables as well. Reusing the same variable is not a good idea unless you're not going to use the previous data the variable contains afterwards.
Kind regards,
Axe0




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users