PHP/MySQL search... show all data by default, or show matched data -


i've built page shows whole mysql table want add search it.

what i've done set when user searches displays matching results, if there's no search displays whole table. it's showing table, when search it's returning no data (everything else working though)

this i've got search...

<?php if(isset($_post['submit'])){ if(isset($_get['query'])){ if(preg_match("/^[  a-za-z]+/", $_post['query'])){ $query=$_get['query']; $q=mysql_query("select * employees name '%$query%'" ) or die("could not search"); $result = mysql_query($q) or die(mysql_error()); while ($row = mysql_fetch_array($result)){ 

i can't work out how needs formatted. search box 'query'. appreciated!

----- edit ----- can't work still... pointers?

<?php if(!empty($_session['loggedin']) && !empty($_session['username']){?><br /><form method="post" action="search.php"> <input name="query" type="text" required class="forms" id="query" placeholder="search name..." size="35" /> <input type="submit" name="submit" id="submit" value=" search " height="25" width="70" border="0" alt="submit"><button onclick="window.location.href='search.php'"> clear </button></form><br /> <table border="0" cellspacing="2" class="data"><tr> <td align="center" class="idtd"><strong>id</strong></td> <td align="center" class="nametd"><strong>name</strong></td> <td align="center" class="positiontd"><strong>position</strong></td> <td align="center" class="banktd"><strong>bank</strong></td> <td align="center" class="pooltd"><strong>pool</strong></td> <td align="center" class="starttd"><strong>start date</strong></td> <td align="center" class="endtd"><strong>end date</strong></td> <td align="center" class="ghourstd"><strong>gross hours</strong></td> <td align="center" class="chourstd"><strong>cont'd hours</strong></td> </tr></table> <?php if(isset($_post['submit'])){$where = !empty($_get['query']) ? $db->real_escape_string($_get['query']) : ""; $q = mysql_query("select * employees name '% " . $where . "%'") or die(mysql_error()); while ($row = mysql_fetch_array($result)){ echo "<table class='data' border='0' cellspacing='2'><tr> <td align='center' class='idtd'>".$row['id']."</td> <td align='center' class='nametd'>".$row['name']."</td> <td align='center' class='positiontd'>".$row['position']."</td> <td align='center' class='banktd'>".$row['bank']."</td> <td align='center' class='pooltd'>".$row['pool']."</td> <td align='center' class='starttd'>".$nstartdate."</td> <td align='center' class='endtd'>".$row['enddate']."</td> <td align='center' class='ghourstd'>".$row['grosshours']."</td> <td align='center' class='chourstd'>".$row['contractedhours']."</td><tr ></table>";}}}} else{ ?> <h1>you must logged in view page.</h1><?php } ?> 

take of variable below

$condition = '1 = 1';//which result rows (1=1 true) if($_get['query']) {   $query=$_get['query'];   $condition = " name '%$query%'";// search querydata }  $q=mysql_query("select * employees '$condition'" ) or die("could not search");  $result = mysql_query($q) or die(mysql_error());//you have double mysql_query() remove 

and important thing stop using mysql_* functions deprecated long ago, use mysqli_* or pdo , dont think these new & difficult ,once @ tutorials example.


Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -