如果某些列数据与原始查询不准确,我该如何阻止显示这些数据?

How can I stop certain column data from showing if it's not accurate to the original query?

有人可以帮我处理 post 以及查询和回显吗?记录有多个不同的日期,我想搜索 6 个不同日期中的任何一个在 2 个不同日期之间。在我从表单搜索并且 post 发生后,这会正确显示所有不同的记录,其中 6 个日期之一位于表单中选择的 2 个日期之间。但它显示了记录的所有日期。我只希望它显示实际介于所选 2 个日期之间的日期。

<?php
    if (isset($_POST['search'])) {
        $fromdate = mysql_real_escape_string(htmlspecialchars($_POST['fromdate']));
        $todate = mysql_real_escape_string(htmlspecialchars($_POST['todate']));

        echo "<h2>Search Result</h2>";
        echo "<p><strong>From:</strong> ".$fromdate." <strong>To:</strong> ".$todate."</p>"; //test

        $query = mysql_query("
SELECT * 
 FROM jobs j 
 WHERE dropdate1 BETWEEN '$fromdate' AND '$todate' 
    OR dropdate2 BETWEEN '$fromdate' AND '$todate'
   OR dropdate3 BETWEEN '$fromdate' AND '$todate'
   OR dropdate4 BETWEEN '$fromdate' AND '$todate' 
   OR dropdate5 BETWEEN '$fromdate' AND '$todate'
   OR dropdate6 BETWEEN '$fromdate' AND '$todate';"
);

        while($row = mysql_fetch_array($query)) {
            $dropdate1 = $row['dropdate1'];
            $dropdate2 = $row['dropdate2'];
            $dropdate3 = $row['dropdate3'];
            $dropdate4 = $row['dropdate4'];
            $dropdate5 = $row['dropdate5'];
            $dropdate6 = $row['dropdate6'];

            echo "<strong>".$row['id'].":</strong> ".$row['clientname']."<br>";

            if ($dropdate1 !== "") {
                echo "<strong>Drop 1:</strong> ".$row['dropdate1']."<br>";
            }
            if ($dropdate2 !== "") {
                echo "<strong>Drop 2:</strong> ".$row['dropdate2']."<br>";
            }
            if ($dropdate3 !== "") {
                echo "<strong>Drop 3:</strong> ".$row['dropdate3']."<br>";
            }
            if ($dropdate4 !== "") {
                echo "<strong>Drop 4:</strong> ".$row['dropdate4']."<br>";
            }
            if ($dropdate5 !== "") {
                echo "<strong>Drop 5:</strong> ".$row['dropdate5']."<br>";
            }
            if ($dropdate6 !== "") {
                echo "<strong>Drop 6:</strong> ".$row['dropdate6']."<br>";
            }

            echo "<br>";
        }
    }
    ?>

这是一些示例结果。

Search Result

From: 04-01-2016 To: 04-08-2016

1: blah1
Drop 1: 04-03-2015

2: blah2
Drop 1: 03-17-2015
Drop 2: 03-23-2015
Drop 3: 03-30-2015
Drop 4: 04-06-2015

3: blah3
Drop 1: 04-05-2015
Drop 2: 04-05-2015

4: blah4
Drop 1: 03-31-2015
Drop 2: 04-07-2015

5: blah5
Drop 1: 04-05-2015

所以我的 if 语句不显示空日期,但现在我想更进一步,不显示不在两个搜索日期之间的日期。

这就是我想要的搜索结果:

Search Result

From: 04-01-2016 To: 04-08-2016

1: blah1
Drop 1: 04-03-2015

2: blah2
Drop 4: 04-06-2015

3: blah3
Drop 1: 04-05-2015
Drop 2: 04-05-2015

4: blah4
Drop 2: 04-07-2015

5: blah5
Drop 1: 04-05-2015

试试这个:

