I have some code on my website that checks if an input value of an input field exists in a database and then outputs the results as simple as possible.
Unfortunately it does not give me "No results found" if a wrong value is sent. 1 - How can I fix this? There's no error message or anything else, just a white page.
2 - Another question: is it possible for the public to get my database login credentials using this type of checkup of an input field with a separated search.php with the login credentials placed on top of the file?
3 - How can I secure this checkup with my php code against any main attacks? I think SQL injection is already prevented.
Thanks
index.php:
<form action="search.php" method="Post">
<input type="text" name="query" />
<input type="submit" value="Search" />
</form>
search.php:
<?php
$mydatabase=mysqli_connect("localhost", "root", "") or die("Error connecting to database: ".mysql_error());
mysqli_select_db($mydatabase, "mydb") or die(mysql_error());
$query = $_POST['query'];
$min_length = 1;
if(strlen($query) >= $min_length){
$query = htmlspecialchars($query);
$query = mysqli_real_escape_string($mydatabase, $query);
$raw_results = mysqli_query($mydatabase, "SELECT * FROM mytable WHERE (`title` LIKE '%".$query."%')") or die("mysql_error()");
while($results = mysqli_fetch_array($raw_results)){
if (empty($results)) {
echo 'No results found';
} else {
echo "<p><h3>".$results['title']."</h3>".$results['name']."</p>";
}
}
} else{
echo "Minimum length is ".$min_length;
}
?>