Skip to content
PHP Logo

What are the potential issues with using addslashes() in PHP for database input?

Using addslashes() for database input in PHP can potentially lead to SQL injection vulnerabilities if not used correctly. It is recommended to use prepared statements with parameterized queries instead to prevent SQL injection attacks. This method separates the SQL query from the user input, making it much safer and more reliable.

php
// Using prepared statements with parameterized queries to prevent SQL injection
$stmt = $pdo->prepare("INSERT INTO table_name (column1, column2) VALUES (:value1, :value2)");
$stmt->bindParam(':value1', $value1);
$stmt->bindParam(':value2', $value2);

// Set parameters and execute the query
$value1 = $_POST['input1'];
$value2 = $_POST['input2'];
$stmt->execute();