【问题标题】:Creating a form to search the database for a specific title创建表单以在数据库中搜索特定标题
【发布时间】:2016-05-29 03:57:27
【问题描述】:

我正在尝试创建一个表单,以便可以在数据库中搜索特定标题,但是每当我搜索时什么都没有发生,我不确定如何连接到数据库。这是应用程序的链接: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>

【问题讨论】:

    标签: php html sql database forms


    【解决方案1】:
       require_once “week11.php”;
    
        $search = "TITLE";
        getGames($search); 
    <?php
    

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

    <?php
    require_once “week11.php”;
    
    $search = "TITLE";
    getGames($search);
    

    【讨论】:

      【解决方案2】:

      我只是在编写一个简单的代码,它有一个带有一个名为 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”,其中 I 为整数,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';
          }
      }
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-26
        • 1970-01-01
        • 1970-01-01
        • 2017-04-23
        • 2020-06-18
        • 1970-01-01
        • 2010-09-09
        • 1970-01-01
        相关资源
        最近更新 更多