2014-07-23 120 views
0

我在使用php進行搜索時遇到問題。我正在嘗試通過使用和html下拉框來更改要搜索的列。它也應該使用佔位符(%?%),以便我可以進行全局搜索。當我用字母'b'搜索任何東西時,它只顯示所有記錄(所有記錄(當它只顯示其中帶有字母'b'的記錄時)。使用php搜索數據,使用php

的代碼如下:

HTML(請這是不是一個編輯的版本,這個問題似乎是在PHP文件):

<html lang="en" class=" js csstransforms3d csstransitions"> 
     <head> 
    <script> 
    function showUser() { 

    var querycolumn = document.getElementById('querycolumn').value; 

    var query = document.getElementById('query').value; 

     if (window.XMLHttpRequest) { 
     // code for IE7+, Firefox, Chrome, Opera, Safari 
     xmlhttp=new XMLHttpRequest(); 
     } else { // code for IE6, IE5 
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
     xmlhttp.onreadystatechange=function() { 
     if (xmlhttp.readyState==4 && xmlhttp.status==200) { 
      document.getElementById("txtHint").innerHTML=xmlhttp.responseText; 
     } 
     } 
     xmlhttp.open("GET","contactsearching.php?querycolumn="+querycolumn+"&query="+query,true); 
     xmlhttp.send(); 
    } 
    </script> 
    <script> 
    function userdata(str) { 
     if (str=="") { 
     document.getElementById("userdetail").innerHTML=""; 
     return; 
     } 
     if (window.XMLHttpRequest) { 
     // code for IE7+, Firefox, Chrome, Opera, Safari 
     xmlhttp=new XMLHttpRequest(); 
     } else { // code for IE6, IE5 
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
     xmlhttp.onreadystatechange=function() { 
     if (xmlhttp.readyState==4 && xmlhttp.status==200) { 
      document.getElementById("userdetail").innerHTML=xmlhttp.responseText; 
     } 
     } 
     xmlhttp.open("GET","searchingforcontact.php?q="+str,true); 
     xmlhttp.send(); 
    } 
    </script> 
     </head> 
     <body> 
<section class="flexform" style="display:inline-block;"> 

<div id="txtHint" style="padding-left:30px;padding-top:10px; padding-bottom:20px;"><b>The Contacts Which Are In The Contact Group Chosen Shall Be Displayed Here:</b></div> 
        <form> 
        <label style="clear:both;"><strong>Searching Parameter:</strong></label> 
        <select name="querycolumn" id="querycolumn" style="float:left;"> 
        <option value="" selected disabled>Please Choose A Parameter To Search By</option> 
        <option value="CustomerID">Customer ID</option> 
        <option value="CustomerBusinessName">Business Name</option> 
        <option value="ContactPerson">Contact Person</option> 
        <option value="Department">Department</option> 
        <option value="OccupationalTitle">Occupational Title</option> 
        <option value="EmailAddress">Email Address</option> 
        <option value="PhoneNumber">Phone Number</option> 
        <option value="EXT">EXT</option> 
        <option value="FaxNumber">Fax Number</option> 
        <option value="BusinessAddress">Business Address</option> 
        <option value="BusinessCity">Business City</option> 
        <option value="BusinessState">Business State</option> 
        <option value="BusinessZipCode">Business Zip Code</option> 
        <option value="BusinessCountry">Business Country</option> 
        <option value="PostalAddress">Postal Address</option> 
        <option value="PostalCity">Postal City</option> 
<option value="Comment">Comment</option> 
</select> 
<br></br> 
        <label style="clear:both;"><strong>Value Searching For:</strong></label><input type="text" name="query" style="float:left;" id="query" /> 
        <input type="button" onclick='showUser()' value="Initiate Searching Sequence" style="float:left;"/> 
<br></br> 
        </form> 

       </section> 
</body></html> 

PHP文件:

<?php 
$querycolumn = $_GET['querycolumn']; 
$query = $_GET['query']; 

$con=mysqli_connect("localhost","user","password","my_db"); 

// Check connection 

if (mysqli_connect_errno()) { 

    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 

} 

echo "<table border='4'> 
<tr> 
<th>Customer ID</th> 
<th>Customer Business Name</th> 
<th>Contact Person</th> 
<th>Department</th> 
<th>Phone Number</th> 
<th>Email Address</th> 
<th>Comment</th> 
</tr>"; 

$stmt=$con->prepare("SELECT CustomerID, CustomerBusinessName, ContactPerson, Department, PhoneNumber, EmailAddress, Comment FROM `Contacts` WHERE ? LIKE CONCAT('%',?,'%')"); 
$stmt->bind_param("ss", $querycolumn, $query); 
$stmt->execute(); 
$stmt->bind_result($col1, $col2, $col3, $col4, $col5, $col6, $col7); 

while ($stmt->fetch()) { 
     printf ("<tr> \n <td><a href onclick='userdata($col1)'>$col1</a></td> \n <td><a href='#' onclick='userdata($col1)'>$col2</a></td> \n <td><a href='#' onclick='userdata($col1)'>$col3</a></td> \n <td><a href='#' onclick='userdata($col1)'>$col4</a></td> \n <td><a href='#' onclick='userdata($col1)'>$col5</a></td> \n <td><a href='#' onclick='userdata($col1)'>$col6</a></td> <td><a href='#' onclick='userdata($col1)'>$col7</a></td> \n \n </tr>", $col1, $col1, $col1, $col2, $col1, $col3, $col1, $col4, $col1, $col5, $col1, $col6, $col1, $col7); 
    } 

?> 

任何幫助將不勝感激。

+1

一件事,你不能做到這一點'其中' - 分配一個變量或選擇一個實際的列名。它不知道如何「向前看」。 –

+1

嘗試使用'WHERE $ querycolumn LIKE'而不是'WHERE? LIKE' –

+1

非常感謝! 它正在工作。把這個作爲答案,我會選擇它作爲最好的! :D – SML

回答

0

「感謝洙多!它的工作將這個下來作爲一個答案,我會選擇它作爲最佳:!d」

按照要求:

你不能這樣做WHERE ?

爲列指定一個變量或選擇一個實際的列名稱。

MySQL不知道如何「向前看」。

即:

$column = "column_name"; 
WHERE $column LIKE CONCAT('%',?,'%')") 

WHERE column_name LIKE CONCAT('%',?,'%')")