What are the best practices for storing survey data in a SQL database using PHP?
When storing survey data in a SQL database using PHP, it is important to properly sanitize the data to prevent SQL injection attacks. Additionally, it is recommended to use prepared statements to securely insert the data into the database. Lastly, consider using a separate table for each survey question to keep the database organized.
// Assuming $conn is the database connection object
// Sanitize the survey data
$surveyData = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
// Prepare the SQL statement
$stmt = $conn->prepare("INSERT INTO survey_responses (question_id, response) VALUES (?, ?)");
// Bind parameters and execute the statement for each question
foreach ($surveyData as $questionId => $response) {
$stmt->bind_param("is", $questionId, $response);
$stmt->execute();
}
// Close the statement and connection
$stmt->close();
$conn->close();
Related questions
-
SSL SSH secure file transfer
-
PHP headers header function
-
ceil function multiple