创建表单以在数据库中搜索特定标题

时间:2016-05-29 03:57:27

标签: php html sql database forms

我正在尝试创建一个表单,以便我可以在数据库中搜索特定的标题,但每当我搜索没有任何反应时,我不确定如何连接到数据库。以下是该应用的链接:https://web-seanakiyama.c9users.io/Week%2011/Index3.php。有人可以帮我解决这个问题,谢谢!

<!DOCTYPE html>

require_once “week11.php”;

$search = "TITLE";
getGames($search);


<?php
$data = [
        ["TITLE" => "Counterstrike: Global Offensive", "DEVELOPER" => "Valve", "PUBLISHER" => "Valve", "PRICE" => 19.99, "PLATFORM" => "PC"],
        ["TITLE" => "Final Fantasy XV", "DEVELOPER" => "Square Enix", "PUBLISHER" => "Square Enix", "PRICE" => 99.99, "PLATFORM" => "PS4"],
        ["TITLE" => "Halo", "DEVELOPER" => "Bungie", "PUBLISHER" => "Microsoft", "PRICE" => 49.99, "PLATFORM" => "Xbox"],
        ["TITLE" => "Battlefield 4", "DEVELOPER" => "EA Digital Illusions", "PUBLISHER" => "EA", "PRICE" => 79.99, "PLATFORM" => "PC"]
]
?>

<html>
<head>
    <link type='text/css' rel='stylesheet' href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"/>
</head>
<body>
    <div>
        <div class="jumbotron">                
            <div class="container">
                <h1>Games</h1>
            <div class="container">
                <div id="Search"</div>
                    <form>
                        <form class="form-inline">
                    <form action="index.php" method="get">
                    <fieldset>
                        <label>Title</label>
                        <input type="text" class="form-control" id="title" name="title"/>
                        <input type="submit" value="Search" class="btn btn-default"/>
                    </fieldset>
                    </form>
            <div class="container">
                <div id="data"</div>
                <table>
                    <table class="table">
                        <thead>
                            <th>Game Title</th>
                            <th>Developer</th>
                            <th>Publisher</th>
                            <th>Price</th>
                            <th>Platform</th>
                        </thead>
                <?php
                 foreach($data as $data)
                {
                    echo"<tr>";
                    echo"<td>".$data["TITLE"]."</td>";
                    echo"<td>".$data["DEVELOPER"]."</td>";
                    echo"<td>".$data["PUBLISHER"]."</td>";
                    echo"<td>".$data["PRICE"]."</td>";
                    echo"<td>".$data["PLATFORM"]."</td>";
                    echo"</tr>";
                }
                ?>
                </table>
    </div>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

我只是在编写一个简单的代码,其中有一个带有一个名为title的输入的表单,并且在提交此表单时将查看该表并显示匹配的搜索,如果在数据库中找到匹配的内容,希望它会给你一些关于在表格或数据库中搜索。

首先,我在searchform.html文件中创建一个表单:

<!DOCTYPE html>
<head>

<--include your bootstrap here-->

</head>

<body>

<form name='search' class='form-horizontal' method='post'  action='search.php'>

    <div class='form-group'>

        <div class='col-sm-2'>
            <label for='title' class = 'control-label'>enter title to search</label>
        </div>

        <div class='col-sm-8'>
            <input type='text' name='title' id='title'>
        </div>

    </div>

    <input type='submit' name='search'  class='btn btn-info' value='search'>`

</form>

</body>

</html>

在此之后我们将在phpmyadmin中创建一个名为'testsearch'的数据库,然后创建一个名为search的表,其中有两列为'id'和'title',其中我将是整数,title将是varchar。

现在我们将创建一个名为search.php的新文件,该文件将在提交表单时运行,我们将在其中添加下面编写的代码。

<?php

//get the posted values
$submit = $_POST['search'];
$title = $_POST['title'];

//if submitted then rurun the code

if ($submit) {
    //create a database connection
    mysql_connect('localhost', 'root', ' ');
    mysql_select_db('testsearch');
    $query = "SELECT * FROM search WHERE 'title' = $title ";
    $result = mysql_query($query);

    if(mysql_num_rows($result) > 0){
        echo 'search result is';

        foreach (mysql_fetch_array($result) as $row) {
            echo $row['title']; //print result
        }
    } else {
        echo 'no match found';
    }
}

注意:不要忘记在数据库中插入值。要检查它,请在表格搜索中插入一些值,然后在浏览器中运行表格文件并输入与您在表格中插入的标题相同的标题,它将显示相应的结果。

答案 1 :(得分:0)

   require_once “week11.php”;

    $search = "TITLE";
    getGames($search); 
<?php

PHP标签应位于顶部,因为您使用php标签包含文件

<?php
require_once “week11.php”;

$search = "TITLE";
getGames($search);
相关问题