請大家幫忙解決!

edited 十月 2013 in 進階PHP討論
我們學校要我們做一個網站,而我是負責搜尋部分的,可是我遇到難題了,又看不出那裡錯,請大家幫忙修改一下!感恩~~
我現在遇到錯誤的地方是查詢keyword_name的地方,我只要一選keyword就會無法查詢,而且查不到就算了,錯誤還會跑到"無法查詢1"是怎樣呢?還有"不限欄位"的查詢我也不太懂請給個方向可以嗎?

<html>

<head>
<meta http-equiv="Content-Language" content="zh-tw">
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<title>新增網頁1</title>
</head>

<body>

<form method="POST" action="test.php">
<p><input type="text" name="t1" size="20"> <select size="1" name="d_kind2">
<option value="author_name">作者</option>
<option value="con_outline">大綱</option>
<option value="con_title">標題</option>
<option value="keyword_name">關鍵字</option>
<option value="NoField">不限欄位</option>
</select> <select size="1" name="d_kind">
<option>and</option>
<option>or</option>
</select></p>
<p><input type="text" name="t2" size="20"> <select size="1" name="d_kind3">
<option value="author_name">作者</option>
<option value="con_outline">大綱</option>
<option value="con_title">標題</option>
<option value="keyword_name">關鍵字</option>
<option value="NoField">不限欄位</option>
</select> </p>
<p><input type="checkbox" name="con" value="ON">模糊比對</p>
<p><input type="submit" value="查詢" name="sel"><input type="reset" value="重新設定" name="new"></p>
</form>

</body>

</html>

<?php
// 判斷使用者是否按傳送按鈕
if ($_POST == '查詢'){
// 連結資料庫
$link= mysql_connect("127.0.0.1", "root", "e22908")
or die("無法開啟MySQL資料庫連結!<br>");
echo"資料庫開啟成功!<br>" ;
$db=mysql_select_db("xoops",$link)
or die("無法連結指定的資料表!<br>");
echo"資料表連結成功!<br>" ;

$input1=$_POST["t1"];
$input2=$_POST["t2"];
$a=$_POST["d_kind2"];
$b=$_POST["d_kind3"];
$ao1=$_POST["d_kind"];
$tt1=0;
$c=0;
if ($input1=='' and $input2<>'') {
$tt1=$input2;
$c=$b;
}
elseif ($input2=='' and $input1<>'') {
$tt1=$input1;
$c=$a;
}

if ($c <>'keyword_name'){
if ($input1<>'' and $input2<>''){
$sql="SELECT xoops_pod_author.author_name ,xoops_pod_contrbution.con_title ,xoops_pod_contrbution.con_outline , xoops_pod_contrbution.con_num FROM xoops_pod_author , xoops_pod_contrbution where xoops_pod_author.author_id = xoops_pod_contrbution.author_id and (($a like '%$input1%') $ao1 ($b like '%$input2%'))";
$result=mysql_query($sql) or die("無法執行查詢1<br>");
} else{
$sql="SELECT xoops_pod_author.author_name ,xoops_pod_contrbution.con_title ,xoops_pod_contrbution.con_outline , xoops_pod_contrbution.con_num FROM xoops_pod_author , xoops_pod_contrbution where (xoops_pod_author.author_id = xoops_pod_contrbution.author_id) and ($c like '%$tt1%')";
$result=mysql_query($sql) or die("無法執行查詢2<br>");
}

echo"<table border=1>";
//顯示欄位名稱
echo "<tr>";
echo "<td>".'作者'."</td>";
echo "<td>".'標題'."</td>";
echo "<td>".'大綱'."</td>";
echo "<td>".'關鍵字(出現次數)'."</td>";
echo "</tr>";
//顯示每一筆記錄
while($row=mysql_fetch_row($result)){

//顯示每一筆記錄的欄位值
echo "<tr>";
for ($i=0; $i<=2; $i++){
echo"<td>".$row[$i]."</td>";

}

$sql1="SELECT * FROM xoops_pod_keyword , xoops_pod_keyword_count where (xoops_pod_keyword.keyword_num = xoops_pod_keyword_count.keyword_num )and(xoops_pod_keyword_count.con_num = '" . $row[3] . "')";
echo $sql1;
$result1=mysql_query($sql1) or die("無法執行查詢3<br>");
$no_row=mysql_num_rows($result1);
echo "<td>";
for ($i=0;$i<$no_row;$i++){

$row1=mysql_fetch_row($result1);
echo $row1[2]."(".$row1[5].")<p>";
}
echo"</td>" ;

echo "</tr>";
}
echo"</table>";


}
//keyword
if ($input1<>'' and $input2<>''){
$sql2="SELECT * FROM xoops_pod_contrbution , xoops_pod_keyword ,xoopa_pod_keyword_count where (xoops_pod_keyword_count.con_num = '" . $row[3] . "') and (($a like '%$input1%') $ao1 ($b like '%$input2%'))";
$result2=mysql_query($sql2) or die("無法執行查詢4<br>");
echo $sql2;
} else{
$sql2="SELECT * FROM xoops_pod_keyword , xoops_pod_keyword_count where (xoops_pod_keyword.keyword_num = xoops_pod_keyword_count.keyword_num) and ($c like '%$tt1%')";
$result2=mysql_query($sql2) or die("無法執行查詢5<br>");
$row2=mysql_fetch_row($result2);
echo $sql2;
echo"<td>".$row2[2]."</td>";
echo "</tr>";
}
echo"</table>";


echo"<td>".$row1[2]."</td>";
echo"<td>".$row1[5]."</td>";
}
?>

