如何用SELECT下拉式功能表和TEXT文字框從選擇資料庫搜尋資料

來源:互聯網
上載者:User
例如我的下拉式清單中學號、姓名、籍貫等選項,我選中“籍貫”,並在後面的文字框中輸入“北京”,點擊搜尋按鈕就在資料庫中相應的籍貫欄位進行匹配,並輸出最終的結果。我是用php+mysql,請問我下面的代碼有問題嗎?總是無法實現功能,顯示記錄為0條。

HTML頁面:


PHP頁面:
error_reporting(E_ALL ^ E_NOTICE);
$dbhost = "localhost";
$dbuser = "root";
$dbpassword = "";
$db = mysql_connect($dbhost, $dbuser, $dbpassword);
mysql_query("SET NAMES 'UTF8'");
mysql_select_db("lxshfile");
if(isset($_GET["action"])&&$_GET["action"]=="search"){
$searchId =$_POST["searchoption"];
$keyword =$_POST["keyword"];
$sqltext = "select * from lxsh where '$searchId' like '%$keyword%'";
$result = mysql_query($sqltext);
$row = mysql_numrows($result);
if($keyword==""){
echo"

請輸入關鍵詞!

";
exit;//若沒有輸入關鍵詞,則結束程式
}
else{
echo("
搜尋結果:共有".$row."條記錄

");
echo("








"); echo(" "); echo(" "); while ($table = mysql_fetch_array($result))//輸出資料查詢資料! { echo(" "); echo(" "); echo(" "); } echo("
學號姓名籍貫
".$table['xh']."".$table['xm']."".$table['jg']."
");
}
}
?>


回複討論(解決方案)

'$searchId' =>`$searchId` 或者乾脆就什麼都別加。

mysql_num_rows

我試過了,還是不行啊,$searchId上的引號加上或者去掉都不成。還有別的方法嗎?

不知道你說哪裡不行。$row還是0嗎?
$result = mysql_query($sqltext) or die(mysql_error());

貼出錯誤提示。

$sqltext = "select * from lxsh where '$searchId' like '%$keyword%'";

把這一句改成:

$sqltext = "select * from lxsh where $searchId like '%$keyword%'";

echo $sqltext;//可以列印出來看一下

$row = mysql_num_rows($result);
不是
$row = mysql_numrows($result);
樓主你寫錯了

回複3樓和4樓:

沒有錯誤提示,就是點擊了搜尋以後搜尋結果顯示0條,沒有任何搜尋結果。
按照4樓的列印,比如我選了“學號”,輸入了“069”,列印結果顯示如下:
select * from lxsh where like '%069%'

$searchId的值沒有取到,where後面什麼都沒有。

回複5樓:
已經修改過了,但是還是沒有結果的顯示,不知道是怎麼回事。

回複3樓:

不好意思,有錯誤提示如下:
“You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'like '%%'' at line 1”

回複3樓:

不好意思,有錯誤提示如下:
“You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'like '%%'' at line 1”



你的$keyword有問題,可能值沒傳過來。檢查一下

學號
姓名
籍貫
這裡的value值的設定,你的有沒有跟你資料庫中的命名是一樣的??如果不一樣,你把它改成跟資料庫的命名一樣試試看!

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.