2015-10-14 63 views
1

我是mysql和php的新手。我正在處理歌曲數據庫。我有一個搜索腳本,它搜索我的數據庫的所有列,並在表中顯示結果。但我想讓該搜索腳本通過選定的列字段進行搜索。在搜索框下方,我想添加項目符號選項(ex: search by column 1, column 2, column 3, column 4)您可以在下面看到一張圖片。mysql數據庫按列搜索

song database

歌曲數據庫表。

+----+--------+----------+-----+-------+ 
| id | title | artist | key | genre | 
+----+--------+----------+-----+-------+ 
| 1 | xxxxx | xxx xxxxx| xxx | xxxxx |  
| 2 | xxxxx | xxx xxxxx| xxx | xxxxx |  
| 3 | xxxxx | xxx xxxxx| xxx | xxxxx |  
| 4 | xxxxx | xxx xxxxx| xxx | xxxxx |   
| 5 | xxxxx | xxx xxxxx| xxx | xxxxx |   
+----+--------+----------+-----+-------+ 

搜索腳本:

<?php 


    $host = "localhost"; 
    $user = "xxxxxxx"; 
    $password = "xxxxxxx"; 
    $database_name = "xxxxxx"; 
    $pdo = new PDO("mysql:host=$host;dbname=$database_name", $user, $password, array(
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION 

    )); 

$pdo->exec("set names utf8"); 
$search=$_POST['search']; 
$query = $pdo->prepare("select * from table where title LIKE '%$search%' OR key LIKE '%$search%' OR genre LIKE '%$search%' OR artist LIKE '%$search%' LIMIT 0 , 100"); 
$query->bindValue(1, "%$search%", PDO::PARAM_STR); 
$query->execute(); 

     if (!$query->rowCount() == 0) { 

      $row_cnt = $result->num_rows; 

    printf("<p class='notice'>Your search for <span class='blue'>「</span><span class='blue'>" . $result['search'] . "</span><span class='blue'>」</span> yielded %d results.\n</p>", $row_cnt); 
       echo "<table class='table'>"; 

       echo "<tr class='tablehead'>"; 

        echo "<th>Title</th>"; 
        echo "<th>Artist</th>"; 
        echo "<th>Key</th>"; 
        echo "<th>Genre</th>"; 
        echo "</tr>";   
      while ($results = $query->fetch()) { 
      echo "<tr>"; 

      echo "<td> <a href=publicsong.php?id=".$results['id'] . ">" "</a> " . $results['title'] . " </td>"; 

        echo "<td>" . $results['artist'] . "</td>"; 
        echo "<td>" . $results['key'] . "</td>"; 
        echo "<td>" . $results['genre'] . "</td>"; 

       echo "</tr>"; 

      } 
       echo "</table>";   
     } else { 
      echo 'Nothing found'; 
     } 
?> 

林新手。請幫忙。

UPDATE

$search=$_POST['search']; 
$field=$_POST['radio_grp']; 
switch($field) 
{ 
case 'Title' : $field='title'; 
break; 
case 'Artist' : $field='artist'; 
break; 
case 'Category' : $field='category'; 
break; 
} 

$query = $pdo->prepare("select * from lyrics_a where $field LIKE '%$search%' LIMIT 0 , 100"); 
$query->bindValue(1, "%$search%", PDO::PARAM_STR); 
$query->execute(); 


    echo"<form action='search.php' method='post'> 
Search: <input type='text' id='search' name='search' placeholder='Search' required data-validation-required-message='Please enter atleast 3 characters'/> 
<input type='submit' class='searchButton greenButton' value='Go' /> 
<input type='radio' name='search' value='title'/>Title 
<input type='radio' name='search' value='artist'/>Artist 
<input type='radio' name='search' value='category'/>Category 

</form><br>"; 



    // Display search result 
     if (!$query->rowCount() == 0) { 

      $row_cnt = $result->num_rows; 


       echo "<table class='table'>"; 

       echo "<tr class='tablehead'>"; 



        echo "<th>Title</th>"; 

        echo "<th>Artist</th>"; 

        echo "<th>Category</th>"; 

       echo "</tr>";   
      while ($results = $query->fetch()) { 
      echo "<tr>"; 

      echo "<td> <a href=publicsong.php?id=".$results['id'] . ">" . $results['tel_title'] . "</a> " . $results['title'] . " </td>"; 

        echo "<td>" . $results['artist'] . "</td>"; 

        echo "<td>" . $results['category'] . "</td>"; 

       echo "</tr>"; 

      } 
       echo "</table>";   
     } else { 
      echo 'Nothing found'; 
     } 
?> 

回答

1

你應該把你的單選按鈕的值,並作出適當的查詢

$search=$_POST['search']; 
$field=$_POST['radio_grp']; 
switch($field) 
{ 
case 'Title' : $filed='title' // or your column field 
break; 
//---similar cases 
} 
$query = $pdo->prepare("select * from table where $field LIKE '%$search%' LIMIT 0 , 100"); 
+0

它不起作用 – dan

+0

嘿rohit。我更新了我的問題,請看看。我不知道我會出錯哪裏。我看到一個空白頁面。 THQ。 – dan

1

試試這個代碼

設置單選按鈕的相同值專欄名稱

<input type="radio" name="search_by" value="title">title<br> 
<input type="radio" name="search_by" value="artist">artist<br> 
<input type="radio" name="search_by" value="key">key<br> 
<input type="radio" name="search_by" value="genre">genre<br> 

$search=$_POST['search']; 
$search_by=$_POST['search_by']; 

$query = $pdo->prepare("select * from table where $search_by LIKE '%$search%' LIMIT 0 , 100"); 
+0

你不應該在html中暴露實際的表名和列名。 –