请帮帮我 我想通过下拉列表逐月搜索员工的数据,如1月,2月等 和date字段是date type 0000-00-00。如何在php中搜索这种类型的数据 谢谢..
<code>
<select name="month" id="month">
<option>
</option>
<option>
January
</option>
<option>
February
</option>
<option>
March
...
答案 0 :(得分: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>
process.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();
}
?>