<?php
if (isset($_POST['search'])) {
    $fromdate = mysql_real_escape_string(htmlspecialchars($_POST['fromdate']));
    $todate = mysql_real_escape_string(htmlspecialchars($_POST['todate']));

    echo "<h2>Search Result</h2>";
    echo "<p><strong>From:</strong> ".$fromdate." <strong>To:</strong> ".$todate."</p>"; //test

    $query = mysql_query("SELECT * FROM jobs WHERE dropdate1 BETWEEN '".$fromdate."' AND '".$todate."' OR dropdate2 BETWEEN '".$fromdate."' AND '".$todate."' OR dropdate3 BETWEEN '".$fromdate."' AND '".$todate."' OR dropdate4 BETWEEN '".$fromdate."' AND '".$todate."' OR dropdate5 BETWEEN '".$fromdate."' AND '".$todate."' OR dropdate6 BETWEEN '".$fromdate."' AND '".$todate."'");
    $fromDate1 = new DateTime($_POST['fromdate']);
    $toDate1 = new DateTime($_POST['todate']);
    while($row = mysql_fetch_array($query)) {
        $dropdate1 = new DateTime($row['dropdate1']);
        $dropdate2 = new DateTime($row['dropdate2']);
        $dropdate3 = new DateTime($row['dropdate3']);
        $dropdate4 = new DateTime($row['dropdate4']);
        $dropdate5 = new DateTime($row['dropdate5']);
        $dropdate6 = new DateTime($row['dropdate6']);

        echo "<strong>".$row['id'].":</strong> ".$row['clientname']."<br>";

        if ($dropdate1 !== "" && $dropdate1 >= $fromDate1 && $dropDate1 <= $toDate1) {
            echo "<strong>Drop 1:</strong> ".$row['dropdate1']."<br>";
        }
        if ($dropdate2 !== "" && $dropdate2 >= $fromDate1 && $dropDate1 <= $toDate1) {
            echo "<strong>Drop 2:</strong> ".$row['dropdate2']."<br>";
        }
        if ($dropdate3 !== "" && $dropdate3 >= $fromDate1 && $dropDate1 <= $toDate1) {
            echo "<strong>Drop 3:</strong> ".$row['dropdate3']."<br>";
        }
        if ($dropdate4 !== "" && $dropdate4 >= $fromDate1 && $dropDate1 <= $toDate1) {
            echo "<strong>Drop 4:</strong> ".$row['dropdate4']."<br>";
        }
        if ($dropdate5 !== "" && $dropdate5 >= $fromDate1 && $dropDate1 <= $toDate1) {
            echo "<strong>Drop 5:</strong> ".$row['dropdate5']."<br>";
        }
        if ($dropdate6 !== "" && $dropdate6 >= $fromDate1 && $dropDate1 <= $toDate1) {
            echo "<strong>Drop 6:</strong> ".$row['dropdate6']."<br>";
        }

        echo "<br>";
    }
}

这应该有效。

又一次修改。我觉得这个还行。

