0

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?

18
  • 1
    This is not a duplicate of that question? Commented May 10, 2016 at 13:41
  • It's very likely that it is a duplicate. When you execute your query, does the database return an error? What is that error? What even is the query that you're executing? (Hint: This looks wide open to SQL injection, so you might be executing anything.) Note also that you're not even checking what error was generated by the first query. Why not? Recording that there is an error is great and all, but recording what that error was seems like it would also be pretty useful. Commented May 10, 2016 at 13:47
  • Seems to be a different question. Commented May 10, 2016 at 13:50
  • 1
    @user3092953: It looks like what you're trying to achieve is to store a query which failed. Which isn't itself a bad idea, if I'm being honest. However, you need to also check why the query failed. And if this query which is storing the failed query is also failing, well, that sounds like a potentially infinite rabbit hole... Commented May 10, 2016 at 14:01
  • 1
    If you are not absolutely sure that the database failure is not due to the database crashing, all of a sudden its not there any more, you cannot sensibly attempt to write to it again. Commented May 10, 2016 at 19:10

1 Answer 1

1

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])";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.