2014-02-17 26 views
0

我創建了一個搜索函數。它可以通過全文查找地址,但我如何通過地址的某些部分進行查詢搜索?對於例如完整的地址是paya lebar Road Blk27,我該如何做到讓用戶只需輸入paya,它仍會顯示出來?在子串中查找查詢而不是全文

SearchForm

<h2>View Patient Records</h2> 


<body> 
<form action="display_patient.php" method="post"> 
<p>Select: 
<select name="patient_var" > 
<?php 
$value = array(view_all, name, address); 
foreach ($value as $option) 
{ 
    echo '<option value="'.$option.'"' . (isset($_POST['patient_var']) && $_POST['patient_var'] == $option ? ' selected' : '') . '>' . $option . '</option>'; 
} 

?> 
</select> 

<input type="text" name="typed" value="" /> 

<input type ="submit" value="submit" /> 
</form> 
</p> 

<p> 

<?php 
if (isset($_POST['patient_var'])) { 

    $type = $_POST['typed']; 
    $select = $_POST['patient_var']; 
if ($select == 'view_all') { 
    echo "<table border='1'>"; 
echo "<tr>\n"; 
echo "<th>ID</th>\n"; 
echo "<th>Patient Name</th>\n"; 
echo "<th>Age</th>\n"; 
echo "<th>NRIC</th>\n"; 
echo "<th>Birth Date</th>\n"; 
echo "<th>Medical Allergies</th>\n"; 
echo "<th>Medical History</th>\n"; 
echo "<th>Phone</th>\n"; 
echo "<th>Address</th>\n"; 
echo "<th>Doctor Assigned</th>\n"; 
echo "</tr>"; 

    $pat_set = default_patient(); 

    while ($mo = mysqli_fetch_array($pat_set)) { 
       echo "<tr>"; 
echo "<td>" . $mo['id'] . "</td>"; 
echo "<td>". $mo['name'] . "</td>"; 
    echo "<td>". $mo['age'] . "</td>"; 
    echo "<td>". $mo['nric'] . "</td>"; 
    echo "<td>". $mo['birthdate'] . "</td>"; 
    echo "<td>". $mo['medical_allergies'] . "</td>"; 
     echo "<td>". $mo['medical_history'] . "</td>"; 
     echo "<td>". $mo['phone'] . "</td>";  
echo "<td>". $mo['address'] ."</td>"; 
     echo "<td>". $mo['doctor_assigned'] . "</td>"; 
echo "</tr>"; 
      } 
} 




else { 
    echo "<table border='1'>\n"; 
echo "<tr>\n"; 
echo "<th>ID</th>\n"; 
echo "<th>Patient Name</th>\n"; 
echo "<th>Age</th>\n"; 
echo "<th>NRIC</th>\n"; 
echo "<th>Birth Date</th>\n"; 
echo "<th>Medical Allergies</th>\n"; 
echo "<th>Medical History</th>\n"; 
echo "<th>Phone</th>\n"; 
echo "<th>Address</th>\n"; 
echo "<th>Doctor Assigned</th>\n"; 
echo "</tr>"; 

    $patients_set = 
find_patients($select, $type); 

while ($row = mysqli_fetch_array($patients_set)) 
{ echo "<tr>"; 
echo "<td>" . $row['id'] . "</td>"; 
echo "<td>". $row['name'] . "</td>"; 
    echo "<td>". $row['age'] . "</td>"; 
    echo "<td>". $row['nric'] . "</td>"; 
    echo "<td>". $row['birthdate'] . "</td>"; 
    echo "<td>". $row['medical_allergies'] . "</td>"; 
     echo "<td>". $row['medical_history'] . "</td>"; 
     echo "<td>". $row['phone'] . "</td>";  
echo "<td>". $row['address'] ."</td>"; 
     echo "<td>". $row['doctor_assigned'] . "</td>"; 
echo "</tr>"; } 

} 

}//end of if post submit 

?> 
</p> 
+2

在sql查詢中使用'LIKE %%'' – krishna

回答

1

在MySQL搜索一列是否已特定的字符串使用LIKE

SELECT * FROM table WHERE column LIKE '%somestring%'; 

在你的情況只是試試這個

$typed = $_POST['typed']; 

,使MySQL查詢像這樣

$query = "select * from table where Address LIKE '%".$typed."%' "; 
1

使用LIKE '%$searchParam%'代替= $searchParam

0

你忘了發表您的功能:find_patients($選擇,$型); 但我猜你需要一個地址varchar字段的部分查詢字符串這看起來是這樣的:

select * from MyTable where myColumn like '%myPartialString%'; 

這種類型的查詢將返回具有內「MyPartialString」的所有行。