CodeLobster IDE

Free cross-platform PHP IDE (HTML, PHP, CSS, JavaScript, Python code editor) with support Drupal, Joomla, Twig, JQuery, BackboneJS, LaravelJS, Node.js, CodeIgniter, CakePHP, Laravel, Magento, MeteorJS, Phalcon, Symfony, VueJS, WordPress, Yii
It is currently Wed Dec 06, 2023 6:33 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: need help with pdo
PostPosted: Sat Mar 04, 2023 8:53 pm 
Offline

Joined: Tue Jun 16, 2015 1:03 am
Posts: 21
Quote:
Hi, my first attempt at pdo, will someone tell me where I went astray?

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'] . "
";
}

/* 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]);
?>

Quote:
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'] . "
"; }
/* 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]); ?>


Top
 Profile  
 
 Post subject: Re: need help with pdo
PostPosted: Mon Mar 06, 2023 7:16 am 
Offline
Site Admin

Joined: Wed Sep 12, 2007 2:18 pm
Posts: 3921
Hi.

Could You, please, clarify the problem?

We see couple of potential problems:

1) Database connection looks OK

2)
Code:
{ $latechg = $latechg + "35.00"; $prevbal = $amtdue - $amtpaid; }


If they are values from database then You should set in at first:

Code:
$stmt = $pdo->query('SELECT tenant, unit, amtpaid, amtdue, hudpay, prevbal, latechg, datepaid FROM paytbl');
while ($row = $stmt->fetch())
{
    echo $row['tenant'] . "
";
    $latechg = $row['latechg'];
    .....
}


3) Undefined function
Code:
prepared_query($conn, $sql, [$amtpaid, $prevbal, $latechg, $datepaid, $unit]);


Ths syntax is more suitable for "update query"

Code:
$pdo->prepare($sql)->execute([$amtpaid, $prevbal, $latechg, $datepaid, $unit]);


Regards,
Codelobster Team.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 32 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2016 phpBB Group