hoachen Posted November 9, 2006 Share Posted November 9, 2006 I am doing a dataabse search on my website, but it seems like the code that i have is not practical. Would someone give me some advice. My goal is when the user type in anything(at least two alpha because they search my state like "MO") and search on the database for every columns that match what ever they want to search especially the city and state the user put in. My question is how to the query know which column to look for if they type in city or state? Are they anyway to do this? <form action="result.php" method="post"> Enter Search:<br> <input name="city" type=text> <br> <input type=submit value="Search"> </form><? $city =$_POST['city'];echo 'this is city'.$city."<br />"; $query="select * from tblinfo where city like \"city\" OR state LIKE \"city\" OR Comments LIKE \"city\" "; $result = mysql_query($query);echo $query."<br /><br />";while ($row = mysql_fetch_assoc($result)) { echo "<b>"."City: "."</b>".stripslashes($row["city"]); echo " "; echo "<b>"."State: "."</b>". stripslashes($row["state"]); echo "<br />"; echo "<b>"."Complaints: "."</b>". "<i>". stripslashes($row["complaints"])."</i>"; echo "<br /><br /><br />"; }?> Link to comment Share on other sites More sharing options...
justsomeguy Posted November 10, 2006 Share Posted November 10, 2006 Your query is a little off: $city = mysql_real_escape_string($city);$query="select * from tblinfo where city like '%$city%' OR state LIKE '%$city%' OR Comments LIKE '%$city%'"; If you want to check more then one field, then you need to use some logic statements to figure out which one to use. if ($_POST['city'] != ""){ $city = mysql_real_escape_string($_POST['city']); $query="select * from tblinfo where city like '%$city%'";}elseif ($_POST['state'] != ""){ $state = mysql_real_escape_string($_POST['state']); $query="select * from tblinfo where State like '%$state%'";} Link to comment Share on other sites More sharing options...
hoachen Posted November 10, 2006 Author Share Posted November 10, 2006 my problem is i only have one textfield how can i use $_post['state']? for second field. Your query is a little off:$city = mysql_real_escape_string($city);$query="select * from tblinfo where city like '%$city%' OR state LIKE '%$city%' OR Comments LIKE '%$city%'"; If you want to check more then one field, then you need to use some logic statements to figure out which one to use. if ($_POST['city'] != ""){ $city = mysql_real_escape_string($_POST['city']); $query="select * from tblinfo where city like '%$city%'";}elseif ($_POST['state'] != ""){ $state = mysql_real_escape_string($_POST['state']); $query="select * from tblinfo where State like '%$state%'";} Link to comment Share on other sites More sharing options...
justsomeguy Posted November 10, 2006 Share Posted November 10, 2006 You can't. If you don't have any way for a user to tell you where they want to search, then use the first code I posted. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now