PHP-mysql:在搜索中使用单选按钮

时间:2014-06-24 14:47:16

标签: php mysql

我使用php和mysql为大学创建了一个数据库;但是我希望能够搜索条目。到目前为止,我可以搜索" text"诸如" name"之类的信息和"项目描述&#34 ;;但无法通过单选按钮按性别搜索学生:M或F.

搜索表单如下:

<tr><td><?php _e("Home university");?>:</td><td><input type="text" name="place" value="<?php echo cG("place");?>" /></td></tr>
<tr><td><?php _e("Supervisor Name");?>:</td><td><input type="text" name="title" value="<?php echo cG("title");?>" /></td></tr>
<tr><td><?php _e("Project Description Keyword");?>:</td><td><input type="text" name="desc" value="<?php echo cG("desc");?>" /></td></tr>

输入信息的表格:

if (strlen(cG("title"))>=MIN_SEARCH_CHAR){
    $filter.= " and p.title like '%".cG("title")."%' ";
    $order="p.insertDate Desc";
    $advs=true;
}

if (strlen(cG("desc"))>=MIN_SEARCH_CHAR){
    $filter.= " and p.description like '%".cG("desc")."%' ";
    $order="p.insertDate Desc";
    $advs=true;
}

1 个答案:

答案 0 :(得分:0)

我试着阅读你的代码然后猜测了。

这应该起作用或者至少是解决方案或线索,取决于“cG”功能的作用。但您仍需要更改某些内容以适合您的MySQL表定义并进行调试。

<tr>
  <td>
    Gender
  </td>
  <td>
    <input type="radio" name="gender" value="Male" <?php if(cG("gender") == "Male") echo "checked=\"checked\" ";?>/>Male<br />
    <input type="radio" name="gender" value="Female" <?php if(cG("gender") == "Female") echo "checked=\"checked\" ";?>/>Female
  </td>
</tr>

在PHP脚本中:

if (strlen(cG("gender")) >= 4){
  $filter.= " and YOUR_GENDER_COLUMN_NAME_IN_TABLE = '".cG("gender")."' ";
  $order="p.insertDate Desc";
  $advs=true;
}

请记住检查用户输入以避免SQL注入。