php - 从复选框查询

标签 php mysql sql

我的 mysql 数据库中有以下表格。我正在尝试创建一个应用程序,它可以虚拟评估某人是否可能患上食道癌

#tblsymptoms - holds all symptoms
######################################
symptomID  | symptom    
-------------------------------------
1            Mass in the throat        
2            Difficulty in swallowing   
3            Lost weight lately
4            Heartburn
5            Hoarse-sounding cough
6            Vomit blood


#tblresponse - holds all responses
######################################
responseID  | response   
-----------------------------------------------------
1            Your symptoms suggest you have a problem       
2            You may be having Oesophageal carcinoma  
3            You dont have a throat carcinoma


#tblrelation - holds relation between response and symptoms
######################################
relationID  | responseID | symptomID
-----------------------------
1         1          3
2         1          4
3         1          5
4         2          1
5         2          2
6         2          3
7         4          2
8         2          5
9         2          6
10        3          3
11        3          4

I want to use check boxes in the html form to query the database and here is the code for the form:

<form method="post" action="process.php">
<INPUT TYPE=CHECKBOX NAME="option[]" VALUE="Difficulty in swallowing" id="">Difficulty in swallowing<br>
<INPUT TYPE=CHECKBOX NAME="option[]" VALUE="Difficulty in swallowing" id="">Difficulty in swallowing<br>
<INPUT TYPE=CHECKBOX NAME="option[]" VALUE="Lost weight lately" id="">Lost weight lately<br>
<INPUT TYPE=CHECKBOX NAME="option[]" VALUE="Heartburn" id="">Heartburn<br>
<INPUT TYPE=CHECKBOX NAME="option[]" VALUE="Hoarse-sounding cough" id="">Hoarse-sounding cough<br>
<INPUT TYPE=CHECKBOX NAME="option[]" VALUE="Vomit blood" id="">Vomit blood<br>
<input type="submit" name="formSubmit" value="Diagnose me" />
</form>

现在,以防万一有人选择了:Lost weight lately and Heartburn 脚本应该从 tblResponse>>>responseID=1 获取数据并显示给用户:Your symptoms suggest you有问题。

我试过了,但它返回 0 个值

SELECT tblresponse.response
FROM tblrelation 
   LEFT JOIN tblresponse ON tblrelation.responseID = tblresponse.responseID
WHERE tblrelation.symptomID = '3' AND tblrelation.symptomID = '4' 

有人知道我应该将代码添加到带有复选框以查询数据库的 html 表单吗?

最佳答案

一列不能有多个值。试试这个:

SELECT tblresponse.response
FROM tblrelation LEFT JOIN
     tblresponse
      ON tblrelation.responseID = tblresponse.responseID
GROUP BY tblresponse.response
having sum(case when tblrelation.symptomID = '3' then 1 else 0 end) > 0 and
       sum(case when tblrelation.symptomID = '4' then 1 else 0 end) > 0

这会将所有关系组合在一起以获得给定的响应。 having 子句计算“3”和“4”的个数。如果它们都大于 0,则返回响应。

顺便说一下,如果 ID 是数字,则不需要引号。

关于php - 从复选框查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15160307/

相关文章:

php - 将 mysql 列转换为数组并在下拉列表中使用该数组

php - 在每个文件中包含 php 头文件

php - Doctrine 和 Symfony2 : WHERE a. title LIKE $array

mysql - 在有条件的重复 key 更新上?

php - 用 PHP 改变 CSS

php - 有没有办法检查 Laravel 计划是否正在运行?

PHP 按空格分割搜索栏关键词

mysql - 使用内连接 2 查询结果不准确

java - 验证连接的数据库 - Java 持久性 API

mysql - 查询无法执行,或者等待时间太长