Php mysql搜索选项

时间:2015-06-18 01:04:46

标签: 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>

1 个答案:

答案 0 :(得分:0)

这是错误的:

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对象,它返回一个布尔值,指示第一个查询是成功还是出错。您需要致电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'");