How can PHP be used to securely insert new member data into a MySQL table from a form input?
To securely insert new member data into a MySQL table from a form input in PHP, you should use prepared statements to prevent SQL injection attacks. Prepared statements separate SQL logic from user input, making it safer to execute queries. By binding parameters to placeholders in the SQL query, you can ensure that user input is treated as data and not executable code.
// Establish a database connection
$pdo = new PDO('mysql:host=localhost;dbname=database', 'username', 'password');
// Prepare a SQL query with placeholders for user input
$stmt = $pdo->prepare("INSERT INTO members (name, email) VALUES (:name, :email)");
// Bind parameters to placeholders
$stmt->bindParam(':name', $_POST['name']);
$stmt->bindParam(':email', $_POST['email']);
// Execute the query
$stmt->execute();
Related questions
-
UTF-16 encoding PHP
-
mathematical calculations accuracy efficiency
-
PHP email notifications form submission