PHP if-else条件不起作用

时间:2017-02-28 06:59:16

标签: php mysql

我在尝试从if-else条件中检索值时遇到问题。 我的查询粘贴在下面:

    <?php
session_start();

if(!$_SESSION['login'] && !isset($_POST['submit'])) {
header("Location:LoginPage.php");
}

function filterTable($query) 
{

$db_name = "id555865_sales_db";
$mysql_username = "id555865_sales_db";
$mysql_password = "password";
$server_name = "localhost"; 
$conn = mysqli_connect($server_name, $mysql_username,$mysql_password,$db_name);
$filter_result = mysqli_query($conn,$query);
return $filter_result;
}


if(isset($_POST['submit']) && isset($_POST['fromDate']) && isset($_POST['toDate']) && isset($_POST['userName']) )
{
$from_date = $_POST['fromDate'];
$to_date = $_POST['toDate'];
$name = $_POST['userName'];

  if(isset($from_date) && isset($to_date) && isset($name)) {
    $query = "SELECT name,date,enquiry,retail,collection,booking,evaluation,test_drive,home_visit FROM employee_details WHERE date BETWEEN '$from_date' AND '$to_date' AND name LIKE'$name';";
$search_result = filterTable($query);
       } 
  }

elseif(empty($_POST['userName']) && !empty($_POST['fromDate']) && !empty($_POST['toDate'])) {
$from_date = $_POST['fromDate'];
$to_date = $_POST['toDate'];  
$query = "SELECT name,date,enquiry,retail,collection,booking,evaluation,test_drive,home_visit FROM employee_details WHERE date BETWEEN '$from_date' AND '$to_date';";
$search_result = filterTable($query);
}

elseif(!empty($_POST['userName'])  && empty($_POST['fromDate']) && empty($_POST['toDate'])) {
$name = $_POST['userName'];  
$query = "SELECT name,date,enquiry,retail,collection,booking,evaluation,test_drive,home_visit FROM employee_details WHERE name LIKE'$name';";
$search_result = filterTable($query);
}

else 
{
$query = "SELECT name,date,enquiry,retail,collection,booking,evaluation,test_drive,home_visit FROM employee_details;";
    $search_result = filterTable($query);

}

$now = time();
if (($now - $_SESSION['start'] > 600) && (isset($_POST['submit']))){
session_destroy();
echo "Session expired.Please Login again.";
header("Location:LoginPage.php");
}

?>

<!DOCTYPE html>
<html>
    <head>
        <style>
              input,input[type='text']
       {
           border:1px solid black;
           padding:5px 5px;
       border-radius:4px;
           font-size:12px;

       }
            table {
                  font-family: 'Roboto', sans-serif;
          font-weight:400;
          font-size:16px;
                  border-collapse: collapse;
                  width: 80%;
          text-align:center;
          margin:auto;
                   }

                  td, th {
                  font-family: 'Roboto', sans-serif;
                  font-weight:400;
                  font-size:12px;
                  border: 1px solid #dddddd;
                  text-align:center;
                  padding: 5px;
                  }

                 tr:nth-child(even) {
                 background-color: #dddddd;
                 }
                 .headingstyle
                 {
                 font-family: 'Roboto', sans-serif;
                 font-weight:400;
                 font-size:14px;
                 text-align:center;
                 }
       </style>
    </head>
<body>

<div class="container;">
<h2 class="headingstyle">Sales App Data</h2>
<form action="https://pranami.000webhostapp.com/salesApp.php" method="post">


    <div class="headingstyle">

     <label class="headingstyle">From Date:</label>
    <input type="text" name="fromDate" placeholder="YYYY-MM-DD" id="datepicker">


    <label class="headingstyle" style="margin-left:20px;">To Date:</label>
    <input type="text" name="toDate" placeholder="YYYY-MM-DD" id="datepicker">


        <label class="headingstyle" style="margin-left:20px;">Name:</label>       
        <input type="text" name="userName">



    <input style="margin-left:20px; background-color:#16367F; font-family:'Roboto', sans-serif;font-weight:400;font-size:14px;color:#ffffff; padding:5px 8px; " type="submit" name="submit" value="Submit">
</div><br/><br/>
    <table>
        <tr>
            <th>Name</th>
                        <th>Date</th>
                        <th>Enquiry</th>
                        <th>Retail</th>
                        <th>Collection</th>
                        <th>Booking</th>
                        <th>Evaluation</th>
                        <th>Test Drive</th>
                        <th>Home Visit</th>
        </tr>
            <?php while($row = mysqli_fetch_array($search_result)):?>
            <tr>
                <td><?php echo $row['name'];?> </td>
                                <td><?php echo $row['date'];?> </td>
                                <td><?php echo $row['enquiry'];?> </td>
                                <td><?php echo $row['retail'];?> </td>
                                <td><?php echo $row['collection'];?> </td>
                                <td><?php echo $row['booking'];?> </td>
                                <td><?php echo $row['evaluation'];?> </td>
                                <td><?php echo $row['test_drive'];?></td>
                                <td><?php echo $row['home_visit'];?></td>
            </tr>
            <?php endwhile;?>
    </table>
    </form>
</body>
</html>

问题出在if-else部分。我有一个HTML表单,它有3个输入字段,当用户在输入字段中给出值时,单击提交按钮后,数据将从MySQL数据库中检索并显示在表格中。如果用户在所有3个字段中输入数据并单击提交按钮,则可以从数据库中正确检索数据。但我想要的是,如果用户没有为&#34; Name&#34;提供任何价值。字段,然后应根据给定的数据值检索所有数据。或者,如果用户仅为&#34;名称&#34;字段,然后应该仅检索给定Name的数据。我在PHP脚本的elseif部分提到了那些条件,但是elseif部分从未执行过。它不返回任何值。在这些情况下,表是空的。 / p>

有人可以帮我解决这个问题吗?

3 个答案:

答案 0 :(得分:5)

isset只是检查字段是否存在。它不检查该字段是否为空。您可以使用empty()检查用户是否在字段中输入内容

当您提交表单

时,文本框,文本区域等也会设置一个空值

答案 1 :(得分:0)

如果设置了一个值,但是当你试图检查它是否为真时它的值是'0',那么你应该使用empty()函数来检查它,但是如果你优化你的'if'会更好结构

if (empty($name))
如果名称为空,

将返回true

答案 2 :(得分:0)

此处无需检查isset()。因为从您的代码中,每次提交页面时,所有三个字段都会发布值。为此,只有条件总是先执行代码。因此,将isset()代码更改为empty()代码。

您的代码就像


if(isset($_POST['submit']) && isset($_POST['fromDate']) && isset($_POST['toDate']) && isset($_POST['userName']))
{
......
}
elseif(empty($_POST['userName']) && !empty($_POST['fromDate']) && !empty($_POST['toDate']))
{
......
}
elseif(!empty($_POST['userName'])  && empty($_POST['fromDate']) && empty($_POST['toDate']))
{
......
}
else
{
......
}

将您的代码更改为以下内容


if(!empty($_POST['submit']) && !empty($_POST['fromDate']) && !empty($_POST['toDate']) && !empty($_POST['userName']))
{
......
}
elseif(empty($_POST['userName']) && !empty($_POST['fromDate']) && !empty($_POST['toDate']))
{
......
}
elseif(!empty($_POST['userName'])  && empty($_POST['fromDate']) && empty($_POST['toDate']))
{
......
}
else 
{
......
}

它会起作用。希望这段代码可以帮到你。