How can the SQL query in the PHP code be modified to search for a specific name entered in an input field?
To search for a specific name entered in an input field, you can modify the SQL query in the PHP code to use a parameterized query with a placeholder for the name input. This allows you to dynamically insert the entered name into the query before execution, preventing SQL injection attacks and ensuring accurate search results.
<?php
// Assuming the name input is submitted via POST method
if(isset($_POST['search_name'])) {
$name = $_POST['search_name'];
// Establish a database connection
$conn = new PDO("mysql:host=localhost;dbname=mydatabase", "username", "password");
// Prepare a parameterized SQL query to search for the entered name
$stmt = $conn->prepare("SELECT * FROM users WHERE name = :name");
$stmt->bindParam(':name', $name);
$stmt->execute();
// Fetch and display the search results
while($row = $stmt->fetch()) {
echo $row['name'] . "<br>";
}
}
?>