I have the following code -
<?php # search.inc
error_reporting(E_ALL); //
ini_set('display_errors', '1');
include "connect_to_mysql.php";
?>
<?php # search.inc.php
//header file:
include_once ('./includes/header.inc');
?>
<?php
$terms= $_GET['terms'];
echo "You've searched for: " . $terms;
$sql= "SELECT * FROM products WHERE product_name ='Big Tree'";
//-run the query against the mysql query function
$result=mysql_query($sql);
while ($row = mysql_fetch_array($result)) { // While there are products that match the search criteria, assign their data to a variable and echo their details on a webpage
$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$details = $row["details"];
$category = $row["category"];
echo '<br/> <img name="image" src="inventory_images/' . $row['id'] . '.jpg" width="100" height="150"/>';
echo '<br/>';
echo '<br/> Product Name: '.$row['product_name'];
echo '<br/>';
echo '<br/> <a href="modules/productsdetails.inc.php?id=' . $id . '">Click Here To View Product Page</a>';
echo '<br/>';
echo '<br/>';
}
mysql_close(); //closes the MySQL connection.
?>
<?php
// Include footer file
include_once ('./includes/footer.inc');
?>
As you can see - I've hard coded it to search for "Big Tree". My question is - How do I code it to search for the users input.
I've tried %$terms% but no results are printed.
%$terms%, right? Surely you wouldn't just write the wordtermsin a string and expect it to know you wanted to inject a variable... Assuming you did that, you need to use theLIKEoperator instead of=. Then you need to worry about SQL injection. Then you need to consider that you might need to combine multiple terms withOR.