0

我一直在努力解决这个问题,但无法让变量用于搜索。可以很好地连接并返回定义为正确的 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>";
?>
4

1 回答 1

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);
php 是一种区分大小写的语言,您的帖子变量名称错误替换代码并重试,如果不成功,请尝试 echo $query 并在 sql server 的查询浏览器中运行它。

希望这能解决问题。

于 2014-12-11T17:00:47.953 回答