简体   繁体   English

PHP使用日期范围显示数据

[英]PHP show data using date range

Here it's, I've got the answer. 在这里,我已经找到了答案。

I just change in track_reports.php to be : 我只是将track_reports.php更改为:

if (isset($_POST['chkOtdate']))
                    {
                        $date_from = $_POST['date_from'];
                        $date_to = $_POST['date_to'];
                        if (empty($bagianWhere))
                        {
                            $bagianWhere .= "ot_date between '$date_from' and '$date_to'";
                        }
                        else
                        {
                            $bagianWhere .= " AND ot_date between '$date_from' and '$date_to'";
                        }
                    }

and now nothing problem the code can be run smoothly. 现在没有问题,代码可以平稳运行。 Thanks for your helps friends. 感谢您对朋友的帮助。 Appreciate with your suggestions and comments. 感谢您的建议和意见。


I have a trouble with my code below : 我在下面的代码中遇到了麻烦:

  1. view_report.php view_report.php

We can view report based on badge_id, employee_name and ot_date (with a range). 我们可以基于badge_id,employee_name和ot_date(带有范围)查看报告。 When I try to find using badge_id and employee_name no problem, data showed. 当我尝试找到使用badge_id和employee_name没问题时,显示了数据。 But when I try to find using date range, error found. 但是,当我尝试使用日期范围查找时,发现了错误。 the error is : : mysql_fetch_array() expects parameter 1 to be resource, boolean given in on line WarningC:\\xampp\\htdocs\\siix_dev\\overtime\\track_reports.php327 错误是:: : mysql_fetch_array() expects parameter 1 to be resource, boolean given in on line WarningC:\\xampp\\htdocs\\siix_dev\\overtime\\track_reports.php327

<form method="post" action="track_reports.php" name="form">
                    <table id="mytable">
                        <tr>
                            <td>
                                <input type="checkbox" name="chkBadge" onClick="apply(this.checked, 'textBox')"> <font class="category">Badge ID</font>
                            </td>

                            <td>
                                <input id="textBox" class="text sign" type="text" name="badge_id">
                            </td>
                        </tr>

                        <tr>
                            <td>
                                <input type="checkbox" name="chkEmp" onClick="apply(this.checked, 'textBox2')"> <font class="category">Employee Name</font>
                            </td>

                            <td>
                                <input id="textBox2" class="text sign" type="text" name="employee_name">
                            </td>
                        </tr>

                        <tr>
                            <td>
                                <input id="myCheckBox" type="checkbox" name="chkOtdate" onClick="apply(this.checked, 'textBox3')" onChange="apply(this.checked, 'textBox4')"> <font class="category">OT Date</font>
                            </td>

                            <td>
                                <font class="category">From</font> <input id="textBox3" class="text sign" type="text" name="date_from" ><font class="category"> To</font> <input id="textBox4" class="text sign" type="text" name="date_to" >
                            </td>
                        </tr>

                        <tr>
                            <td></td>
                            <td>
                                <input type="submit" name="submit" value="Submit">
                            </td>
                        </tr>
                    </table>
                </form>
  1. track_reports.php track_reports.php

      $bagianWhere = ""; if (isset($_POST['chkBadge'])) { $badge_id = $_POST['badge_id']; if (empty($bagianWhere)) { $bagianWhere .= "badge_id = '$badge_id'"; } } if (isset($_POST['chkEmp'])) { $employee_name = $_POST['employee_name']; if (empty($bagianWhere)) { $bagianWhere .= "employee_name LIKE '$employee_name'"; } else { $bagianWhere .= " AND employee_name LIKE '$employee_name'"; } } if (isset($_POST['chkOtdate'])) { $date_from = $_POST['date_from']; $date_to = $_POST['date_to']; $query=mysql_query("select badge_id, employee_name from t_submissions where ot_date between '$date_from' and '$date_to'"); while($row=mysql_fetch_array($query)){ echo $row['badge_id']; echo $row['employee_name']; } } $query = "SELECT * FROM t_submissions WHERE ".$bagianWhere; $hasil = mysql_query($query); echo " <div id='main' class='wrapper'> <div class='content-area'> <table cellspacing='0' class='font'>"; echo "<tr><th class='th'>Badge ID</th><th class='th'>Employee Name</th><th class='th'>OT Date</th><th class='th'>Department</th><th class='th'>OT From</th><th class='th'>OT To</th><th class='th'>Remarks</th><th class='th'>Submissions By</th><th class='th'>Acknowledged By</th><th class='th'>Approved By</th></tr>"; while ($data = mysql_fetch_array($hasil)) { echo "<tr><td class='td'>".$data['badge_id']."</td><td class='td'>".$data['employee_name']."</td><td class='td'>".$data['ot_date']."</td><td class='td'>".$data['dept_name']."</td><td class='td'>".$data['ot_from']."</td><td class='td'>".$data['ot_to']."</td><td class='td'>".$data['remarks']."</td><td class='td'>".$data['submission_by']."</td></tr>"; } echo "</table> <br> <input type='button' VALUE='Back' onClick='history.go(-1);return true;'> </div> </div> "; ?> 

