Php从HTML复选框中选择并选择适当的查询

时间:2014-09-02 12:30:40

标签: php html mysql sql

我有一个带有复选框的HTML文件,如下所示:

 <body>
<div id="wb_Web_Reports" style="position:absolute;left:329px;top:143px;width:510px;height:244px;z-index:4;">
<form name="Web_Reports" method="post" action="stsarrival.php" enctype="text/plain" id="Web_Reports">
<input type="submit" id="Button1" name="" value="Submit" style="position:absolute;left:210px;top:184px;width:96px;height:25px;z-index:0;">
<input type="text" id="ArrivalDate" style="position:absolute;left:140px;top:71px;width:253px;height:33px;line-height:33px;z-index:1;" name="ArrivalDate" value="">
<div id="wb_Text1" style="position:absolute;left:195px;top:34px;width:234px;height:16px;z-index:2;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:13px;">From Arrival Date:</span></div>
<input type="checkbox" id="Checkbox1" name="Checkbox1" value="on" style="position:absolute;left:442px;top:71px;z-index:3;" onclick="document.getElementById('ArrivalDate').disabled=this.checked;">
   </form>

如果选中该复选框,则发布的php文件(stsarrival.php)应运行查询,如果未选中该复选框,则运行不同的查询。我写了以下内容:

if (isset($_POST['[Checkbox1]'])) {

    $result = mysql_query("SELECT *  FROM Main_Data_Table INNER JOIN Title ON 
Main_Data_Table.TitleID = Title.ID INNER JOIN Agent ON Main_Data_Table.AgentID = Agent.ID 
INNER JOIN Accommodation ON Main_Data_Table.AccommodationID = Accommodation.ID 
INNER JOIN `Host Family details` ON Main_Data_Table.HostFamilyID = `Host Family details`.ID 
INNER JOIN Transfers ON Main_Data_Table.TransferId = Transfers.ID 
INNER JOIN `Board Basis` ON Main_Data_Table.BoardBasisID = `Board Basis`.ID 
INNER JOIN `Course Booked` ON Main_Data_Table.CourseTypeID = `Course Booked`.ID");
} else {

    $result = mysql_query("SELECT *  FROM Main_Data_Table INNER JOIN Title ON 
Main_Data_Table.TitleID = Title.ID INNER JOIN Agent ON Main_Data_Table.AgentID = Agent.ID 
INNER JOIN Accommodation ON Main_Data_Table.AccommodationID = Accommodation.ID 
INNER JOIN `Host Family details` ON Main_Data_Table.HostFamilyID = `Host Family details`.ID 
INNER JOIN Transfers ON Main_Data_Table.TransferId = Transfers.ID 
INNER JOIN `Board Basis` ON Main_Data_Table.BoardBasisID = `Board Basis`.ID 
INNER JOIN `Course Booked` ON Main_Data_Table.CourseTypeID = `Course Booked`.ID 
  WHERE (Main_Data_Table.`Arrival Date` = '2014-09-01')");
}

但是无论选中是否选中复选框,我总是会得到一个查询的结果。

请帮忙......

感谢

6 个答案:

答案 0 :(得分:1)

isset返回一个布尔值,因此它永远不会等于字符串"on"。只需删除:

if (isset($_POST['Checkbox1'])) {

无需检查值,因为如果未选中该复选框,则后置数组中不存在该键

答案 1 :(得分:1)

好的解决方案非常简单 - 我很惭愧地说......

我在写

['[Checkbox1]'] 

而不是

 ['Checkbox1']

所以完整的工作线是:

if (isset($_POST['Checkbox1'])&& $_POST['Checkbox1'] == 1)

有两个额外的[] ......

谢谢大家

答案 2 :(得分:1)

像这样......

$query  = "
SELECT *  
  FROM Main_Data_Table m
  JOIN Title ti
    ON m.TitleID = ti.ID 
  JOIN Agent ag
    ON m.AgentID = ag.ID 
  JOIN Accommodation ac
    ON m.AccommodationID = ac.ID 
  JOIN `Host Family details` d
    ON m.HostFamilyID = d.ID 
  JOIN Transfers tr
    ON m.TransferId = tr.ID 
  JOIN `Board Basis` b
    ON m.BoardBasisID = b.ID 
  JOIN `Course Booked` c
    ON m.CourseTypeID = c.ID
";


if (isset($_POST['Checkbox1'])&& $_POST['Checkbox1'] == 1){
$query .=  " WHERE m.`Arrival Date` = '2014-09-01'";
}

...但不要使用“SELECT *”,也不要在列/表名中使用空格。它将驱动你的债券人

答案 3 :(得分:0)

更改代码:

if (isset($_POST['Checkbox1'])== "on") {

要:

if (isset($_POST['Checkbox1']) && $_POST['Checkbox1'] == "on") {

同时尽量不要使用mysql_*函数,因为它们已被弃用!

  

从PHP 5.5.0开始,不推荐使用此扩展,不建议用于编写新代码,因为将来会删除它。相反,应该使用mysqli或PDO_MySQL扩展。另请参阅MySQL API概述,以便在选择MySQL API时获得进一步的帮助。

答案 4 :(得分:0)

更改代码如下:

if (isset($_POST['Checkbox1']) && $_POST['Checkbox1'] == "on") 
{
}
else 
{
}

答案 5 :(得分:0)

不要将复选框的值设为打开或关闭。只需给一个或零。尝试提交表单并检查帖子中的值。根据该值,您可以编写所需的条件。