【问题标题】:PHP MySQL search using multiple text boxes to search multiple columnsPHP MySQL搜索使用多个文本框搜索多个列
【发布时间】:2013-11-22 16:57:38
【问题描述】:

我正在学习 PHP,并正在从事一个在 MySQL 数据库中搜索书籍的项目。用户应该能够按书名、书名和类别进行搜索,使用这三者的全部、一个或任意组合。

目前这里是我的代码:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome to Library Management System</title>
<link href="main.css" rel="stylesheet" type="text/css" />
</head>
<body>

<?php

require_once "db.php";
include "header.html";


if(isset($_POST["bookTitle"]))
{
$bookTitle = mysqli_real_escape_string($con, $_POST["bookTitle"]);
}
else
{
$bookTitle = NULL;
}

if(isset($_POST["bookAuthor"]))
{
$bookAuthor = mysqli_real_escape_string($con, $_POST["bookAuthor"]);
}
else
{
$bookAuthor = NULL;
}

if(isset($_POST["category"]))
{
$category = mysqli_real_escape_string($con, $_POST["category"]);
}
else
{
$category= NULL;
}



echo "Results by Book Title Search";
$bookTitle = mysqli_real_escape_string($con, $_POST["bookTitle"]);
$query = "Select * From book NATURAL JOIN category where category.CategoryDesc LIKE '%" .$category ."%' OR book.BookTitle LIKE '%" .$bookTitle ."%' OR book.Author LIKE '%" .$bookAuthor."%'";
$result=mysqli_query($con, $query) or die(mysqli_error());

echo '<table border="1" width="95%">'."\n";
echo "<tr><th>ISBN</th><th>Title</th><th>Author</th><th>Edition</th><th>Year</th><th>Category ID</th><th>Reserved</th><th>Reserve?</th><tr>";

while($row = mysqli_fetch_array($result, MYSQLI_BOTH)){
echo "<tr><td>";
echo(htmlentities($row[0]));
echo("</td><td>");
echo(htmlentities($row[1]));
echo("</td><td>");
echo(htmlentities($row[2]));
echo("</td><td>\n");
echo(htmlentities($row[3]));
echo("</td><td>\n");
echo(htmlentities($row[4]));
echo("</td><td>\n");
echo(htmlentities($row[5]));
echo("</td><td>\n");
echo(htmlentities($row[6]));
echo("</td><td>\n");
echo('<a href="edit.php?id='.htmlentities($row[1]).'">Edit</a> 
/ ');
echo('<a 
href="delete.php?id='.htmlentities($row[1]).'">Delete</a>');
echo("</td></tr>\n");
}
echo "</br>"; 

如果我使用所有三个字段进行搜索,查询将返回相关结果。如果一个或多个字段留空,则返回整个数据库,这不是我想要的。

有更好的方法吗?

【问题讨论】:

    标签: php mysql search multiple-columns


    【解决方案1】:

    你可以用这个

    $condition="sasaaa";
    $bookTitle=trim($_POST['bookTitle']);
    $bookAuthor=trim($_POST['bookAuthor']);
    $category=trim($_POST['category']);
    if(isset($bookTitle))
       $condition="booktitle=$bookTitle";
    if(isset($bookAuthor))
       $condition="bookAuthor=$bookAuthor";
    if(isset($category))
       $condition="category=$category";
    

    并在您的 SQl 中使用此 $condition 变量。使用 mysqli_real_escape_string()。 希望对你有帮助:)

    【讨论】:

    • 感谢您的回复。我在该代码的第一 IF 行收到“致命错误:无法在写入上下文中使用函数返回值”错误。
    • 使用更新的版本,对于您在早期代码中遇到的问题,我们深表歉意。
    • 当我使用此代码时,搜索根本没有返回任何结果。
    【解决方案2】:

    最好在开始时跳过所有测试,并简单地动态构建查询,仅在设置后变量时设置where 条件。但是,如果您希望保留此逻辑(这不太好),只需将您的 NULLvalues 替换为空字符串,这应该可以解决问题...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-03
      • 1970-01-01
      • 2011-08-01
      • 1970-01-01
      • 2021-05-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多