Issue with filtering down MySQL database with internal search PHP -
i trying search box working filter down little dummy database made, when try , search in search box on site gives me error:
you have error in sql syntax; check manual corresponds mysql server version right syntax use near '= ''' @ line 1 i have feeling mysql query isn't working "select * table city = searched" doesn't sound right me.
<?php mysql_connect("***","******","*****") or die(mysql_error()); mysql_select_db("*****"); $sql = "select * table"; if (isset($_post['search'])) { $search_term = mysql_real_escape_string($post['search_box']); $sql .= "where city = '{$search_term}'"; } $query = mysql_query($sql) or die(mysql_error()); ?> <form name='search' method="post" action="test.php" > search: <input type="text" name="search_box" value=""> <input type="submit" name="search" value="search table..."> </form> <table width="70%"> <tr> <td><strong>city</strong></td> <td><strong>country</strong></td> <td><strong>climate</strong></td> <td><strong>company</strong></td> <td><strong>activities</strong></td> <td><strong>continent</strong></td> <td><strong>terrain</strong></td> </tr> <?php while ($row = mysql_fetch_array($query)) { ?> <tr> <td><?php echo $row['city']; ?></td> <td><?php echo $row['country']; ?></td> <td><?php echo $row['climate']; ?></td> <td><?php echo $row['company']; ?></td> <td><?php echo $row['activities']; ?></td> <td><?php echo $row['continent']; ?></td> <td><?php echo $row['terrain']; ?></td> </tr> <?php } ?> </table> thanks.
you have typo. this:
$post['search_box'] should this:
$_post['search_box'] which means you're creating empty variable ($search_term). you'd see if had error reporting on. next time when develop, put @ top of scripts:
ini_set('display_errors', 1); error_reporting(-1); // or use e_all and last not least, need space before where:
$sql .= " city = '{$search_term}'"; notes
- you need run query (ie,
mysql_query()). - you'd better off learning pdo / mysqli prepared statements
mysql_*deprecated.
one more thing note search query. you're going find exact match search query submitted. you're best stick best practice , search using like:
$sql .= " city '%{$search_term}%'";
Comments
Post a Comment