PHP代码中没有处理Html搜索栏值

时间:2017-01-04 07:18:14

标签: php html mysql html-form

我正在尝试创建一个遍历数据库的搜索栏并返回一个表。存储表单中的值但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>

2 个答案:

答案 0 :(得分:0)

试试这个:

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

答案 1 :(得分:0)

在Bootstrap模式中你应该在模态开启器的href标签中更改这个标签&#34; data-target =&#34;或删除它,因为它意味着模态视图模式是打开模态视图还是重定向到页面。 另外我在你的搜索查询中注意到一个字符串应该是单引号

SELECT * FROM students WHERE university = '$uname'