我已经打了一段时间,并且不能让变量适用于搜索。可以连接正常并返回定义为正确的mssql_query的结果,也可以通过变量等进行精确排序,但尝试实现文本搜索并不会返回结果,不确定原因。
<?php
$link = mssql_connect('SERV13\\RALSQL12', 'RA4joomla', 'Fenestron1');
if (!$link || !mssql_select_db('RALNHV', $link)) {
die('Unable to connect or select database!');
}else{
echo"";
}
if(isset($_REQUEST['submit'])){
$firstname=$_POST['FirstName'];
$surname=$_POST['Surname'];
$query = 'SELECT * FROM lEmployee WHERE FirstName LIKE '%".$firstname."%' OR Surname LIKE '%".$surname."%'';
$q=mssql_query($sql);
}
else{
$query = 'SELECT * FROM lEmployee';
$q = mssql_query($query);
}
?>
<form method="post">
<table width="200" border="1">
<tr>
<td>Name</td>
<td><input type="text" name="firstname" value="<?php echo $firstname;?>" /></td>
<td>Email</td>
<td><input type="text" name="surname" value="<?php echo $surname;?>" /></td>
<td><input type="submit" name="submit" value=" Find " /></td>
</tr>
</table>
</form>
<?php
// Check if there were any records
echo "<table class='table'>";
echo "<tr>";
echo "<th><a href='?orderBy=FirstName'>FirstName</a></th><th><a href='?orderBy=Surname'>Surname</a></th><th><a href='?orderBy=EmployeeNo'>Trigram</a></th><th>Office Phone</th><th>Mobile</th><th><a href='?orderBy=EmployeeJobTitle'>Job Title</a></th><th><a href='?orderBy=Name'>Base</a></th>";
echo "</tr>";
while ($row = mssql_fetch_array($query)) {
echo "<tr>";
echo "<td>" . '<a href="user-profile-id?id=' . $row['ID'] . '" class="modal">' . iconv("CP1252", "UTF-8", $row['FirstName']) . '</a>' . "</td>";
echo "<td>" . '<a href="user-profile-id?id=' . $row['ID'] . '" class="modal">' . iconv("CP1252", "UTF-8", $row['Surname']) . '</a>' . "</td>";
echo "<td>" . '<a href="user-profile-id?id=' . $row['ID'] . '" class="modal">' . $row['EmployeeNo'] . '</a>' . "</td>";
echo "<td>" . '<a href="user-profile-id?id=' . $row['ID'] . '" class="modal">' . $row['Phone'] . '</a>' . "</td>";
echo "<td>" . '<a href="user-profile-id?id=' . $row['ID'] . '" class="modal">' . $row['Mobile'] . '</a>' . "</td>";
echo "<td>" . '<a href="user-profile-id?id=' . $row['ID'] . '" class="modal">' . $row['EmployeeJobTitle'] . '</a>' . "</td>";
echo "<td>" . '<a href="user-profile-id?id=' . $row['ID'] . '" class="modal">' . $row['Name'] . '</a>' . "</td>";
echo "</tr>";
}
echo "</table>";
?>
答案 0 :(得分:1)
我100%同意nickL您在查询中遇到一些格式化问题尝试用此替换您的搜索查询:
$firstname=$_POST['firstname'];
$surname=$_POST['surname'];
$query = "SELECT * FROM lEmployee WHERE FirstName LIKE '%".$firstname."%' OR Surname LIKE '%".$surname."%'";
$q=mssql_query($sql);
希望这能解决问题。