Are PDO prepared statements sufficient to prevent SQL injection?

The short answer is NO, PDO prepares will not defend you from all possible SQL-Injection attacks. For certain obscure edge-cases. I’m adapting this answer to talk about PDO… The long answer isn’t so easy. It’s based off an attack demonstrated here. The Attack So, let’s start off by showing the attack… $pdo->query(‘SET NAMES gbk’); $var … Read more

Can I bind an array to an IN() condition in a PDO query?

You’ll have to construct the query-string. <?php $ids = array(1, 2, 3, 7, 8, 9); $inQuery = implode(‘,’, array_fill(0, count($ids), ‘?’)); $db = new PDO(…); $stmt = $db->prepare( ‘SELECT * FROM table WHERE id IN(‘ . $inQuery . ‘)’ ); // bindvalue is 1-indexed, so $k+1 foreach ($ids as $k => $id) $stmt->bindValue(($k+1), $id); $stmt->execute(); … Read more

Cleansing User Passwords

You should never escape, trim or use any other cleansing mechanism on passwords you’ll be hashing with PHP’s password_hash() for a number of reasons, the single largest of which is because doing additional cleansing to the password requires unnecessary additional code. You will argue (and you see it in every post where user data is … Read more

Why does this PDO statement silently fail?

TL;DR Always have set PDO::ATTR_ERRMODE to PDO::ERRMODE_EXCEPTION in your PDO connection code. It will let the database tell you what the actual problem is, be it with query, server, database or whatever. Always replace every PHP variable in the SQL query with a question mark, and execute the query using prepared statement. It will help … Read more

Undefined index $_POST

it looks like $row[‘matriculeAgent’] is not set. this could be caused by multiple causes. I noticed several problems in your code, which I try to explain here. first of all, if you want to you prepared statements, dont include your parameters directly inside the stmt->prepare() function call, use bind-param instead: $stmt = $bdd->prepare(“SELECT `matriculeAgent`, `motdepasseAgent` … Read more

Insert form in MySQL with PDO [duplicate]

This is more or less the simplest way to run an update using PDO: // database connection $conn = new PDO(“mysql:host=localhost;dbname=MyDBName”,aDBUser,aDBPassword); // Disable emulated prepared statements // PDO will **TRY** to use real (non-emaulated) prepared statements $conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Some sample data $aTitle=”PHP Security”; $anAuthor=”John Doe”; // Prepare a statement with some placeholders … Read more