|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Custom WHERE and LIKE tags
I am trying to create the search feature where the user can type in a number into a text box. It will then search the database row for that number, then list certain information for the resort. This is for a vacation website, where the number is the Ad number and the search results page will display information like location, resort name, etc.
I have been working with a few books I purchased which has helped me get the database created and am able to enter information. However here is the example from one of my books, which I cannot get to work or even display anything from the database. Search Page <form action="results.php" method="post"> Choose Search Type:<br /> <select name="searchtype"> <option value="ad_number">Ad Number</option> <option value="resort_location">State</option> <option value="bed">Bedrooms</option> </select> <br /> Enter Search Term:<br /> <input name="searchterm" type="text"> <br /> <input type="submit" value="Search"> </form> Results.php <?php // create short variable names $searchtype=$_POST['searchtype']; $searchterm=$_POST['searchterm']; $searchterm= trim($searchterm); if (!$searchtype || !$searchterm) { echo 'You have not entered search details. Please go back and try again.'; exit; } if (!get_magic_quotes_gpc()) { $searchtype = addslashes($searchtype); $searchterm = addslashes($searchterm); } @ $db = new mysqli('localhost', 'user', 'password', 'dbname'); if (mysqli_connect_errno()) { echo 'Error: Could not connect to database. Please try again later.'; exit; } $query = "select ad_number, resort_location, resort_name, bed, bath, sales_price from ads where ".$searchtype." like '%".$searchterm."%'"; $result = $db->query($query); $num_results = $result->num_rows; echo '<p>Number of Resorts found: '.$num_results.'</p>'; for ($i=0; $i <$num_results; $i++) { $row = $result->fetch_assoc(); echo '<p><strong>'.($i+1).'. Ad Number: '; echo htmlspecialchars(stripslashes($row['ad_number'])); echo '</strong><br />Resort Location: '; echo stripslashes($row['resort_location']); echo '<br />Resort Name: '; echo stripslashes($row['resort_name']); echo '<br />Price: '; echo stripslashes($row['sales_price']); echo '</p>'; } $result->free(); $db->close(); ?> As a test I even put in the wrong database login information, and I do not even get any error message. I would think that the examples from a book would at least work. |
|
#2
|
|||
|
|||
|
Ah, another bookworm ;-)
This may sound stupid, but where are the HTML tags? I would think you require those simply because you have HTML tags like the form tag and all. |
|
#3
|
|||
|
|||
|
Quote:
Well when it comes to learning a new proraming language, I prefer a book or two. Anyhow, the HTML tags are done, and I figured they were not needed to be shown here because they work and would take up to much of the article. It was the php that was giving me the problem, not the html. |
|
#4
|
||||
|
||||
|
In the future, or even now (edit your post), use surround your code and/or PHP markup with [code] and/or [php]... it will colourize the php code, and both will section the code off with scroll bars so it doesn't take up too much forum real estate =)
Also, I'd advise against using actual column names in your <select> box (as it appears to me that you've done)... many would consider this a security flaw... Are you using Pear's DB class? or something odd? I ask because some of your code deviates from what I usually see from new members =) This line here seems odd: @ $db = new mysqli('localhost', 'user', 'password', 'dbname'); perhaps add or die('Error connecting to database :: '.mysql_error()); to the end of that line... that mysql_error() might cause an error itself... look into the DB class that you're using... |
|
#5
|
|||
|
|||
|
Quote:
I didn't mean it like that, I am just glad I am not the only one. :-) |
|
#6
|
|||
|
|||
|
Quote:
he's using php5 which I am also having a problem with. I am updating all of my scripts to the php5 and I am also having a problem with the code. I know it connects to the database but for some reason it doesn't like the query with the select statement using the where clause. The query will not return any results. The query will return results if the where clause isn't there but that would be pointless. So if anyone else has ran into the same problem but figured out the solution please let us know. Thanks |
|
#7
|
|||
|
|||
|
PHP Code:
try this code instead and see if it works for you PHP Code:
just copy and paste that over the code in the code block and try again.. let me know what happens |
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > Custom WHERE and LIKE tags |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|