dongpo5207 2011-02-24 03:51
浏览 128
已采纳

在表单中使用带有SQL的SELECT语句的下拉菜单

Thank you for reading my question.

I am building a form that will let users search a db for cards used in a collectible card game. I want to include a dropdown menu that will let users narrow their search down to a specific card expansion.

Here is my 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 is the select statement:

$sql = "SELECT * FROM carddb 
WHERE Expsn ='" . $_POST['Expsn'] . "' AND Type='Hero' ";

This works well when selecting either Core or MoR, but if the dropbox is left blank "All Expansions" is passed into the $_POST['Expsn']. (I use echo $sql; to output the SELECT statement). How can I set up the default to return card from all expansions.

I have spent the last few weeks learning PHP and SQL online, so it is very possible that my whole approach here is VERY wrong and not scalable, so any 'big picture' help would be greatly appreciated. Thank you.

  • 写回答

3条回答 默认 最新

  • duanlao1552 2011-02-24 03:57
    关注

    If Expsn comes set, then add the condition to SQL, like:

    $expsn = (strlen($_POST['Expsn']) > 0) ? "Expsn = '" . $_POST['Expsn'] . "' AND " : '';
    $sql = "SELECT * FROM carddb WHERE $expsn Type='Hero'";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 idea自动补全键位冲突
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 定制ai直播实时换脸软件
  • ¥100 栈回溯相关,模块加载后KiExceptionDispatch无法正常回溯了
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错
  • ¥15 java python或者任何一种编程语言复刻一个网页