我试图使搜索栏通过数据库并返回一个表。表单中的值已存储,但php代码未返回任何信息数组。
这是代码:
<?php
header("Location:http://127.0.0.1:58199/graduateYearPage.html");
if (isset($_GET['uni_name']) && is_string($_GET['uni_name'])) {
$uname = $_GET['uni_name'];
} else {
$uname = "";
}
$con = new mysqli('127.0.0.1', 'root', 'tR60135400', 'db');
if ($con->connect_errno) {
echo "We are experiencing problems.";
exit;
}
$sql = "SELECT * FROM students WHERE university = $uname";
if (!$result = $con->query($sql)) {
echo "Sorry, the website is experiencing problems.";
exit;
}
if ($result->num_rows === 0) {
echo "We could not find a match for ID $uname, sorry about that. Please try again.";
exit;
} else {
//OUTPUT A TABLE
echo "<table> <ul>"
while($row = $result->fetch_assoc()){
printf ("<li>%s</li>", $row["firstName"]);
printf ("<li>%s</li>", $row["lastName"]);
printf ("<li>%s</li>", $row["university"]);
}
echo "</ul> </table>"
}
$result->free();
$con->close();
exit;
?>
完成此工作后,这将是Bootstrap模式中的弹出消息。该表不起作用。我还认为这可能是因为表单以及我如何称呼php代码。
这是表单的html代码:
<form method="GET" id="search_bar" style="text-align: center;">
<div class="input-group">
<input id="placeholder" type="search" class="form-control" name="uni_name" placeholder=" Enter a university name.">
<div class="input-group-btn">
<button class="btn btn-default" type="submit" name="submit">
<i class="glyphicon glyphicon-search"></i>
</button>
</div>
</div>
</form>
最佳答案
尝试这个:
index.html或index.php
<form method="GET" id="search_bar" action="search_bar.php" style="text-align: center;">
<div class="input-group">
<input id="placeholder" type="search" class="form-control" name="uni_name" placeholder=" Enter a university name.">
<div class="input-group-btn">
<button class="btn btn-default" type="submit" name="submit">
<i class="glyphicon glyphicon-search"></i>
</button>
</div>
</div>
</form>
search_bar.php
<?php
if($_SERVER["REQUEST_METHOD"]="GET"){
if(isset($_GET["uni_name"])){
$uname = $_GET['uni_name'];
}else{
$uname = "";
}
}
$con = new mysqli('127.0.0.1', 'root', 'tR60135400', 'db');
if ($con->connect_errno) {
echo "We are experiencing problems.";
exit;
}
$sql = "SELECT * FROM students WHERE university = '$uname'";
if (!$result = $con->query($sql)) {
echo "Sorry, the website is experiencing problems.";
exit;
}
if ($result->num_rows === 0) {
echo "We could not find a match for ID $uname, sorry about that. Please try again.";
exit;
} else {
//OUTPUT A TABLE
echo "<table> <ul>"
while($row = $result->fetch_assoc()){
printf ("<li>%s</li>", $row["firstName"]);
printf ("<li>%s</li>", $row["lastName"]);
printf ("<li>%s</li>", $row["university"]);
}
echo "</ul> </table>"
}
$result->free();
$con->close();
exit;
?>
关于php - HTML搜索栏值未在php代码中处理,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41458226/