I have the following code in a PHP which works, but I suspect it susceptible to SQL injection. Am I right? The query is to a Interbase (firebird) 2.1 database, not MySQL.
$employeename = 'ALLSOPPC';
$query = "SELECT DATEWORKED, ORDERTYPE, REFERENCENUMBER, REFERENCEDESCRIPTION, TASKORSTEPNAME, HOURSWORKED, RECORDEDNOTES, ADDITIONALFIELD_1 as EU_REFERENCENUMBER, ADDITIONALFIELD_2 AS EU_TASKORSTEP, ADDITIONALFIELD_3 AS RDACTIVITY";
$query .= " FROM TIMESHEETLINES ";
$query .= " WHERE TIMESHEETBATCHNO=$timesheetBatchNo ";
I believe instead of including $employeename in the SQL string, I need to PARSE the variable into the query.
I understand I should be able to do this via:
ibase_bind_param($query , 1, $employeename);
or
ibase_execute($query, $employeename);
or
$stmt->bindValue(':empname', $employeename);
but none of these are working right now.
2
Answers
Yes, you’re correct. The code you provided is susceptible to SQL injection because it directly includes the $timesheetBatchNo variable in the SQL string without proper sanitization or parameter binding.
You should use parameterized queries or prepared statements.
As luukd has already confirmed, your current code is vulnerable to SQL Injection.
Most of the ibase_* functions return false on failure, so you can check and return the
ibase_errormsg()
. This is a crude example, and you should not blindly return error messages in production as they can give hints as to possible attack vectors:You may want to try the Firebird PDO driver but I do not know whether it will work with your old version.