2016-05-29 104 views
0

我試圖創建一個表單,因此我可以搜索特定標題的數據庫,但每當我搜索時什麼也沒有發生,我不知道如何連接到數據庫。以下是該應用的鏈接: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> 

回答

0

我只是書面方式一個簡單的代碼,有一個輸入命名的標題和形式提交此表將調查表,並顯示匹配的搜索,如果發現了一些在數據庫進行匹配,希望它會給你關於在表或數據庫中搜索的一點想法。

所以,首先我創造了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」數據庫,然後創建一個名爲搜索表這將有兩列'id'和'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'; 
    } 
} 

注意:不要忘記也要在數據庫中插入值。要檢查它,請在表格搜索中插入一些值,然後在瀏覽器中運行表單文件,並輸入與您在表格中插入相同的標題,並顯示相應的結果。

0
require_once 「week11.php」; 

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

PHP標籤應在頂部,你正在使用PHP的標籤

<?php 
require_once 「week11.php」; 

$search = "TITLE"; 
getGames($search); 
包括文件