【问题标题】:Issue with mysql search resultsmysql搜索结果的问题
【发布时间】:2011-09-16 13:50:08
【问题描述】:

一切都假装工作得很好,除了当我搜索任何东西时,我没有得到没有错误的结果。我认为我的问题在我的查询范围内,但我似乎可以查明它。我之前进行了一些搜索,但只链接了 2 个表。现在我正在尝试链接 4 个表,它变得非常复杂。我想澄清一下我可能需要调整什么来清除这个混乱。

    <?php


$search = $_GET['search'];


if (!$search) 
echo "You didn't enter a keyword";
else
{
  echo "<td>You searched for: <strong>$search </strong></td><br><br>";
  mysql_connect('localhost','myuserexample','mypassexample');
  mysql_select_db('spusers');  
  $id=@$_GET['id'];


  $query="
        SELECT
            spusername.id, spusername.firstname, spusername.lastname, spusername.splocation_id,
            sptraining.id, sptraining.trainingtype, sptraining.level,
            splocation.id, splocation.location,
            sprecord.spusername_id, sprecord.sptraining_id

        FROM spusername 
        JOIN sprecord ON spusername.id = sprecord.spusername_id
        JOIN sptraining ON sprecord.sptraining_id = sptraining.trainingtype
        JOIN splocation ON spusername.splocation_id = splocation.location
        WHERE MATCH ( firstname, lastname, trainingtype, level, location, spusername_id, sptraining_id, splocation_id ) 
        AGAINST('%".$search."%' IN BOOLEAN MODE) ORDER BY lastname ASC";

  $result1 = MySQL_query($query);  
  if(!$result1) {  
    echo MySQL_error()."<br>$query<br>";
  }  
  if (MySQL_num_rows($result1) > 0) {
    echo "<table class='sortable' width='750' align='center' border='1' bordercolor='#000000' bgcolor='#000000'
           cellspacing='2' cellpadding='2'><tr><th bgcolor=#999999>
           Employee</th><th bgcolor=#999999>
           Location</th><th bgcolor=#999999>
           Training</th><th bgcolor=#999999>
           Level</a></th><th bgcolor=#999999>
           Date Completed</th></tr bgcolor=#999999>";
    while($result2 = MySQL_fetch_array($result1)) {


      echo  "<td bgcolor=#d4d5ff>{$result2['lastname']},
            {$result2['firstname']}</td><td bgcolor=#d4d5ff>
            {$result2['location']}</td><td bgcolor=#d4d5ff>
            {$result2['trainingtype']}</td><td bgcolor=#d4d5ff>
            {$result2['level']}</td></tr>";
    }
    echo "</table>";
  } else {
    echo "No Results were found in this category.<br>";
  } 
  echo "<br>";
}
?>

【问题讨论】:

  • 进行了一些编辑,我删除了一些我所做的测试,并将其恢复为原始格式
  • 除了代码中的大量 SQL 注入漏洞之外,您还遇到了什么错误?还是只是返回了您没想到的数据?
  • 第一步是删除 where 子句并可能添加限制 1,这样您就可以确保您的连接工作。这样你就会知道你正在搜索的记录集是否存在
  • 马克,我没有收到任何错误。我得到我的 else “在此类别中未找到任何结果。”
  • Horatio,当我删除除第一个以外的其他连接并从选择中删除这些表时,我搜索名字或姓氏。我的退货结果正确显示。

标签: php mysql search select


【解决方案1】:

已解决...

而不是

加入 sptraining on sprecord.sptraining_id = sptraining.trainingtype 在 spusername.splocation_id = splocation.location 上加入 splocation

我错误地说它等于 trainingtype 和 location 而不是 id。

它的工作示例: 加入 sptraining on sprecord.sptraining_id = sptraining.id 在 spusername.splocation_id = splocation.id 上加入 splocation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    • 2014-11-02
    • 1970-01-01
    相关资源
    最近更新 更多