如何通过php中的下拉菜单按月搜索数据库


How can search the database by month through drop down in php

请帮帮我我想通过下拉列表按月份(如1月、2月等)搜索员工的数据日期字段是日期类型0000-00-00.我如何在php中搜索这种类型的数据非常感谢。

<code>
<select name="month" id="month">
<option>
</option>
<option>
January
</option>
<option>
February
</option>
<option>
March

您首先需要为表单选项提供一些月份编号的值。这将由您的数据库进行查询。您的操作可以是您想要显示数据的页面

   <form method="post" action="process.php">
    <select name="month" id="month">
        <option></option>
        <option value="1">January</option>
        <option value="2">February</option>
        <option value="3">March</option>
    </select>
   </form>

process.php

"用户名"answers"密码"以及用于连接到数据库的数据库用户名和密码

<?php
// Make a MySQL Connection
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("database_name") or die(mysql_error());
$date = mysql_real_escape_string($_POST['month']);
// Retrieve all the data from the "example" table
//set date_column to the date field in your database
$sql ="SELECT * FROM table_name WHERE MONTH(date_column) = $date";
$result = mysql_query($sql)
or die(mysql_error());  
// store the record of the "example" table into $row
while($row = mysql_fetch_array($result)){
    // Print out the contents of the entry 
    extract($row, EXTR_PREFIX_INVALID, '_');
    //change these to whatever you want to display
    echo "Name: ".$row['column1'];
    echo " Age: ".$row['column2'];
}
?>

编辑:我已经为你重写了它,我也测试过它。这是假设您正在使用MySQLi

这使用了预先准备好的语句,使其更加安全。查看此处了解更多信息http://mattbango.com/notebook/code/prepared-statements-in-php-and-mysqli/

在SELECT语句中,您可以放置您想要的任何表,但其他表应该可以使用。

<html>
<head>
</head>
<body>
 <form method="post" action="testing.php">
    <select name="month" id="month">
        <option></option>
        <option value="1">January</option>
        <option value="2">February</option>
        <option value="3">March</option>
        <option value="4">April</option>
        <option value="5">May</option>
        <option value="6">June</option>
        <option value="7">July</option>
        <option value="8">August</option>
        <option value="9">September</option>
        <option value="10">October</option>
        <option value="11">November</option>
        <option value="12">December</option>
    </select>
    <input type="submit" value="submit" name="submit"/>
   </form>
</body>
</html>
<?php
// Include connection file here and replace these variables
$hostname="localhost";
$username="username";
$password="password";
$dbname="dbasename";
 /* Create a new mysqli object with database connection parameters */
$db = new mysqli($hostname, $username, $password, $dbname);
// Create statement object
$stmt = $db->stmt_init();
// Create a prepared statement
if($stmt->prepare("SELECT name, month, blah FROM test WHERE MONTH(month) = ?")) {
    // Bind your variable to replace the ?
    if(!$stmt->bind_param('s', $month)) {
        //if BIND fails, display an error
        printf("Errormessage: %s'n", $stmt->error);
    }
    // escape the POST data for added protection
    $month = isset($_POST['month'])
          ? $db->real_escape_string($_POST['month'])
          : '';
    // Execute query
    if(!$stmt->execute()) {
        printf("Errormessage: %s'n", $stmt->error);
    }
    // Bind your result columns to variables
    if(!$stmt->bind_result($name, $url, $logo)) {
        printf("Errormessage: %s'n", $stmt->error);
    }
    // Fetch the result of the query
    while($stmt->fetch()) {
        echo $name;
    }
    // Close statement object
    $stmt->close();
}
?>