-1

请帮助我,我想通过下拉列表按月份搜索员工的数据,如 1 月、2 月等,日期字段为日期类型 0000-00-00。如何在 php 中搜索此类数据谢谢..

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

...

4

1 回答 1

0

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

   <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>

进程.php

“用户名”和“密码”以及用于连接数据库的数据库用户名和密码

<?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();
}
?>
于 2013-08-12T08:44:14.493 回答