简体   繁体   中英

Save error to MySQL database

I have a php query to update a MySQL database, see below

$sql=("update hr_payroll set 
payroll_number='$payroll_number', 
tax_code='$tax',
bacs_ref='$bacs_ref',  
pay_frequency='$pay',
last_update_by='$user'
where employee_id='$employee'")or die('Could not connect: Error 23 ' . mysql_error());

If this query is successful a row is inserted into the DB, see below;

if ($conn->query($sql) === TRUE) {
$sql1 = "INSERT INTO audit_hr_employees
(tab, employee, error_type, user, result)
VALUES ('4a', '$employee', 'info', '$user', 'success')";
}

This all works fine.... However if the query is not successful I want to do the same as above but add the error or the query into the database.

Below is a snippet of my code, I want the information to be added to the error_info column.

else
{       
$sql2 = "INSERT INTO audit_hr_employees 
(tab, employee, error_type, user, error_info)
VALUES ('4a', '$employee', 'warning', '$user', '  ')";
}

Is this possible?

It looks like you're connecting to MySQL via PHP's PDO interface. You can use the errorInfo() function ( http://php.net/manual/en/pdo.errorinfo.php ) to retrieve the most recent error message and use that in place of your blank string:

$err = $dbh->errorInfo();

$sql2 = "INSERT INTO audit_hr_employees 
(tab, employee, error_type, user, error_info)
VALUES ('4a', '$employee', 'warning', '$user', $err[2])";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM