Php mysql 搜索选项

标签 php mysql

我目前正在自学 PHP,现在,我正在尝试创建一个简单的图书馆系统。现在,我在使用搜索选项时遇到了一些问题。我相信我的查询和条件语句是正确的,但我仍然无法显示输出。

这是代码:

    <html>
<link rel="stylesheet" type="text/css" href="style.css">
    <head>Home Page!</head>
<h1>List of Books</h1>

<?php include 'show.php'; ?>
<?php

require 'dbcon.php';
$Terror = $Aerror = $Derror = $Cerror = $matches = "";

if ($_SERVER["REQUEST_METHOD"] == "POST"){
$T = $_POST['title'];
$A = $_POST['Author'];
$D = $_POST['Desc'];
$C = $_POST['Category'];
$X = $_POST['Opt'];

$del_sql = "delete from a where Title = '".$T."' AND Author = '".$A."' ";
"delete from b where Title = '".$T."' AND Description = '".$D."' AND Category = '".$C."' "; 

    if($X == 'Add'){
    $in_sql = "insert a (Title, Author) values ('".$T."', '".$A."'); insert b (Title, Description, Category) values ('".$T."', '".$D."', '".$C."')";
        if(empty($T) || empty($A) || empty($D) || empty($C)){
        if(empty($A)) $Aerror = "Fill up Author";
        if(empty($T)) $Terror = "Fill up Title";
        if(empty($D)) $Derror = "Fill up Description";
        if(empty($C)) $Cerror = "Fill up Category";
        }
        else if(mysqli_multi_query($dbcon, $in_sql)){
        echo "New recored added";
        }
    }
    else if ($X == 'Del'){
        if (mysqli_multi_query($dbcon, $del_sql)){
        echo "Record deleted";
        }
    }

    else if ($X == 'Search'){
        $sea1_sql = "select count(Title) from a where Title = '".$T."'";
        $sea_sql = mysqli_multi_query($dbcon, "select * from a where Title = '".$T."' AND Author = '".$A."'");
                    "select Description, Category from b where Title = '".$T."' AND Description = '".$D."' AND Category = '".$C."'";
        if (mysqli_query($dbcon, $sea1_sql) > 0){           
            while ($row = mysqli_fetch_array($sea_sql)){
                echo "<table class = fix>";
                echo "<tr><th>Title</th> <th> ". $row['Title'] . " </th></tr>";
                echo "<tr><th>Author </th> <th> ". $row['Author'] . " </th></tr>";
                echo "<tr><th>Description </th> <th> ". $row['Description'] . " </th></tr>";
                echo "<tr><th>Category </th> <th> ". $row['Category'] . " </th></tr>";
                echo "</table><br>";
            }
        }
            else if (mysqli_query($dbcon, $sea1_sql) < 0){
                echo "Can't find data!";
            }
        }

    }

    ?>

    <body>
        <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
            <br>Title: <input type="text" name="title"> <?php echo $Terror; ?><br>
            Author: <input type="text" name="Author"><?php echo $Aerror; ?><br>
            Description: <input type="text" name="Desc"><?php echo $Derror; ?><br>
            Category: <input type="text" name="Category"><?php echo $Cerror; ?><br>
        <select name="Opt">
        <option disabled selected>N/A</option>
        <option value='Add'>Add</option>
        <option value='Edit'>Edit</option>
        <option value='Del'>Del</option>

    <option value='Search'>Search</option>
    </select><br>
    <input type="submit" name="submit">



    </form>

    </body>

    </html>

最佳答案

这是错误的:

if (mysqli_query($dbcon, $sea1_sql) > 0){   

myqli_query() 不返回 count(Title) 的值。您需要获取该行。

$sea1_result = mysqli_query($dbcon, $sea1_sql);
$row = mysqli_fetch_assoc($sea1_result);
if ($row['count(Title)'] > 0) {

您还错误地使用了 mysqli_multi_query()。它不返回您可以从中获取结果的 mysqli_result 对象,它返回一个 bool 值,指示第一个查询是成功还是出错。您需要调用mysqli_use_result()获取每个查询的结果,然后在此调用 mysqli_fetch_array()

但看起来您根本不应该使用 mysqli_multi_query()。您想要从 ab 中获取相同标题的行,而不是所有 a 行都在所有 b 之前> 行。你应该加入两个表:

$sea_result = mysqli_query("SELECT a.*, b.Description, b.Category
                            FROM a JOIN b ON a.Title = b.Title
                            WHERE a.Title = '$T' AND a.Author = 'A'
                            AND b.Description = '$D' AND b.Category = '$C'");

关于Php mysql 搜索选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30904927/

相关文章:

mysql_fetch_row 和 ORDER BY - 我做错了什么?

MySQL JOIN 忽略 NULL 值

c# - 无法加载 ado.net MySQL

javascript - 将函数参数值传递给该函数内部的函数

php - Paypal 订阅按钮

php - 如何在 Slim 3 中手动启动 404 处理程序?

php - 更新php mysql中的多行

php - 在模块中查找 Prestashop 商店可用的语言

php - 这就是用 PHP 和 Ajax 防止 CSRF 所需要做的全部工作吗?

php - 返回给定时间/日期的可用性