check pdo php insert success

Martin
$hpaystmt = $con->_con->prepare(".....");

object(PDOStatement)#7 (1) { ["queryString"]=> string(1515) " INSERT INTO hpay ( enccode, orno, hpercode, acctno, amt, curcode, paytype, paycode, entryby, payctr, chrgcode, itemcode, chrgtbl) VALUES ( '000060000000000101783710/14/201722:00:00', '00000000033', '000000001017837', '2017-000165903', '1500', 'PESO', 'F', 'C', '', '1', 'DR', 'DR1', 'MISC') "}====object(PDOStatement)#8 (1) { ["queryString"]=> string(1517) " INSERT INTO hpay ( enccode, orno, hpercode, acctno, amt, curcode, paytype, paycode, entryby, payctr, chrgcode, itemcode, chrgtbl) VALUES ( '000060000000000101783710/14/201722:00:00', '00000000033', '000000001017837', '2017-000165903', '100', 'PESO', 'F', 'C', '', '2', 'BIRTC', '085', 'MISC') "}

I get the above code when from

ini_set('display_errors', 1);
var_dump($hpaystmt);
$hpayinsert = $hpaystmt->execute();
echo "==";
echo $hpaystmt->execute() === TRUE ;
echo "==";

My aim is to check if the insert is success or not but echo $hpaystmt->execute() === TRUE ; this line always return blank that is why i alwats get }==== in echo;

I dont get any error from display error and also I wrap this in try catch and I always go in try part not catch.

How can I check this for success insert?

OTHER INFO:

I want to check if success so that if it fails I want to rollback the database my var $hpayinsert is set to $hpayinsert = true; before the inserting. then after i check for true or false then commit or rollback depending on the value of that var.

UPDATE:

after putting print_r($hpaystmt->errorInfo()); I get:

Array( [0] => 22001 [1] => 8152 [2] => [Microsoft][ODBC SQL Server Driver][SQL Server]String or binary data would be truncated. (SQLExecute[8152] at ext\pdo_odbc\odbc_stmt.c:254) [3] => 22001)Array( [0] => 22001 [1] => 8152 [2] => [Microsoft][ODBC SQL Server Driver][SQL Server]String or binary data would be truncated. (SQLExecute[8152] at ext\pdo_odbc\odbc_stmt.c:254) [3] => 22001)

Your Common Sense

You don't need it.

There are three possible scenarios for handling the result of insert operation in PDO:

  1. To tell the success, no verification is needed. Just keep with your program flow.
  2. To handle an unexpected error, keep with the same - no immediate handling code is needed. An exception will be thrown in case of a database error, and it will bubble up to the site-wide error handler that eventually will result in a common 500 error page.
  3. To handle an expected error, like a duplicate primary key, and if you have a certain scenario to handle this very error - then use a try..catch operator.

For a regular PHP user it sounds a bit alien - how's that, not to verify the direct result of the operation? - but this is how exceptions work - you check the error somewhere else. Once for all. Extremely convenient.

So, in a generic case you don't need any handling code at all. Just keep your code as is.

I wrote an article on PHP error reporting basics explaining the matter in detail, you may find it useful

Only in case you have a handling scenario other than just reporting the error, you could catch an error. To rollback a transaction for example. The code is taken from my PDO tutorial:

try {
    $pdo->beginTransaction();
    $stmt = $pdo->prepare("INSERT INTO users (name) VALUES (?)");
    foreach (['Joe','Ben'] as $name)
    {
        $stmt->execute([$name]);
    }
    $pdo->commit();
}catch (Exception $e){
    $pdo->rollback();
    throw $e;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to show success with multi insert inside function PDO PHP without javascript for processing

From Dev

pdo, insert php object to database

From Dev

Insert PHP PDO via AJAX

From Dev

PHP PDO Multiple image INSERT

From Dev

Insert PHP PDO via AJAX

From Dev

How to check insert success or not When i using this code?

From Dev

Insert record PHP/PDO throwing error

From Dev

PHP PDO Insert Only Inserting First Character

From Dev

PHP,PDO,MySQLi function dont insert to database

From Dev

PDO Insert operation using PHP and MYSQL not working

From Dev

PHP; PDO; MySQL; INSERT query not taking input

From Dev

PHP; PDO; MySQL INSERT query with drop down

From Dev

php pdo insert datas using function

From Dev

PHP PDO insert statement not going through

From Dev

PHP PDO: Insert working without attributes

From Dev

Insert multiple rows using PHP PDO

From Dev

PHP, PDO, MySQL - Multiple INSERT vulnerable to injection?

From Dev

php pdo dynamic html form insert array

From Dev

PHP PDO Insert Statement exceeding execution time

From Dev

PHP PDO insert not returning expect from operation

From Dev

PHP PDO - INSERT INTO with bindParam does not work

From Dev

Indexing array in php pdo insert while loop

From Dev

PDO::commit() success or failure

From Dev

PDO Success message not firing

From Dev

PDO::commit() success or failure

From Dev

PDO Success message not firing

From Dev

How to check if rows exist PDO PHP?

From Dev

How to get the insert ID after insert using the PDO transactino in php

From Dev

Why is unordered_map "find + insert" faster than "insert + check for success"?