Reputation: 87
I am a php newbie and I am trying to get this form to work but I get an "error getting data" when I run my query. Any help on this would be fantastic!
Here is the html form code:
<html>
<head>
<title>Where to go play basketball?</title>
<style type="test/css">
table {
background-color: #FCF;
}
th {
width:150px;
text-align:left;
}
</style>
</head>
<body>
<h1>Venue Search</h1>
<form method="post" action="search.php">
<label> Search Criteria: <input type="text" name="term" /></label>
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
And here is search.php:
<?php
mysql_connect ("localhost", "root","*****", "baskt_venues") or die (mysql_error());
$term = $_POST['term'];
$sql = mysql_query("SELECT * FROM complete_table WHERE country LIKE $term")or die('error getting data');
while ($row = mysql_fetch_array($sql, MYSQLI_ASSOC)){
echo '<br/> Country: '.$row['country'];
echo '<br/> Date: '.$row['date'];
echo '<br/> Time: '.$row['time'];
echo '<br/><br/>';
}
?>
Thanks to everyone take the time to look at this.
Upvotes: 1
Views: 1501
Reputation: 651
You need to add quotes around $term
in the SQL.
$sql = mysql_query("SELECT * FROM complete_table WHERE country LIKE '$term'") ...
You might also want to add % signs around it so the search returns countries containg the search criteria instead of exact matches.
$sql = mysql_query("SELECT * FROM complete_table WHERE country LIKE '%$term%'")
Edit:
Just realized that the fourth paramater to mysql_connect
is not name of the database.
You will need a call to mysql_select_db
after connecting:
mysql_connect("localhost", "root","*****") or die(mysql_error())
mysql_select_db("baskt_venues") or die(mysql_error())
Upvotes: 1
Reputation: 2683
Yes you need to add the % sign and the quotes but you also need to concatenate with the contents of $term so you don't compare with '$term'. So looks like you need something in the lines of :
"SELECT * FROM complete_table WHERE country LIKE '%" + $term + "'"
Upvotes: 0