contra10 Posted January 13, 2009 Share Posted January 13, 2009 hi i want to make a search based on the options selected i tried using elseif statements before and i did it this way as well...i want it so that if an option is selected that mysql will search the db based on the criteria selected... <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <table border="0" height="500" bgcolor="#5f5f5f" align="center"> <tr><td align="center" bgcolor="black"><b><h3>Search Criteria</b></tr></td> <td align="center" bgcolor="#DEDAD7"><b><h4>RESULTS</b></td> <tr><td align="center" font="red"><h5>ADVERTISMENTS</tr></td> <tr><td><b>Username:</b> <input type="text" name="username" maxlength="50" value="username" STYLE="color: #FFFFFF; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #72A4D2;"><p> <b>Gender:</b><p> <input type="radio" name="sexm" value="male" align="left"> Male <br> <input type="radio" name="sexf" value="female" align="right"> Female <p> <b>Age:</b> <input type="text" name="age1" maxlength="02" size="01" STYLE="color: #FFFFFF; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #72A4D2;"> To <input type="text" name="age2" maxlength="02" size="01" STYLE="color: #FFFFFF; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #72A4D2;"> <p> <b>Country:</b> <select name="country" type="country" STYLE="color: #FFFFFF; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #72A4D2;"> <option value="USA" selected>USA</option> <option value="UK">UK</option> <option value="Albania">Albania</option> <option value="Algeria">Algeria</option> <option value="American Samoa">American Samoa</option> <option value="Andorra">Andorra</option> <option value="Angola">Angola</option> <option value="Anguilla">Anguilla</option> <option value="Antigua">Antigua</option> <option value="Argentina">Argentina</option> <option value="Armenia">Armenia</option> <option value="Aruba">Aruba</option> <option value="Australia">Australia</option> <option value="Austria">Austria</option> <option value="Azerbaijan">Azerbaijan</option> <option value="Bahamas">Bahamas</option> <option value="Bahrain">Bahrain</option> <option value="Bangladesh">Bangladesh</option> <option value="Barbados">Barbados</option> <option value="Barbuda">Barbuda</option> <option value="Belgium">Belgium</option> <option value="Belize">Belize</option> <option value="Benin">Benin</option> <option value="Bermuda">Bermuda</option> <option value="Bhutan">Bhutan</option> <option value="Bolivia">Bolivia</option> <option value="Bonaire">Bonaire</option> <option value="Botswana">Botswana</option> <option value="Brazil">Brazil</option> <option value="Virgin islands">British Virgin isl.</option> <option value="Brunei">Brunei</option> <option value="Bulgaria">Bulgaria</option> <option value="Burundi">Burundi</option> <option value="Cambodia">Cambodia</option> <option value="Cameroon">Cameroon</option> <option value="Canada">Canada</option> <option value="Cape Verde">Cape Verde</option> <option value="Cayman isl">Cayman Islands</option> <option value="Central African Rep">Central African Rep.</option> <option value="Chad">Chad</option> <option value="Channel isl">Channel Islands</option> <option value="Chile">Chile</option> <option value="China">China</option> <option value="Colombia">Colombia</option> <option value="Congo">Congo</option> <option value="cook isl">Cook Islands</option> <option value="Costa Rica">Costa Rica</option> <option value="Croatia">Croatia</option> <option value="Curacao">Curacao</option> <option value="Cyprus">Cyprus</option> <option value=" Czech Republic">Czech Republic</option> <option value= "Denmark">Denmark</option> <option value="Djibouti">Djibouti</option> <option value="Dominica">Dominica</option> <option value="Dominican Republic">Dominican Republic</option> <option value="Ecuador">Ecuador</option> <option value="Egypt">Egypt</option> <option value="El Salvador">El Salvador</option> <option value="Equatorial Guinea">Equatorial Guinea</option> <option value="Eritrea">Eritrea</option> <option value="Estonia">Estonia</option> <option value="Ethiopia">Ethiopia</option> <option value="Faeroe isl">Faeroe Islands</option> <option value="Fiji">Fiji</option> <option value="Finland">Finland</option> <option value="France">France</option> <option value="French Guiana">French Guiana</option> <option value="French Polynesia">French Polynesia</option> <option value="Gabon">Gabon</option> <option value="Gambia">Gambia</option> <option value="Georgia">Georgia</option> <option value="Gemany">Germany</option> <option value="Ghana">Ghana</option> <option value="Gibraltar">Gibraltar</option> <option value="GB">Great Britain</option> <option value="Greece">Greece</option> <option value="Greenland">Greenland</option> <option value="Grenada">Grenada</option> <option value="Guadeloupe">Guadeloupe</option> <option value="Guam">Guam</option> <option value="Guatemala">Guatemala</option> <option value="Guinea">Guinea</option> <option value="Guinea Bissau">Guinea Bissau</option> <option value="Guyana">Guyana</option> <option value="Haiti">Haiti</option> <option value="Honduras">Honduras</option> <option value="Hong Kong">Hong Kong</option> <option value="Hungary">Hungary</option> <option value="Iceland">Iceland</option> <option value="India">India</option> <option value="Indonesia">Indonesia</option> <option value="Irak">Irak</option> <option value="Iran">Iran</option> <option value="Ireland">Ireland</option> <option value="Northern Ireland">Ireland, Northern</option> <option value="Israel">Israel</option> <option value="Italy">Italy</option> <option value="Ivory Coast">Ivory Coast</option> <option value="Jamaica">Jamaica</option> <option value="Japan">Japan</option> <option value="Jordan">Jordan</option> <option value="Kazakhstan">Kazakhstan</option> <option value="Kenya">Kenya</option> <option value="Kuwait">Kuwait</option> <option value="Kyrgyzstan">Kyrgyzstan</option> <option value="Latvia">Latvia</option> <option value="Lebanon">Lebanon</option> <option value="Liberia">Liberia</option> <option value="Liechtenstein">Liechtenstein</option> <option value="Lithuania">Lithuania</option> <option value="Luxembourg">Luxembourg</option> <option value="Macau">Macau</option> <option value="Macedonia">Macedonia</option> <option value="Madagascar">Madagascar</option> <option value="Malawi">Malawi</option> <option value="Malaysia">Malaysia</option> <option value="Maldives">Maldives</option> <option value="Mali">Mali</option> <option value="Malta">Malta</option> <option value="Marshall isl">Marshall Islands</option> <option value="Martinique">Martinique</option> <option value="Mauritania">Mauritania</option> <option value="Mauritius">Mauritius</option> <option value="Mexico">Mexico</option> <option value="Micronesia">Micronesia</option> <option value="Moldova">Moldova</option> <option value="Monaco">Monaco</option> <option value="Mongolia">Mongolia</option> <option value="Montserrat">Montserrat</option> <option value="Morocco">Morocco</option> <option value="Mozambique">Mozambique</option> <option value="Myanmar">Myanmar/Burma</option> <option value="Namibia">Namibia</option> <option value="Nepal">Nepal</option> <option value="Netherlands">Netherlands</option> <option value="Netherlands Antilles">Netherlands Antilles</option> <option value="New Caledonia">New Caledonia</option> <option value="New Zealand">New Zealand</option> <option value="Nicaragua">Nicaragua</option> <option value="Niger">Niger</option> <option value="Nigeria">Nigeria</option> <option value="Norway">Norway</option> <option value="Oman">Oman</option> <option value=""Palau>Palau</option> <option value="Panama">Panama</option> <option value="Papua New Guinea">Papua New Guinea</option> <option value="Paraguay">Paraguay</option> <option value="Peru">Peru</option> <option value="Philippines">Philippines</option> <option value="Poland">Poland</option> <option value="Portugal">Portugal</option> <option value="Puerto Rico">Puerto Rico</option> <option value="Qatar">Qatar</option> <option value="Reunion">Reunion</option> <option value="Rwanda">Rwanda</option> <option value="Saba">Saba</option> <option value="Saipan">Saipan</option> <option value="Saudi Arabia">Saudi Arabia</option> <option value="Scotland">Scotland</option> <option value="Senegal">Senegal</option> <option value="Seychelles">Seychelles</option> <option value="Sierra Leone">Sierra Leone</option> <option value="Singapore">Singapore</option> <option value="Slovac Republic">Slovak Republic</option> <option value="Slovenia">Slovenia</option> <option value="South Africa">South Africa</option> <option value="South Korea">South Korea</option> <option value="Spain">Spain</option> <option value="Sri Lanka">Sri Lanka</option> <option value="Sudan">Sudan</option> <option value="Suriname">Suriname</option> <option value="Swaziland">Swaziland</option> <option value="Sweden">Sweden</option> <option value="Switzerland">Switzerland</option> <option value="Syria">Syria</option> <option value="Taiwan">Taiwan</option> <option value="Tanzania">Tanzania</option> <option value="Thailand">Thailand</option> <option value="Togo">Togo</option> <option value="Trinidad-Tobago">Trinidad-Tobago</option> <option value="Tunesia">Tunisia</option> <option value="Turkey">Turkey</option> <option value="Turkmenistan">Turkmenistan</option> <option value="United Arab Emirates">United Arab Emirates</option> <option value="U.S. Virgin isl">U.S. Virgin Islands</option> <option value="USA">U.S.A.</option> <option value="Uganda">Uganda</option> <option value="United Kingdom">United Kingdom</option> <option value="Urugay">Uruguay</option> <option value="Uzbekistan">Uzbekistan</option> <option value="Vanuatu">Vanuatu</option> <option value="Vatican City">Vatican City</option> <option value="Venezuela">Venezuela</option> <option value="Vietnam">Vietnam</option> <option value="Wales">Wales</option> <option value="Yemen">Yemen</option> <option value="Zaire">Zaire</option> <option value="Zambia">Zambia</option> <option value="Zimbabwe">Zimbabwe</option> </select><p> <input type="submit" name="submit" value="submit"> </tr></td> <td width="600" bgcolor="#DEDAD7"><?php // Connects to your Database mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("registration") or die(mysql_error()); //This code runs if the form has been submitted if (isset($_POST['submit'])) { $postuser = "{$_POST['username']}"; $postcountry = "{$_POST['country']}"; $query = "SELECT * FROM users WHERE username = '$postuser' and country ='$postcountry'"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); $user = "{$row['id']}"; $userq = "{$row['username']}"; $mycountry = "{$row['country']}"; $sex = "{$row['sex']}"; $city = "{$row['city']}"; echo "<TABLE width=600 bgcolor=black border=1 align=center cellpadding=4 cellspacing=0>"; echo "<tr>"; echo "<td width='35%'><a style='text-decoration:none' href='http://localhost/profile/index.php?user=$user'><FONT FACE='ariel' SIZE='2'>$userq<p>"; echo "$city, $mycountry $sex</a></FONT></td>"; echo "</tr>"; } ?> </td> </table> </form> <? } ?> Quote Link to comment Share on other sites More sharing options...
dawsba Posted January 13, 2009 Share Posted January 13, 2009 just simply add if statements after your $query ie if(isset($sexf)){$query .= "and `sex` = 'female' ";} and so on for each option. (dont forget to use .= {full stop equals} or it will override the previous query string. Quote Link to comment Share on other sites More sharing options...
contra10 Posted January 13, 2009 Author Share Posted January 13, 2009 i did it the way our talking about and nothing displayed when i clicked female... i then did this <?php if(isset($_POST['submit']) and ($_POST['country'])) { $postuser = "{$_POST['username']}"; $postcountry = "{$_POST['country']}"; $query = "SELECT * FROM users WHERE country='$postcountry'"; $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)){ $user = "{$row['id']}"; $userq = "{$row['username']}"; $mycountry = "{$row['country']}"; $sex = "{$row['sex']}"; $city = "{$row['city']}"; echo "<TABLE width=600 bgcolor=black border=1 align=center cellpadding=4 cellspacing=0>"; echo "<tr>"; echo "<td width='35%'><a style='text-decoration:none' href='http://localhost/profile/index.php?user=$user'><FONT FACE='ariel' SIZE='2'>$userq<p>"; echo "$city, $mycountry $sex</a></FONT></td>"; echo "</tr>"; } } if(isset($_POST['sexf']) and ($_POST['country']) and ($_POST['submit'])){$query .= "and `sex` = 'female' ";} ?> it would display only the first query i then did this <?php if(isset($_POST['submit']) and ($_POST['country'])) { $postuser = "{$_POST['username']}"; $postcountry = "{$_POST['country']}"; $query = "SELECT * FROM users WHERE country='$postcountry'"; $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)){ $user = "{$row['id']}"; $userq = "{$row['username']}"; $mycountry = "{$row['country']}"; $sex = "{$row['sex']}"; $city = "{$row['city']}"; echo "<TABLE width=600 bgcolor=black border=1 align=center cellpadding=4 cellspacing=0>"; echo "<tr>"; echo "<td width='35%'><a style='text-decoration:none' href='http://localhost/profile/index.php?user=$user'><FONT FACE='ariel' SIZE='2'>$userq<p>"; echo "$city, $mycountry $sex</a></FONT></td>"; echo "</tr>"; } } if(isset($_POST['sexf']) and ($_POST['country']) and ($_POST['submit'])) { $postuser = "{$_POST['username']}"; $postcountry = "{$_POST['country']}"; $postsexf = "{$_POST['sexf']}"; $query2 .= "SELECT * FROM users WHERE country='$postcountry' and sex='$postsexf'"; $result2 = mysql_query($query2); while ($row2 = mysql_fetch_assoc($result2)){ $user2 = "{$row2['id']}"; $userq2 = "{$row2['username']}"; $mycountry2 = "{$row2['country']}"; $sex2 = "{$row2['sex']}"; $city2 = "{$row2['city']}"; echo "<TABLE width=600 bgcolor=black border=1 align=center cellpadding=4 cellspacing=0>"; echo "<tr>"; echo "<td width='35%'><a style='text-decoration:none' href='http://localhost/profile/index.php?user=$user2'><FONT FACE='ariel' SIZE='2'>$userq2<p>"; echo "$city2, $mycountry2 $sex2</a></FONT></td>"; echo "</tr>"; }?> at which i got the result i was looking for but it loops so i see the first result and this result Quote Link to comment Share on other sites More sharing options...
dawsba Posted January 13, 2009 Share Posted January 13, 2009 <?php if(isset($_POST['submit']) and ($_POST['country'])) { $postuser = "{$_POST['username']}"; $postcountry = "{$_POST['country']}"; $query = "SELECT * FROM users WHERE country='$postcountry'"; if(isset($_POST['sexf']) and ($_POST['country']) and ($_POST['submit'])){$query .= "and `sex` = 'female' ";} $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)){ $user = "{$row['id']}"; $userq = "{$row['username']}"; $mycountry = "{$row['country']}"; $sex = "{$row['sex']}"; $city = "{$row['city']}"; echo "<TABLE width=600 bgcolor=black border=1 align=center cellpadding=4 cellspacing=0>"; echo "<tr>"; echo "<td width='35%'><a style='text-decoration:none' href='http://localhost/profile/index.php?user=$user'><FONT FACE='ariel' SIZE='2'>$userq<p>"; echo "$city, $mycountry $sex</a></FONT></td>"; echo "</tr>"; } } ?> You have to put it in order wuth the original query before you run the DB result Quote Link to comment Share on other sites More sharing options...
redarrow Posted January 13, 2009 Share Posted January 13, 2009 slap some preg_match on that incase the user try to change the form ..... validate the variables and add mysql_real_ecsape_string(); <?php $postuser = "{mysql_real_ecsape_string($_POST['username'])}"; $postcountry = "{mysql_real_escape_string($_POST['country'])}"; if((!preg_match("/[a-z]/i",$postuser)||(!preg_match("/[a-z]/i",$postcountry)){ header("location: back_to_the_form_page.php"); } ?> just a thort. Quote Link to comment 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.