2013-05-11 62 views
0

我有PHP的使用選擇查詢和內加入,但瀏覽器顯示錯誤:代碼與核能研究所joinnnn並選擇不工作

您的SQL語法錯誤;檢查手冊中 對應於你的MySQL服務器版本正確的語法使用 附近「SELECT specialization_name從用戶u INNER JOIN SPE」在1號線

誰能幫助我?

這是查詢:似乎增加了某種字符串查詢語句的結束

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 user u  INNER JOIN specialization s  ON u.specialization=s.specialization_id  WHERE u.user_id='$id'"; 

     $result = mysql_query($queryString) 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, registered_date FROM user $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> 
        </table> 
        <hr /> 
      '; 

}//close while 

?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta name="keywords" content="" /> 
<meta name="description" content="" /> 
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
<title>Search_Members</title> 
<link href='http://fonts.googleapis.com/css?family=Oswald:400,300' rel='stylesheet' type='text/css' /> 
<link href='http://fonts.googleapis.com/css?family=Abel|Satisfy' rel='stylesheet' type='text/css' /> 
<link href="default.css" rel="stylesheet" type="text/css" media="all" /> 
<!--[if IE 6]> 
<link href="default_ie6.css" rel="stylesheet" type="text/css" /> 
<![endif]--> 
</head> 
<body> 
<?php require_once('header.php'); ?> 
<div id="wrapper"> 
    <div id="page-wrapper"> 
     <div id="page"> 
      <div id="wide-content"> 


       <table width="94%" height="63"> 
       <tr> 
        <td width="29%"><form id="form1" method="post" action="member_search.php"> 

        Browse Newest Members 
        <input type="submit" name="button" id="button" value="go" /> 
        <input type="hidden" name="listbyq" value="newest_members" /> 
        </form></td> 
        <td width="28%"><form id="form2" name="form2" method="post" action="member_search.php"> 
        Search By specialization<br /> 
        <select name="specialization" class="formField"> 
         <option value="0">-- Select Your Specialization --</option> 

          <?php specializationQuery(); ?> 
        </select> 


        <input type="submit" name="button2" id="button2" value="go" /> 
        <input type="hidden" name="listbyq" value="by_specialization" /> 
        </form></td> 
        <td width="43%"><form id="form3" name="form3" method="post" action="member_search.php"> 
        Search By firstname 
        <label> 
        <input type="text" name = "fname" id="fname" /> 
        </label> 
        <input type="submit" name="button3" id="button3" value="go" /> 
        <input type="hidden" name="listbyq" value="by_firstname" /> 
        </form></td> 
       </tr> 
       </table> 
       <br /> 
       <table width="70%" align="center"> 
       <tr> 
        <td><?php print "$queryMSG"; ?> 
         <br /> 
         <?php print "$outputlist"; ?> 
        </td> 
       </tr> 
       </table> 
+0

你能迴應你的'$ querySrting'並粘貼嗎? – mehdi 2013-05-11 20:17:14

回答

0

您的代碼。該基地的說法是:

SELECT user_id, first_name, last_name, birth_date, registered_date 
FROM user 

然後您可以添加以下字符串之一:

$querySrting="WHERE registered_date!='' ORDER BY registered_date DESC " 

    $querySrting="SELECT specialization_name FROM user u  INNER JOIN specialization s  ON u.specialization=s.specialization_id  WHERE u.user_id='$id'"; 

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

嗯。其中之一不符合您的基本select . . . from聲明。要解決中間的例子,你可以,但在order by語句中的子查詢:

$querySrting="order by (SELECT specialization_name FROM user u  INNER JOIN specialization s  ON u.specialization=s.specialization_id  WHERE u.user_id='$id')"; 

或者,一個簡單的版本:

$querySrting="order by (SELECT specialization_name FROM specialization s WHERE s.user_id=user.user_id"; 

然而,可讀的專業化可能是有用的,所以把它放在基本查詢:

SELECT user_id, first_name, last_name, birth_date, registered_date, specialization_name 
FROM user u left outer join 
    specialization s 
    on u.specialization = s.specialization_id 

而只是使中間排序:

$querySrting="order by specialization_name";