php - 如何使用表单显示具有多个 where 条件的数据?

问题描述 投票:0回答:1

我为志愿者列表创建了一个数据库。然后是一个表格,用于提取注册志愿执行特定任务的志愿者的个人信息。我是 php 的初学者,我已经到处寻找答案并尝试了多种方法,但可惜,没有运气。

这是我目前正在努力解决的关键代码:

if(isset($_POST['planning']) && $_POST['planning'] == '1')
  { $result1 = mysql_query("SELECT * FROM volunteers WHERE planning = '1'");}
if(isset($_POST['signatures']) && $_POST['signatures'] == '1')                 
  { $result2 = mysql_query("SELECT * FROM volunteers WHERE signatures = '1'"); 
  $newresult1 = array_merge($result1, $result2);}
if(isset($_POST['canvassing']) && $_POST['canvassing'] == '1')
  { $result3 = mysql_query("SELECT * FROM volunteers WHERE canvassing = '1'");
  $newresult2 = array_merge($newresult1, $result3);}
if(isset($_POST['phone_bank']) && $_POST['phone_bank'] == '1')
  { $result4 = mysql_query("SELECT * FROM volunteers WHERE phone_bank = '1'"); 
  $newresult3 = array_merge($newresult2, $result4);}
if(isset($_POST['media']) && $_POST['media'] == '1')
  { $result5 = mysql_query("SELECT * FROM volunteers WHERE media = '1'"); $newresult4 =  
  array_merge($newresult3, $result5);}
if(isset($_POST['press_releases']) && $_POST['press_releases'] == '1')
  { $result6 = mysql_query("SELECT * FROM volunteers WHERE press_releases = '1'"); 
  $newresult5 = array_merge($newresult4, $result6);}
if(isset($_POST['volunteer_coordinator']) && $_POST['volunteer_coordinator'] == '1')
  { $result7 = mysql_query("SELECT * FROM volunteers WHERE volunteer_coordinator =
  '1'"); 
  $newresult6 = array_merge($newresult5, $result7);}
if(isset($_POST['speaker']) && $_POST['speaker'] == '1')
  { $result8 = mysql_query("SELECT * FROM volunteers WHERE speaker = '1'"); $newresult7
  = array_merge($newresult6, $result8);}
if(isset($_POST['house_parties']) && $_POST['house_parties'] == '1')
  { $result9 = mysql_query("SELECT * FROM volunteers WHERE house_parties = '1'"); 
  $newresult8 = array_merge($newresult7, $result9);}
if(isset($_POST['web_page']) && $_POST['web_page'] == '1')
  { $result10 = mysql_query("SELECT * FROM volunteers WHERE web_page = '1'"); 
  $newresult9 
  = array_merge($newresult8, $result10);}
if(isset($_POST['other']) && $_POST['other'] == '1')
  { $result11 = mysql_query("SELECT * FROM volunteers WHERE other = '1'"); $newresult10 
  = array_merge($newresult9, $result11);}

$newresult10 = array_unique($newresult10); 

while($row = mysql_fetch_array($newresult10)) {
echo $row['first_name'] . " " . $row['last_name'];  
echo "    ";
echo $row['email'];  
echo "    ";
echo $row['phone']; 
echo "<br />";

我非常感谢任何建议。

php conditional-statements where-clause
1个回答
0
投票

你可以这样写

[编辑]--------我认为这就是您想要的

$postVars = array(
    "canvassing"=>"1",
    "other"=>"1"
    //and you would go on post name valid post value
);

//start the query
$query="SELECT * FROM volunteers WHERE ";    

//loop thru the posibable post vars and check and add to the query
$andcount=0;
foreach($postVars as $name=>$value){
    if(isset($_POST[$name]) && $_POST[$name] == $value){
        if($andcount>0)$query.=" AND ";
        $query.=" ".$name."= '".mysql_real_escape_string($value)."'";
        $andcount++;
    }
}
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
    echo $row['first_name'] . " " . $row['last_name'];  
    echo "&nbsp;&nbsp;&nbsp;&nbsp;";
    echo $row['email'];  
    echo "&nbsp;&nbsp;&nbsp;&nbsp;";
    echo $row['phone']; 
    echo "<br />";
}
© www.soinside.com 2019 - 2024. All rights reserved.