Azyrus Posted September 3, 2013 Share Posted September 3, 2013 Hello, my code below seems to work great when you enter a value into the input however it does not return all values when the input is empty. It shows rows 1-5 and 79-95 does anyone know how this is happening? Thank you ! <div id="main"> <form method="post" action=""> <div id="search_query"> Keyword: <input type="text" name="brand"> <input type="submit" name="submit" value="submit"> </div> </form> <div id="main_container"> <?php $db_con = mysql_connect('localhost', 'hwsuser', 'password'); if (!$db_con) { die('Could not connect: ' . mysql_error()); } mysql_select_db('hws', $db_con); if(!isset($_POST['submit'])) { $sql = sprintf("SELECT * FROM hws"); $result = mysql_query($sql); echo "<table>"; echo "<tr>"; echo "<th scope='col'>ID</th>"; echo "<th scope='col'>Brand</th>"; echo "<th scope='col'>Model</th>"; echo "<th scope='col'>Capacity</th>"; echo "<th scope='col'>Loading</th>"; echo "<th scope='col'>Elements</th>"; echo "<th scope='col'>Type</th>"; echo "<th scope='col'>Warranty</th>"; echo "<th scope='col'>Reece</th>"; echo "<th scope='col'>Coop</th>"; echo "<th scope='col'>Samios</th>"; echo "<th scope='col'>KR</th>"; echo "<th scope='col'>BBK</th>"; echo "<th scope='col'>Supply</th>"; echo "<th scope='col'>Continuous</th>"; echo "<th scope='col'>Offpeak</th>"; echo "</tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>$row[ID]</td>"; echo "<td>$row[Brand]</td>"; echo "<td>$row[Model]</td>"; echo "<td>$row[Capacity]</td>"; echo "<td>$row[Loading]</td>"; echo "<td>$row[Elements]</td>"; echo "<td>$row[Type]</td>"; echo "<td>$row[Warranty]</td>"; echo "<td>$row[Reece]</td>"; echo "<td>$row[Coop]</td>"; echo "<td>$row[Samios]</td>"; echo "<td>$row[KR]</td>"; echo "<td>$row[BBK]</td>"; echo "<td>$row[Supply]</td>"; echo "<td>$row[Continuous]</td>"; echo "<td>$row[Offpeak]</td>"; echo "</tr>"; } echo "</table>"; } else { $brand = mysql_real_escape_string($_POST['brand']); $sql = sprintf("SELECT * FROM units WHERE Brand LIKE'$brand' OR Model LIKE'$brand' OR Capacity LIKE'$brand' OR Type LIKE'$brand'"); $result = mysql_query($sql); echo "<table class='altrowstable' id='alternatecolor'>"; echo "<tr>"; echo "<th scope='col'>ID</th>"; echo "<th scope='col'>Brand</th>"; echo "<th scope='col'>Model</th>"; echo "<th scope='col'>Capacity</th>"; echo "<th scope='col'>Loading</th>"; echo "<th scope='col'>Elements</th>"; echo "<th scope='col'>Type</th>"; echo "<th scope='col'>Warranty</th>"; echo "<th scope='col'>Reece</th>"; echo "<th scope='col'>Coop</th>"; echo "<th scope='col'>Samios</th>"; echo "<th scope='col'>KR</th>"; echo "<th scope='col'>BBK</th>"; echo "<th scope='col'>Supply</th>"; echo "<th scope='col'>Continuous</th>"; echo "<th scope='col'>Offpeak</th>"; echo "</tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>$row[ID]</td>"; echo "<td>$row[Brand]</td>"; echo "<td>$row[Model]</td>"; echo "<td>$row[Capacity]</td>"; echo "<td>$row[Loading]</td>"; echo "<td>$row[Elements]</td>"; echo "<td>$row[Type]</td>"; echo "<td>$row[Warranty]</td>"; echo "<td>$row[Reece]</td>"; echo "<td>$row[Coop]</td>"; echo "<td>$row[Samios]</td>"; echo "<td>$row[KR]</td>"; echo "<td>$row[BBK]</td>"; echo "<td>$row[Supply]</td>"; echo "<td>$row[Continuous]</td>"; echo "<td>$row[Offpeak]</td>"; echo "</tr>"; } echo "</table>"; } Quote Link to comment Share on other sites More sharing options...
Yohanne Posted September 3, 2013 Share Posted September 3, 2013 It might couse of your condition. $sql = sprintf("SELECT * FROM units WHERE Brand LIKE'$brand' OR Model LIKE'$brand' OR Capacity LIKE'$brand' OR Type LIKE'$brand'"); try it into "==" $sql = sprintf("SELECT * FROM units WHERE Brand = '$brand' OR Model = '$model' OR Capacity = '$capacity' OR Type = '$type'"); Quote Link to comment Share on other sites More sharing options...
Barand Posted September 3, 2013 Share Posted September 3, 2013 Are the records that appear the ones that have an empty string value in one or more of those four columns? Use the wildcard character "%" with LIKE. That way if $brand is empty then all values are matched. $sql = "SELECT * FROM units WHERE Brand LIKE '%$brand%' OR Model LIKE '%$brand%' OR Capacity LIKE '%$brand%' OR Type LIKE '%$brand%'"; Quote Link to comment Share on other sites More sharing options...
Azyrus Posted September 3, 2013 Author Share Posted September 3, 2013 Sorry for not clarifying guys, the ELSE statement is working correctly as it is intended the first part of the IF statement is not displaying ALL records as it is expected. Any ideas? Quote Link to comment Share on other sites More sharing options...
inversesoft123 Posted September 4, 2013 Share Posted September 4, 2013 The 'like' query can have the possibility of returning more matches, especially if the argument it is given has the proper wildcards in it. '%PHP%', for instance, will match 'PHPFREAKS' and 'PHP', but as a literal it will not match anything that doesn't have the percent signs in those specific spots. In else part you may have records in DB with exact match. 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.