ps.keyword會用到的資料庫有 keyword、keyword_count、contrbution
如果我的疑問說的不清楚請反應我會再回應的!謝謝~

原始討論: http://twpug.net/x/modules/newbb/viewtopic.php?topic_id=715

評論

  • edited 八月 2005
    if ($input1<>'' and $input2<>'')
    

    有這樣的語法嗎?是不是應該這樣啊:
    if ($input1<>'' && $input2<>'')
    

    自己可以試試逐行除錯,一次將整個程式丟上來,大家也不知道給什麼建議吧。
  • edited 八月 2005
    會跑到"無法執行查詢1"是因為sql問題所以程式才跑到die那裡
    所以就從sql開始除錯,在$result=mysql_query($sql) 這航之前echo $sql出來看看,是不是sql指令有錯。

    "不限欄位"查詢的部份,要欄位應該是預先就可以知道的吧?比如c1~c3共3個欄位
    ,把sleect句子裡where的條件全串起來,如下:
    $SQL = "... where c1 like '%keyword%' or c2 like '%keyword%' or c3 like '%keyword%'";

    打的好累....
  • edited 八月 2005
    if ($input1<>'' and $input2<>'')
    

    真的有這個,一時不察,抱歉
  • edited 八月 2005
    有什麼指令是可以直接查詢資料庫裡各表單面所有的欄位嗎?因為"不限欄位"這個查詢會用得到?
  • edited 八月 2005
    1. 全文搜尋的功能可以針對MYSQL的特性實做:

    http://dev.mysql.com/doc/mysql/en/fulltext-search.html

    2. 從資料庫取回資料的函數就會帶有資料庫的欄位名稱,建議還是乖乖的輸入能夠查詢的欄位,過度空汎的條件會讓程式不容易承受較大的挑戰。
  • edited 八月 2005
    if ($input3<>'' && $input2<>'' && $input1==''){
    $tt1=$input2;
    $e=$ao2 ;
    $tt2=$input3;
    //載入物件
    include"traslation.php";
    $c_to_e=new c_to_e;
    $c=$c_to_e->translate($a3);
    $d=$c_to_e->translate($a2);
    if($a3=='NoField' and $a2=='NoField'){
    $query="SELECT * FROM ".$xoopsDB->prefix("users").",".$xoopsDB->prefix("pod_contrbution").",".$xoopsDB->prefix("pod_keyword").",".$xoopsDB->prefix("pod_keyword_count"). ",".$xoopsDB->prefix("ranks") ." WHERE (((xoops_users.name like '%$tt1%'and xoops_ranks.rank_title='作者') or (con_title like '%$tt1%') or (con_outline like '%$tt1%') or (keyword_name like '%$tt1%')) $e ((xoops_users.name like '%$tt2%'and xoops_ranks.rank_title='作者') or (keyword_name like '%$tt2%') or (con_title like '%$tt2%') or (con_outline like '%$tt2%')))and xoops_users.uname = xoops_pod_contrbution.author_id and xoops_pod_contrbution.con_num=xoops_pod_keyword_count.con_num and xoops_pod_keyword.keyword_num=xoops_pod_keyword_count.keyword_num ";
    $result =$xoopsDB->query($query);echo $query;
    }
    elseif($a3<>'NoField' and $a2=='NoField'){
    $query="SELECT * FROM ".$xoopsDB->prefix("users").",".$xoopsDB->prefix("pod_contrbution").",".$xoopsDB->prefix("pod_keyword").",".$xoopsDB->prefix("pod_keyword_count"). ",".$xoopsDB->prefix("ranks") ." WHERE ((xoops_users.name like '%$tt1%'xoops_ranks.rank_title='作者') or (con_title like '%$tt1%') or (con_outline like '%$tt1%') or (keyword_name like '%$tt1%')) $e (".$d." like '%$tt2%') and xoops_users.uname = xoops_pod_contrbution.author_id and xoops_pod_contrbution.con_num=xoops_pod_keyword_count.con_num and xoops_pod_keyword.keyword_num=xoops_pod_keyword_count.keyword_num ";
    $result =$xoopsDB->query($query);
    }
    elseif($a3=='NoField' and $a2<>'NoField'){
    $query="SELECT * FROM ".$xoopsDB->prefix("users").",".$xoopsDB->prefix("pod_contrbution").",".$xoopsDB->prefix("pod_keyword").",".$xoopsDB->prefix("pod_keyword_count"). ",".$xoopsDB->prefix("ranks") ." WHERE (".$c." like '%$tt1%') $e ((keyword_name like '%$tt2%') or (xoops_users.name like '%$tt2%'and xoops_ranks.rank_title='作者') or (con_title like '%$tt2%') or (con_outline like '%$tt2%')) and xoops_users.uname = xoops_pod_contrbution.author_id and xoops_pod_contrbution.con_num=xoops_pod_keyword_count.con_num and xoops_pod_keyword.keyword_num=xoops_pod_keyword_count.keyword_num ";
    $result =$xoopsDB->query($query);
    }
    else{
    $query="SELECT * FROM ".$xoopsDB->prefix("users").",".$xoopsDB->prefix("pod_contrbution").",".$xoopsDB->prefix("pod_keyword").",".$xoopsDB->prefix("pod_keyword_count"). ",".$xoopsDB->prefix("ranks") ." WHERE ((" .$c. " like '%$tt1%')$e(" .$d. " like '%$tt2%'))and xoops_ranks.rank_title='作者' and xoops_users.uname = xoops_pod_contrbution.author_id and xoops_pod_contrbution.con_num=xoops_pod_keyword_count.con_num and xoops_pod_keyword.keyword_num=xoops_pod_keyword_count.keyword_num ";
    $result =$xoopsDB->query($query);echo $query;
    }

    這是我試著寫出來的"不限欄位",這一段是在判斷2、3欄的值不是空白(因為我們的介面有3個欄位可以輸入值),但是我測試後,只有2、3 都不是選擇"不限欄位"的可以成功,其它的還是不可以,我自已也找不出那裡錯了,請各位大大幫個忙吧!
  • edited 八月 2005
    1. 建議先花點時間看個文章:

    http://twpug.net/x/modules/smartsection/item.php?itemid=1

    你的程式碼很難看懂...

    2. 可以在
    if($a3=='NoField' and $a2=='NoField'){
    
    之前嘗試輸出 $a3 與 $a2 ,也許是大小寫問題或打錯字。
Sign In or Register to comment.