Skip to content
PHP Logo

What are some potential methods for storing search queries from forms in a database for future use in PHP?

Storing search queries from forms in a database for future use in PHP can be achieved by capturing the form input, sanitizing it to prevent SQL injection, and then inserting it into a database table. This allows you to keep a record of user search queries for analysis or future reference.

php
// Assuming you have a form with a search input field named 'search_query'

// Capture the form input
$searchQuery = $_POST['search_query'];

// Sanitize the input to prevent SQL injection
$sanitizedSearchQuery = mysqli_real_escape_string($connection, $searchQuery);

// Insert the search query into a database table
$query = "INSERT INTO search_queries (query) VALUES ('$sanitizedSearchQuery')";
mysqli_query($connection, $query);