I was just wondering If anyone knew any good ways to prevent Sql Injections on this code. The Last thing I want is someone hacking my database. I am fairly new to this and would like to hear expert opinions. Thanks.
<?php
$input = $_GET['input'];//Note to self $input in the name of the search feild
$terms = explode(" ", $input);
$query = "SELECT * FROM content WHERE ";
foreach ($terms as $each){
$i++;
if ($i == 1)
$query .= "keywords LIKE '%$each%' ";
else
$query .= "OR keywords LIKE '%$each%' ";
}
// connecting to our mysql database
mysql_connect("localhost", "username", "password");
mysql_select_db("database");
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0){
$i = 0;
while ($row = mysql_fetch_assoc($query)){
$i++;
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
$plink = $row ['plink'];
$views = $row ['views'];
if($i == 3){
echo '<td valign="top" "width="248" height="100%">
<table width="100%" border="0">
<tr>
<td align="center" valign="top"><a href='.$link.'>
<img src='.$plink.'width="200" height="151" vspace="5" />
<br><b><a href='.$link.'>'.$title.'</b></a>
<br><strong><span style="line-height:20px">Total views: '.$views.'</span></strong>
</td>
</tr>
</table>
</td><tr>';
}
else{
echo '<td valign="top" "width="248" height="100%">
<table width="100%" border="0">
<tr>
<td align="center" valign="top"><a href='.$link.'>
<img src='.$plink.'width="200" height="151" vspace="5" />
<br><b><a href='.$link.'>'.$title.'</b></a>
<br><strong><span style="line-height:20px">Total views: '.$views.'</span></strong>
</td>
</tr>
</table>'
;
}
}
}
else
echo "No results found for \"<b>$input</b>\"";
// disconnect
mysql_close();
?>