Someone can help me ? 有人可以帮助我吗? Appreciate with your helps. 感谢您的帮助。

Make sure the datatype of ot_date is datetime or not ? 确保ot_date的数据类型为datetime还是不是? perhaps you used varchar 也许你用过varchar

In your comments above, you state that the ot_date column in which you are storing your "dates" has type VARCHAR . 在上面的评论中,您声明要在其中存储“日期”的ot_date列具有VARCHAR类型。 Therefore MySQL doesn't recognise its contents as dates at all, but rather treats them like a sequence of meaningless (to it) characters (such as a word). 因此,MySQL根本不会将其内容识别为日期,而是将它们视为无意义的字符序列(例如单词)对待。

The best thing you could do is to alter your database so that such dates are stored in a format that MySQL recognises as a date, like the DATE data type. 最好的办法是更改数据库,以便以MySQL可以识别为日期的格式(例如DATE数据类型)存储此类日期。 To do this without losing your existing data: 为此,而不会丢失现有数据:

  1. Rename the existing column and add a new one in its place: 重命名现有列,并在其位置添加一个新列:

     ALTER TABLE t_submissions CHANGE ot_date ot_date_old VARCHAR(size), ADD ot_date DATE [NOT NULL] AFTER ot_date_old; 
  2. Copy the contents of the old column into the new one, as dates: 将旧列的内容复制为新列,作为日期:

     UPDATE t_submissions SET ot_date = STR_TO_DATE(ot_date_old, '%d-%b-%Y'); 
  3. Verify that the new column contains the expected data: 验证新列是否包含预期的数据:

     SELECT ot_date, ot_date_old FROM t_submissions; 
  4. Delete the old column (optional): 删除旧列(可选):

     ALTER TABLE t_submissions DROP ot_date_old; 

Next, to make your query work, MySQL needs to know how to convert the submitted dates into dates rather than treating them as strings: 接下来,为了使查询正常工作,MySQL需要知道如何将提交的日期转换为日期,而不是将它们视为字符串:

SELECT badge_id, employee_name FROM t_submissions
WHERE ot_date BETWEEN STR_TO_DATE('%d-%b-%Y', ?) AND STR_TO_DATE('%d-%b-%Y', ?)

Note that I have used ? 注意我用过? in place of the date variables because, as mentioned in my comments above, your code is currently subject to SQL injection and you really should be passing such variables to MySQL as parameters to a prepared statement (as such, they wouldn't get evaluated for SQL). 代替日期变量,因为,正如我在上面的评论中提到的那样,您的代码当前需要进行SQL注入,并且您确实应该将此类变量作为参数传递给MySQL到准备好的语句中(因此,它们不会被评估为SQL)。 Read about Bobby Tables for more information. 阅读有关Bobby Tables的更多信息。

Also note that your current form requires the user to enter the dates into the text boxes in the specified format, or else the query will fail. 还要注意,您当前的表单要求用户以指定的格式在文本框中输入日期,否则查询将失败。 You would be better off requiring the user to select date parts from drop-downs (or some form of calendar control) and/or perform validation in order to ensure that submitted dates are in the correct format. 您最好要求用户从下拉菜单(或某种形式的日历控件)中选择日期部分和/或执行验证,以确保提交的日期格式正确。

Finally, you should test the result of your query to see if it succeeded before attempting to read a recordset that might not exist (as indeed it didn't in this case). 最后,在尝试读取可能不存在的记录集之前,应该测试查询的结果以查看其是否成功(因为在这种情况下实际上并不存在)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM