0

我在 querybook.php 中有 5 个文本框,它们从 html(文本框)中的表单中获取 bookid bookname bookauthor bookpublisher 和 bookingition 的值来搜索一本书。bookid 是 book 表的主键。当用户填写 html 表单中的所有字段时,就可以了。当用户在我的 php 页面中只填写几个字段或一个字段时,它应该占用该字段的值并在查询中使用它来检索指定的书籍、书名、作者、出版商或书籍版本。下面给出了我的代码,对于来自单个字段的值,它不像上面指定的那样工作。

<HTML>
 <HEAD><h1 align="center">THIS IS QUERY BOOK PAGE</h1></HEAD>
 <br /> 
    <BODY>
                <FORM action="querybook.php" method="POST">
                    ENTER ANY OF THE FOLLOWING DETAILS TO SEARCH FOR A BOOK</br></br></br>
        <table>
                    <tr><td>ENTER THE BOOK ID :<td><input type=text value=bkid  name="bkid"></tr>
        <tr><td>ENTER THE NAME OF THE BOOK :<td><input type=text value=bkname autofocus="true" name="bkname"></tr>
        <tr><td>ENTER THE NAME OF THE AUTHOR :<td><input type=text value=bkauth name="bkauth"></tr>
        <tr><td>ENTER THE NAME OF THE PUBLISHER:<td><input type=text value=bkpub name="bkpub"></tr>
        <tr><td>ENTER THE NAME OF THE EDITION :<td><input type=text value=bkedn name="bkedn"></tr>
        </table>
        <BR>
                    CLICK HERE TO SUBMIT THE FORM :<input type="submit"  value="QUERY BOOK" name="submit"></br></br>
                </FORM>
                <?php
                $server="localhost";
                $username="root";
                $password="pramit";
                $link=mysql_connect($server, $username, $password) or die("Could not connect to database");
                mysql_select_db('test',$link);
                if(isset($_POST['submit']))    
                {
                    $bkid   = trim($_POST['bkid']);
                    $bkauth = trim($_POST['bkauth']);
                    $bkname = trim($_POST['bkname']);
                    $bkpub  = trim($_POST['bkpub']);
                    $bkedn  = trim($_POST['bkedn']);
                    if(isset($bkid) || isset($bkname) || isset($bkauth) || isset($bkpub) ||isset($bkedn))
                    {  
                        if($bkid!="" )
                        {
                            $query="select bkid,bkname,bkauth,bkpub,bkedn from books where bkid=$bkid";
                            $result=mysql_query($query);
                        }
                        if($bkname!="")
                        {
                            $query="select bkid,bkname,bkauth,bkpub,bkedn from books where bkname=$bkname";
                            $result=mysql_query($query);
                        }
                        if($bkauth!=""  )
                        {
                            $query="select bkid,bkname,bkauth,bkpub,bkedn from books where bkauth=$bkauth";
                            $result=mysql_query($query);
                        }
                        if($bkpub!="" )
                        {
                            $query="select bkid,bkname,bkauth,bkpub,bkedn from books where bkpub=$bkpub";
                            $result=mysql_query($query);
                        }
                        if($bkedn!=""  )
                        {
                            $query="select bkid,bkname,bkauth,bkpub,bkedn from books where bkedn=$bkedn";
                            $result=mysql_query($query);
                        }
                    }
                    echo "<table border='1'>";
                    echo "<th>BOOK ID</th><th>BOOK NAME</th><th>BOOK AUTHOR</th><th>BOOK PUBLISHER</th><th>BOOK EDITION</th>";
                    while($row=mysql_fetch_row($result))
                    {
                        echo "<tr>";print("<td>{$row[0]}<td>{$row[1]}<td>{$row[2]} <td>{$row[3]}<td>{$row[4]}\n");echo "</tr>";
                    }
                    echo "</table>";
                 }
                 mysql_close($link);
                ?>
    </BODY>

4

2 回答 2

0

您的声明:

if(isset($bkid) || isset($bkname) || isset($bkauth) || isset($bkpub) ||isset($bkedn))

即使已输入所有字段,也会评估为 true。尝试:

if(isset($bkid) && isset($bkname) && isset($bkauth) && isset($bkpub) &&isset($bkedn)){
   //insert record
}
elseif(isset($bkid) || isset($bkname) || isset($bkauth) || isset($bkpub) ||isset($bkedn)){
   //select record
}
于 2013-11-05T18:37:59.010 回答
0

无需检查每个字段,只需按如下方式更改您的查询。使用ORand LIKEinWHERE子句

$query="select bkid,bkname,bkauth,bkpub,bkedn from books where bkid LIKE '%$bkid%' OR bkname LIKE '%$bkname%' OR bkauth LIKE '%$bkauth%' OR bkpub LIKE '%$bkpub%' OR bkedn LIKE '%$bkedn%' ";

编辑

$query="select bkid,bkname,bkauth,bkpub,bkedn from books where 1=1 ";
if($bkid!="" ) {

      $query .= " AND bkid=$bkid";
}
if($bkname!="") {

      $query .= " AND bkname=$bkname ";                       
}
if($bkauth!=""  ) {

       $query .= " AND bkauth=$bkauth";
}
if($bkpub!="" ) {

       $query .= " AND bkpub=$bkpub";
}
if($bkedn!=""  ) {

       $query .= " AND bkedn=$bkedn";
}

$result=mysql_query($query);

仅当值不等于 null 时,才会将条件附加到 where 子句

于 2013-11-05T18:43:00.060 回答