1

我有一个带有内部连接的选择查询。我在 phpmyadmin 中尝试它工作正常。但是当我在浏览器中尝试它时,它会显示以下错误消息:

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“SELECT s.specialization_name FROM user u INNER JO”附近使用正确的语法

member_search.php

/default  message  on top of the result  display
$querySrting="WHERE registered_date!='' ORDER BY registered_date ASC ";
$queryMSG="Showing newest to oldest memebrs by default";
///IF STATMENT TO DISTINGOUICH SEARCHING
if(isset($_POST['listbyq']))
{
  if($_POST['listbyq']=="newest_members")
  {
      $querySrting="WHERE registered_date!='' ORDER BY registered_date DESC " or die(mysql_error());
      $queryMSG="Showing senior to oldest memebrs";
  }
elseif($_POST['listbyq']=="by_specialization")

   {
      $querySrting="SELECT specialization_name FROM members u 
                         INNER JOIN specialization s 
                                     ON u.specialization=s.specialization_id";

    $result = mysql_query($querySrting)  or die(mysql_error());
      $queryMSG="showing members with specs";
   }
elseif($_POST['listbyq']=="by_firstname")
  {
      $fname = $_POST['fname'];
      $fname = stripcslashes($fname);
      $fname = strip_tags($fname);
      $querySrting="WHERE  first_name LIKE '%$fname%'"or die(mysql_error());
      $queryMSG="Showing member with the name you searched for";
  }
}
///******query the member data using the queryString*******//
$sql = mysql_query("SELECT user_id, first_name, last_name, birth_date, specialization, registered_date FROM members $querySrting") or die(mysql_error());
 //**********************outputlist*********************************//
 $outputlist="";
 while($row = mysql_fetch_array($sql))
 {
     $id=$row['user_id'];
     $firstname=$row['first_name'];
     $lastname=$row['last_name'];
     $birthdate=$row['birth_date'];
     $spec = $row['specialization'];
     $registereddate=$row['registered_date']; 
     ////***********for the upload image*************************//
      $check_pic="members/$id/image01.jpg";
   $default_pic="members/0/image01.jpg";
   if(file_exists($check_pic))
   {
       $user_pic="<img src=\"$check_pic\"width=\"120px\"/>";
   }
   else
   {
       $user_pic="<img src=\"$default_pic\"width=\"120px\"/>";
   }

   $outputlist.='
   <table width="100%">
               <tr>
                  <td width="23%" rowspan="3"><div style="height:120px;overflow:hidden;"><a href = "http://localhost/newadamKhoury/profile.php?user_id='.$id.'" target="_blank">'.$user_pic.'</a></div></td>
                  <td width="14%"><div  align="right">Name:</div></td>
                  <td width="63%"><a href = "http://localhost/newadamKhoury/profile.php?user_id='.$id.'" target="_blank">'.$firstname.' '.$lastname.'</a></td>
                  </tr>

                  <tr>
                    <td><div align="right">Birth date:</div></td>
                    <td>'.$birthdate.'</td>
                  </tr>
                  <tr>
                   <td><div align="right">Registered:</div></td>
                   <td>'.$registereddate.'</td>
                  </tr>

                  <tr>
                   <td><div align="right">His Job:</div></td>
                   <td>'.$spec.'</td>
                  </tr>
                  </table>
                  <hr />
          ';

 }//close while
4

2 回答 2

2

$querySrting是一个变量。您应该连接这些字符串:

$sql = mysql_query("SELECT user_id,
                           first_name,
                           last_name,
                           birth_date,
                           specialization,
                           registered_date
                    FROM members $querySrting") // HERE

应该是这样的:

$sql = mysql_query("SELECT user_id,
                           first_name,
                           last_name,
                           birth_date,
                           specialization,
                           registered_date
                    FROM members".$querySrting) // HERE
于 2013-05-13T17:38:16.890 回答
1

您的代码非常糟糕,并显示出货物崇拜编程的主要症状:

  $querySrting="WHERE  first_name LIKE '%$fname%'"or die(mysql_error());

您正在将一些文本分配给一个变量,这不可能失败,但您尝试将其视为潜在的 SQL 错误。你没有执行那一小段 SQL,所以这行绝对不可能触发 sql 错误。

在上一行之前,您有

  $fname = $_POST['fname'];
  $fname = stripcslashes($fname);
  $fname = strip_tags($fname);

这让你对SQL 注入攻击敞开了大门。通过通过 stripcslashes() 运行 $fname ,假设您使用的是启用了 magic_quotes 的 PHP,您已经消除了防止注入攻击的一个 MINOR 完全FAINT HOPE 。

最重要的是,您的代码中的所有查询都不是导致您遇到的 SQL 错误的实际查询。该错误清楚地说明了FROM USER u,它不会出现在您的代码中的任何位置。

于 2013-05-13T17:38:07.623 回答