<?php
    if (isset($_POST['search'])) {
        $fromdate = mysql_real_escape_string(htmlspecialchars($_POST['fromdate']));
        $todate = mysql_real_escape_string(htmlspecialchars($_POST['todate']));

        list($fmonth, $fday, $fyear) = split('[/.-]', $fromdate);
        list($tmonth, $tday, $tyear) = split('[/.-]', $todate);

        $f = mysql_real_escape_string("$fmonth-$fday-$fyear");
        $t = mysql_real_escape_string("$tmonth-$tday-$tyear");

        if ($fromdate == "" || $todate == "") {
            echo "<div class='alert alert-danger match-lh30'>From or To cannot be empty! Try again.</div>";
        }

        else {

            echo "<h2>Search Result</h2>";
            echo "<p><strong>From:</strong> ".$f." <strong>To:</strong> ".$t."</p>";

            $query = mysql_query("SELECT id, clientname, dropdate1, dropdate2, dropdate3, dropdate4, dropdate5, dropdate6 FROM `jobs` WHERE 
            STR_TO_DATE(`dropdate1`,'%m-%d-%Y') BETWEEN STR_TO_DATE('".$f."', '%m-%d-%Y') AND STR_TO_DATE('".$t."', '%m-%d-%Y') 
            OR STR_TO_DATE(`dropdate2`,'%m-%d-%Y') BETWEEN STR_TO_DATE('".$f."', '%m-%d-%Y') AND STR_TO_DATE('".$t."', '%m-%d-%Y') 
            OR STR_TO_DATE(`dropdate3`,'%m-%d-%Y') BETWEEN STR_TO_DATE('".$f."', '%m-%d-%Y') AND STR_TO_DATE('".$t."', '%m-%d-%Y') 
            OR STR_TO_DATE(`dropdate4`,'%m-%d-%Y') BETWEEN STR_TO_DATE('".$f."', '%m-%d-%Y') AND STR_TO_DATE('".$t."', '%m-%d-%Y') 
            OR STR_TO_DATE(`dropdate5`,'%m-%d-%Y') BETWEEN STR_TO_DATE('".$f."', '%m-%d-%Y') AND STR_TO_DATE('".$t."', '%m-%d-%Y') 
            OR STR_TO_DATE(`dropdate6`,'%m-%d-%Y') BETWEEN STR_TO_DATE('".$f."', '%m-%d-%Y') AND STR_TO_DATE('".$t."', '%m-%d-%Y')");

            $num_rows = mysql_num_rows($query);
            echo "<p><strong>".$num_rows."</strong> records found.</p>";

            while($row = mysql_fetch_array($query)) {
                $id = $row['id'];
                $clientname = $row['clientname'];
                $dropdate1 = $row['dropdate1'];
                $dropdate2 = $row['dropdate2'];
                $dropdate3 = $row['dropdate3'];
                $dropdate4 = $row['dropdate4'];
                $dropdate5 = $row['dropdate5'];
                $dropdate6 = $row['dropdate6'];

                if (($dropdate1 !== "" && $dropdate1 >= $f && $dropdate1 <= $t) || 
                ($dropdate2 !== "" && $dropdate2 >= $f && $dropdate2 <= $t) || 
                ($dropdate3 !== "" && $dropdate3 >= $f && $dropdate3 <= $t) || 
                ($dropdate4 !== "" && $dropdate4 >= $f && $dropdate4 <= $t) || 
                ($dropdate5 !== "" && $dropdate5 >= $f && $dropdate5 <= $t) || 
                ($dropdate6 !== "" && $dropdate6 >= $f && $dropdate6 <= $t)) {
                    echo "<div class='row-fluid no-bottom-margin somepaddingtop somepaddingbottom' style='border-bottom: 1px solid #ccc;'>";

                        echo "<div class='span4' style='min-height: 0;'>";
                            echo "<strong>".$id.":</strong> ";
                            echo $clientname;
                        echo "</div>";

                        echo "<div class='span8' style='min-height: 0;'>";
                            if ($dropdate1 !== "" && $dropdate1 >= $f && $dropdate1 <= $t) {
                                echo "<strong>Drop 1:</strong> ".$dropdate1." ";
                            }
                            if ($dropdate2 !== "" && $dropdate2 >= $f && $dropdate2 <= $t) {
                                echo "<strong>Drop 2:</strong> ".$dropdate2." ";
                            }
                            if ($dropdate3 !== "" && $dropdate3 >= $f && $dropdate3 <= $t) {
                                echo "<strong>Drop 3:</strong> ".$dropdate3." ";
                            }
                            if ($dropdate4 !== "" && $dropdate4 >= $f && $dropdate4 <= $t) {
                                echo "<strong>Drop 4:</strong> ".$dropdate4." ";
                            }
                            if ($dropdate5 !== "" && $dropdate5 >= $f && $dropdate5 <= $t) {
                                echo "<strong>Drop 5:</strong> ".$dropdate5." ";
                            }
                            if ($dropdate6 !== "" && $dropdate6 >= $f && $dropdate6 <= $t) {
                                echo "<strong>Drop 6:</strong> ".$dropdate6." ";
                            }
                        echo "</div>";

                    echo "</div>";
                }

            }

        }

    }
    ?>