gpt4 book ai didi

php - 使用多个文本框中的单个值到 php 的 sql 查询中?

转载 作者:行者123 更新时间:2023-11-30 01:04:47 26 4
gpt4 key购买 nike

我在querybook.php中有5个文本框,它们从html(文本框)的表单中获取bookid bookname bookauthor bookpublisher和bookedition的值来搜索书籍。Bookid 是图书表的主键。当用户填写 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>

最佳答案

无需检查每个字段,只需按如下方式更改您的查询即可。在 WHERE 子句中使用 ORLIKE

$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 子句

关于php - 使用多个文本框中的单个值到 php 的 sql 查询中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19796018/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com