BTW8892 Posted May 12, 2014 Share Posted May 12, 2014 Hey guys, So I've been devloping a TV Guide website that will allow you to fill out a variety of different fields, then searches the database for that information, and returns the results. The way I have it right now, the user has to fill out all of the fields in order for the search to work properly. I want to have it so even if the user only enters information for one field, it will ignore all of the empty fields, and still search the database. Here is the code for the form: And the results page: <form method="POST" action="guideresult.php"> <table cellspacing="7" cellpadding="0" bgcolor="#ecedef" align="center"> <tr> <td>DATE</td> <td>TIME</td> <td>TYPE</td> <td>GENRE</td> <td>RATING</td> <td>PROGRAM NAME SEARCH</td> <td>Submit</td> </tr> <tr> <td> <input type="date" name="date"> </td> <td> <input type="time" name="startime"> </td> <td> <select name="type"> <option value="NULL"></option> <option value="TV Show">TV Show</option> <option value="Movie">Movie</option> </select> </td> <td> <select name="genre"> <option value="NULL"></option> <option value="Action">Action</option> <option value="Animated">Animated</option> <option value="Comedy">Comedy</option> <option value="Crime">Crime</option> <option value="Drama">Drama</option> <option value="Entertainment">Entertainment</option> <option value="Family">Family</option> <option value="Fantasy">Fantasy</option> <option value="Horror">Horror</option> <option value="Musical">Musical</option> <option value="Reality">Reality</option> <option value="Romance">Romance</option> <option value="ScFi">ScFi</option> <option value="Sports">Sports</option> <option value="Talk Show">Talk Show</option> </select> </td> <td> <select name="rating" class="rating"> <option value="NULL"></option> <option class="tvshow" value="TV-Y">TV-Y</option> <option class="tvshow" value="TV-Y7">TV-Y7</option> <option class="tvshow" value="TV-G">TV-G</option> <option class="tvshow" value="TV-PG">TV-PG</option> <option class="tvshow" value="TV-14">TV-14</option> <option class="tvshow" value="TV-MA">TV-MA</option> <option class="movie" value="G">G</option> <option class="movie" value="PG">PG</option> <option class="movie" value="PG-13">PG-13</option> <option class="movie" value="R">R</option> <option class="movie" value="NC-17">NC-17</option> </select> </td> <td><input type="text" name="programname" size="40"></td> <td><input type="submit" value=" Submit "></td> </tr> </table> </form> <?php require_once 'connect.php'; $db_server = mysql_connect($db_hostname, $db_username, $db_password); if (!$db_server) die("Unable to connect to MySQL: " . mysql_error()); mysql_select_db($db_database) or die("Unable to select database: " . mysql_error()); $date = $_POST['date']; $time = $_POST['startime']; $type = $_POST['type']; $genre = $_POST['genre']; $rating = $_POST['rating']; $programname = $_POST['programname']; $query = "SELECT Schedule.Channel_Number, StationInformation.Station_Name, Schedule.Program_Name, Schedule.Date, Schedule.Time FROM Schedule INNER JOIN StationInformation ON Schedule.Channel_Number=StationInformation.Channel_Number INNER JOIN TVShows ON Schedule.Program_Name=TVShows.Program_Name WHERE Schedule.Date='$date' AND Schedule.Time='$time' AND TVShows.Genre='$genre' AND TVShows.Rating='$rating' AND TVShows.Program_Name='$programname' ORDER BY Channel_Number, Time LIMIT 50"; $result = mysql_query($query); if (!$result) die ("Database access failed: " . mysql_error()); $rows = mysql_num_rows($result); echo "<div id='content'>"; echo "<table align='center' class='CSSTableGenerator' width='1035'> <tr> <th>Channel Number</th> <th>Station Nation</th> <th>Program Name</th> <th>Date</th> <th>Time</th> </tr>"; for ($j = 0 ; $j < $rows ; ++$j) { $row = mysql_fetch_row($result); echo "<tr>"; echo "<td><center>" . $row[0] . "</center></td>"; echo "<td><center>" . $row[1] . "</center></td>"; echo "<td><center>" . $row[2] . "</center></td>"; echo "<td><center>" . date ('m/d/Y',strtotime($row[3])) . "</center></td>"; echo "<td><center>" . date ('h:i A',strtotime($row[4])) . "</center></td>"; } echo "</table>"; echo "</div>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/288447-database-search-from-form/ Share on other sites More sharing options...
BTW8892 Posted May 12, 2014 Author Share Posted May 12, 2014 Someone recommended me to use the following code, but it doesn't appear to be appending to the SELECT query. $query = "SELECT Schedule.Channel_Number, StationInformation.Station_Name, Schedule.Program_Name, Schedule.Date, Schedule.Time FROM Schedule INNER JOIN StationInformation ON Schedule.Channel_Number=StationInformation.Channel_Number INNER JOIN TVShows ON Schedule.Program_Name=TVShows.Program_Name WHERE 1 "; If(isset($_POST['date']) && !empty($_POST['date'])) { $date = mysql_real_escape_string($_POST['date']); $query .= " AND Schedule.Date='$date'"; } If(isset($_POST['startime']) && !empty($_POST['startime'])) { $startime = mysql_real_escape_string($_POST['startime']); $query .= " AND Schedule.Date='$startime'"; } Quote Link to comment https://forums.phpfreaks.com/topic/288447-database-search-from-form/#findComment-1479281 Share on other sites More sharing options...
MDCode Posted May 12, 2014 Share Posted May 12, 2014 1. You do not need to check isset() if you use empty(). 2. Your query will return an error because of the random 1 at the end 3. Are the vars actually set? Have you echoed $query at the end? Have you tried print_r($_POST); ? Quote Link to comment https://forums.phpfreaks.com/topic/288447-database-search-from-form/#findComment-1479288 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.