Asked
Updated
Viewed
4.4k times

I've implemented a PHP script to handle form submissions and insert data into a PostgreSQL database. However, I'm concerned about the potential security vulnerabilities, particularly SQL injection. While I've used pg_escape_string() to sanitize user input, I understand that it might not provide sufficient protection against all SQL injection attacks.

What are the best practices for enhancing security against SQL injection when using PHP with PostgreSQL? Should I switch to using parameterized queries (prepared statements) with PDO or mysqli extension instead of pg_escape_string()? If so, could someone provide guidance or code examples on how to implement parameterized queries securely in my PHP code?

add a comment
0

3 Answers

  • Votes
  • Oldest
  • Latest
Answered
Updated

Definitely use PDO and prepared statements. This will ensure that the SQL query and its parameters are separated, making it impossible for an attacker to inject malicious SQL code.

Assuming you have already setup $pdo to be a PDO connection to your PostgreSQL database, and assuming we are trying to insert a username and email into a users table, then it would simply look like this:

$username = 'Cool User';
$email = 'cool-user@domain.com';

// Prepare an SQL statement with placeholders
$stmt = $pdo->prepare("INSERT INTO users (username, email) VALUES (:username, :email)");

// Bind the actual values to the placeholders
$stmt->bindParam(':username', $username, PDO::PARAM_STR);
$stmt->bindParam(':email', $email, PDO::PARAM_STR);

// Execute the statement
$stmt->execute();
add a comment
0
Answered
Updated

Hey Raivivan,

You're right to be cautious — pg_escape_string() is better than nothing, but it doesn’t offer the same level of protection as parameterized queries. I've run into similar concerns before, and switching to prepared statements with PDO made a big difference in both security and code clarity.

One small tip: if you're handling multiple inserts or different types of user inputs, using named parameters (like Brian showed) keeps things easier to manage and reduces the chance of mistakes. Also worth double, checking that error reporting is set up properly so you catch issues during development.

add a comment
0
Answered
Updated

You're right to question pg_escape_string() —it's not enough on its own to prevent SQL injection. The best practice is to use prepared statements with bound parameters.

With PostgreSQL, you can either use pg_prepare() or pg_execute() or switch to PDO for cleaner syntax and better portability. Here's a quick PDO example:

$pdo = new PDO('pgsql:host=localhost;dbname=testdb', 'user', 'pass');
$stmt = $pdo->prepare('INSERT INTO users (name, email) VALUES (:name, :email)');
$stmt->execute([
    ':name' => $_POST['name'],
    ':email' => $_POST['email']],
);

Prepared statements handle escaping safely and reduce the risk of injection.

add a comment
0

User

Community

Market

Help Center

Legal

Company

Social Media