如何检索数据和下拉列表月份

时间:2015-12-27 21:49:38

标签: php mysql database display

我正在尝试从数据库中检索数据。我正在使用下面的代码: -

<?php
  include('header.php'); //Header which contains html, head, etc 
  include('function/dbconnect.php'); //Database connection
?>

<!-- menu bar here -->

        <form method="post" class="form">

        <?php           
            $month          = mysql_real_escape_string($_POST['month']);
            $m_no           = mysql_real_escape_string($_POST['m_no']);
            $section        = mysql_real_escape_string($_POST['section']);
            $reporter       = mysql_real_escape_string($_POST['reporter']);
            $location       = mysql_real_escape_string($_POST['location']);
            $incident_date = mysql_real_escape_string($_POST['incident_date']);
            $incident_time = mysql_real_escape_string($_POST['incident_time']);
            $description    = mysql_real_escape_string($_POST['m_description']);
            $priority       = mysql_real_escape_string($_POST['priority']);
            $received_date = mysql_real_escape_string($_POST['received_date']);
            $received_time = mysql_real_escape_string($_POST['received_time']);
            $issued_to  = mysql_real_escape_string($_POST['issued_to']);
            $completed_date = mysql_real_escape_string($_POST['completed_date']);   
            $completed_time = mysql_real_escape_string($_POST['completed_time']);   
            $incident_reason= mysql_real_escape_string($_POST['incident_reason']);  
            $m_status = mysql_real_escape_string($_POST['m_status']);

            $query = mysql_query("SELECT *, (DATE_FORMAT(DATE(incident_date),'%m/%Y')) AS month
                                  FROM tbl_maintenance_form
                                  WHERE DATE_FORMAT(DATE(incident_date),'%m/%Y') LIKE '%" . $incident_date . "%' or die('Could not get data: ' . mysql_error()) ");

            $sql = mysql_query("SELECT DISTINCT(DATE_FORMAT(DATE(incident_date),'%m/%Y')) AS month
                                FROM tbl_maintenance_form
                                ORDER BY DATE_FORMAT(DATE(incident_date),'%Y'), DATE_FORMAT(DATE(incident_date),'%m')
                                ASC");
        ?>
        <div align="center">
            <select name="date" style="width:10%">
                <option selected disabled>-- Sort By Month --</option>
                <?php
                    while ($row = mysql_fetch_array($sql)) {
                        echo "<option value='" . $row['month'] . "'>" . $row['month'] . "</option>";
                    }                               
                ?>
            </select>
            <input type="submit" value="Search" name="mon" id="mon" />

        </div>
        <br />
                <table class="sortable" id='myTable' cellspacing="5" cellpadding="5" style="width:100%;">
                    <tr bgcolor="#66FF99">
                        <td align="center"><b>No.</b></td>
                        <td align="left"><b>Section</b></td>
                        <td align="left"><b>Report By</b></td>
                        <td align="left"><b>Location</b></td>
                        <td align="left"><b>Incident Date</b></td>
                        <td align="left"><b>Month</b></td>
                        <td align="left"><b>Incident Time</b></td>
                        <td align="left"><b>Description of Fault</b></td>
                        <td align="left"><b>Priority</b></td>
                        <td align="left"><b>Received Date</b></td>
                        <td align="left"><b>Received Time</b></td>
                        <td align="left"><b>Issued To</b></td>
                        <td align="left"><b>Completed Date</b></td>
                        <td align="left"><b>Completed Time</b></td>
                        <td align="left"><b>Reason of Issue</b></td>
                        <td align="left"><b>Status</b></td>
                        <td align="left"><b>Details</b></td>
                    </tr>
                    <?php
                        while($rows = mysql_fetch_array($query)) { 
                    ?>
                    <tr class="c1">
                        <td><?php echo $rows['m_no']; ?></td>
                        <td><?php echo $rows['section']; ?></td>
                        <td><?php echo $rows['reporter']; ?></td>
                        <td><?php echo $rows['location']; ?></td>
                        <td><?php echo $rows['incident_date']; ?></td>
                        <td><?php echo $rows['month']; ?></td>
                        <td><?php echo $rows['incident_time']; ?></td>
                        <td><?php echo $rows['m_description']; ?></td>
                        <td><?php echo $rows['priority']; ?></td>
                        <td><?php echo $rows['received_date']; ?></td>
                        <td><?php echo $rows['received_time']; ?></td>
                        <td><?php echo $rows['issued_to'];?></td>
                        <td><?php echo $rows['completed_date'];?></td>
                        <td><?php echo $rows['completed_time'];?></td>
                        <td><?php echo $rows['incident_reason'];?></td>
                        <td><?php echo $rows['m_status'];?></td>


                        <td align="center"><?php echo "<a href='maintenance_edit_report.php?m_no=".$rows['m_no']."'>"?>
                                           <span title="Click to view details"><i class="icon-folder-open"></i></span>
                        </td> 
                    </tr>
                    <?php   
                        } 
                    ?> 
                </table>
            <br />
        </form>
</body>
</html>

首先,它没有返回任何错误,它显示我想要的完美。不知何故,在构建系统的过程中,我得到布尔错误。那时我意识到$query中存在一些错误。因此,我从WHERE DATE_FORMAT(DATE(incident_date),'%m-%Y') LIKE '%" . $incident_date . "%' or die('Could not get data: ' . mysql_error())删除了$query。但是,系统无法检索月份。

我正在尝试做什么/我的愿望输出如下: -

  1. 我错过了月份列 - 列中的值= events_date的月份列表。
  2. enter image description here

    1. 假设有下拉列表根据所选月份显示数据,但没有什么可以选择的。下拉列表中的值= incident_date的月份年份的不同列表。
    2. enter image description here

      enter image description here

      如何检索和显示数据并制作显示月份和年份的下拉列表。?

0 个答案:

没有答案