choosing from only one drop down box in php -
i have 2 list boxes , click on both of them create select query.i placed $post variable variable , placed these select query.this seems work fine,but problem arises when want select 1 of boxes example of ken davis's books or books adventure genre.it seems have chhose both boxes before result.can suggest way round this
<html> <head> <title>my page</title> </head> <body> <br> <form name="myform" action="dropdown2.php" method="post"> <select name="author" size="4"> <option value="ken davies">ken davies</option> <option value= "arthur smith">arthur smith</option> <option value="gill rafferty">gill rafferty</option><br /> <option value="molly brown">molly brown</option><br /> <option value="gilbert riley">gilbert riley</option><br /> <input type = "submit" name = "submit" value = "go"> <select name="genre" size="4"> <option value="adventure">adventure</option> <option value="biography">biography</option> <option value="crime">crime</option><br /> <option value="romance">romance</option> <option value="2007">thriller</option> <input type = "submit" name = "submit" value = "go"> <?php $_post['author']; $bird = $_post['author']; $_post['genre']; $cat = $_post['genre']; $con = mysql_connect("localhost","root",""); if (!$con){ die("can not connect database" . mysql_error()); } mysql_select_db("authors",$con); $sql = "select * books author = '$bird' , genre = '$cat' "; $mydata = mysql_query($sql,$con); echo"<table border=1> <tr>id</th> <tr>author</th> <tr>title</th> <tr>publisher</th> <tr>year</th> <tr>genre</th> <tr>sold</th> </tr>"; while($record = mysql_fetch_array($mydata)){ echo "<tr>"; echo "<td>" . $record['id'] . "</td>"; echo "<td>" . $record['author'] . "</td>"; echo "<td>" . $record['title'] . "</td>"; echo "<td>" . $record['publisher'] . "</td>"; echo "<td>" . $record['year'] . "</td>"; echo "<td>" . $record['genre'] . "</td>"; echo "<td>" . $record['sold'] . "</td>"; echo "<tr />"; } echo "</table>"; mysql_close($con); ?> </form> </body> </html>
if want filter author or genre, change logic leading sql.
if (isset($bird) && isset($cat)) $sql = "select * books author = '$bird' , genre = '$cat' "; elseif (isset($bird)) $sql = "select * books author = '$bird' "; elseif (isset($cat)) $sql = "select * books genre = '$cat' ";
also i'm legally required let know putting variables in sql lead sql injection. should prepare , execute. i'll write example code below (one sec).
Comments
Post a Comment