【发布时间】:2014-09-21 07:31:47
【问题描述】:
我正在尝试使用 3 个文本字段和一个提交按钮创建一个可以接受 3 种不同类型的查询请求的 html 表单。当该人在其中一个字段中输入数据并单击提交按钮时,它将根据输入查询数据库。我可以用一个文本框来做,但我不知道如何做 3 个文本框。此外,此人一次只能选择查询一个文本字段。
字段 1 是 student_id 字段 2 是 last_name 字段 3 是考试日期。
个人可以输入 student_id,并且只能输入 student_id,或者 last_name 并且只能输入姓氏,或者可以输入examDate 并且只能输入考试日期。
我遇到的一个问题是如何将 3 种不同类型的数据传递给 PHP 并在 select 语句中进行查询。看来我必须创建 3 个不同的选择查询语句,但是如何创建代码来识别要运行的查询?
这是我目前所拥有的:
<!DOCTYPE html>
<div id="form_wrap"><!-- start form wrap -->
<div id="form_header">
</div>
<div id="form_body">
<p>Search for a certification request (Enter one of the following):</p>
<form action="search.php" method="post" name="information" id="information"
onsubmit="return(validate())">
<div class="field">
<label for = "Student_id"> Student ID:</label>
<input type="text" name="search" id="search" />
</div>
<div class="field">
<label for = "last_name"> Last Name:</label>
<input type="text" name="last" id="last" />
</div>
<div class="field">
<label for = "examDate"> Exam Date:</label>
<input type="text" name="date" id="mm/dd/yyyy" />
<input type="submit" value="Search" />
</form>
</div>
</div>
这是php代码
<?php
require 'security.php';
error_reporting(E_ALL);
ini_set('display_errors', '1');
// Otherwise we connect to our Database
$db = new mysqli('localhost', 'root', '', 'corpmis_ddempsey');
//check connection
if($db->connect_errno) {
die('sorry, we are having some problems');
}else {
echo 'connected';
}
$search = $_REQUEST['search'];
//If they did not enter a search term we give them an error
if ($search == "")
{
echo "<p>You forgot to enter a search term!!!";
exit;
}
// We perform a bit of filtering
//$search = strtoupper($search);
$search = strip_tags($search);
$search = trim ($search);
//Now we search for our search term, in the field the user specified
//$result = $db->query("SELECT * FROM records WHERE student_id LIKE '$search'");
/*
");
*/
//And we display the results
if($result = $db->query("SELECT * FROM records WHERE student_id LIKE '$search'" )){
if($result->num_rows){
while($row = $result->fetch_object()){
$records[] = $row;
}
$result->free();
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Sample</title>
</head>
<body>
<h3>People</h3>
<?php
if(!count($records)) {
echo 'No records';
} else {
?>
<table>
<thead>
<tr>
<th>student_id</th>
<th>First name</th>
<th>Last name</th>
<th>email</th>
<th>Major</th>
<th>Exam Name</th>
<th>Taken class</th>
<th>Prepare</th>
<th>MeasureUp Key</th>
<th>Exam Date</th>
<th>Request Made On</th>
</tr>
</thead>
<tbody>
<?php
foreach($records as $r){
?>
<tr>
<td><?php echo $r->student_id; ?></td>
<td><?php echo $r->first_name; ?></td>
<td><?php echo $r->last_name; ?></td>
<td><?php echo $r->email; ?></td>
<td><?php echo $r->major; ?></td>
<td><?php echo $r->examName?></td>
<td><?php echo $r->taken_class; ?></td>
<td><?php echo $r->prepare; ?></td>
<td><?php echo $r->MeasureUpKey; ?></td>
<td><?php echo $r->examDate; ?></td>
<td><?php echo $r->request_made; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
}
?>
【问题讨论】:
-
你可以在 where 子句中使用多个列,只需搜索即可,
where col1 = 2 and col2 = whatever ... -
你可以轻松地做你想做的事。
标签: javascript php html mysqli