I’ve just created a very simple search function in PHP, but am having a few issues. The search function sends a query to the database, and prints results, but it doesn’t print the results I want. It’s supposed to print those records that are like the user input, but it just prints every record.
My Form is as follows:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="name">
<input type="submit" name="submit" value="Search">
</form>
And the PHP code to make it search is:
if(isset($_POST['submit']))
{
require ('mysqli_connect.php');
include ('config.php');
$term = $_POST['term'];
$q = "SELECT name, producer, jamtypes, user FROM Jam WHERE name LIKE '%" . $term . "%'";
$sql = @mysqli_query ($dbc, $q);
while ($row = mysqli_fetch_array($sql)){
echo "<br />";
echo 'Name: '.$row['name'];
echo '<br/> Producer: '.$row['producer'];
echo '<br/> Created By: '.$row['user'];
echo '<br/> Category: '.$row['jamtypes'];
echo '<br/>';
}
}
Thanks guys
Your form field is
name, notterm, so your PHP should beBecause
$termis blank due to having an incorrect value your query returns all results. (Your query looks likeSELECT name, producer, jamtypes, user FROM Jam WHERE name LIKE '%&')FYI, having error_reporting turned on and reporting notices would have told you this. Always develop with error reporting on and displaying all errors.