2015-10-14 62 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中暴露实际的表名和列名。 –