I am new to SQL injection and would like to know if I am using the mysql_real_escape_string properly? Should I make strings for the database and password to make this secure? Any advice would be great and highly appreciated!
<html>
<body>
<img src="searchlogo.png" style="PADDING-TOP: 50px">
<form action='./search.php' method='get' >
<label>Search Our Archives</label>
<input type="text" name="k" size="50" value='<?php echo $_GET['k'];?>'/ >
<button type="submit">Search</button>
</form>
<hr/>
<?php
$k = $_GET['k'];
$terms = explode(" ", $k);
$query ="SELECT * FROM search WHERE ";
foreach ($terms as $each){
$i++;
if ($i == 1)
$query .= "keywords LIKE'%$each%'";
else
$query .= "OR keywords LIKE'%$each%'";
}
$db = mysql_connect("**host***","***database***","***password***",
mysql_real_escape_string($user),
mysql_real_escape_string($password));
if (!$db) {
die("Database connection failed: " . mysql_error());
}
$db_select = mysql_select_db("***database***",$db);
if (!$db_select) {
die("Database selection also failed: " . mysql_error());
}
$result = mysql_query("SELECT * FROM search", $db);
if (!$result) {
die("Database query failed: " . mysql_error());
}
while ($row = mysql_fetch_assoc($result)){
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
echo "<h2><a href='$link'>$title</a></h2>$description<br /><br />";
}
mysql_close($db);
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows >0){
}
else
echo "No results found for \"<b>$k</b>\"";
?>
</body>
</html>