php - Using a dropdown menu in a form for a SELECT statement with SQL -
thank reading question.
i building form let users search db cards used in collectible card game. want include dropdown menu let users narrow search down specific card expansion.
here dropdown menu:
<select name="expsn" onchange="this.submit()"> <option>all expansions</option> <option value="core">core</option> <option value="som">shadows of mirkwood</option>
and here select statement:
$sql = "select * carddb expsn ='" . $_post['expsn'] . "' , type='hero' ";
this works when selecting either core or mor, if dropbox left blank "all expansions" passed $_post['expsn']. (i use echo $sql; output select statement). how can set default return card expansions.
i have spent last few weeks learning php , sql online, possible whole approach here wrong , not scalable, 'big picture' appreciated. thank you.
if expsn comes set, add condition sql, like:
$expsn = (strlen($_post['expsn']) > 0) ? "expsn = '" . $_post['expsn'] . "' , " : ''; $sql = "select * carddb $expsn type='hero'";
Comments
Post a Comment