根据用户输入搜索数据库

时间:2016-12-01 01:27:45

标签: php

我是PHP的新手。我正在寻找功能,但它不能很好地工作,我找不到原因。问题是; $查询已被发送并被接受但是无法在数据库中找到$ query ,即使存在 $ query 。我认为, $ sql命令在某处可能是错误的,但无论如何都找不到它。谢谢。

这是我的代码:asset_search.php

<?php
//Search data in database
$query = $_GET['query'];
$min_length = 3;

if(strlen($query) >= $min_length)
{
    //$query = htmlspecialchars($query);
    //$query = mysql_real_escape_string($query);
    $query = strtoupper($query);
    $sql = "SELECT * FROM asset WHERE ('asset_name' LIKE '%".$query."%')";
    $result = mysqli_query($conn, $sql);
    $row_cnt = mysqli_num_rows($result);

    $count = 0;

    if($row_cnt > 0)
    {
        echo "<table style='padding: 5px; font-size: 15px;'>";
        echo "<tr><th style='width: 30px; border: 1px solid black; align:'center''>No</th>";
        echo "<th style='width: 200px; border: 1px solid black; align:'center''>Status</th>";
        echo "<th style='width: 200px; border: 1px solid black; align:'center''>Asset Sub-identifier</th>";
        echo "<th style='width: 200px; border: 1px solid black; align:'center''>Asset Name</th>";
        echo "<th style='width: 200px; border: 1px solid black; align:'center''>Asset Type</th>";
        echo "<th style='width: 200px; border: 1px solid black; align:'center''>Brand</th>";
        echo "<th style='width: 200px; border: 1px solid black; align:'center''>Service Tag/ Product Tag/ Product S/N</th>";
        echo "<th style='width: 200px; border: 1px solid black; align:'center''>CSM Tag</th>";
        echo "<th style='width: 200px; border: 1px solid black; align:'center''>Action</th></tr>";

        while($row = mysqli_fetch_assoc($result))
        {
             echo "<tr><td align='center'>" . ++$count . "</td>";
             echo "<td align='center'>" . $row["asset_status"] . "</td>";
             echo "<td align='center'><a href='asset_viewfull.php?asset_id=" . $row["asset_id"] . "'><ins>" . $row["asset_subidentifier"] . "</a></ins></td>";
             echo "<td align='center'>" . $row["asset_name"] . "</td>";
             echo "<td align='center'>" . $row["asset_type"] . "</td>";
             echo "<td align='center'>" . $row["asset_brand"] . "</td>";
             echo "<td align='center'>" . $row["asset_sertag"] . "</td>";
             echo "<td align='center'>" . $row["asset_csmtag"] . "</td>";

             if($row["asset_status"] == "DISPOSE")
             {
                    echo "<td align='center'><a href='asset_delete.php?asset_id=" . $row["asset_id"] . "'>Delete</a>";
                    echo " ";
                    echo "<a href='asset_print.php?asset_id=" . $row["asset_id"] . "'>Print</a></td></tr>";
             }else
             {
                    echo "<td align='center'><a href='asset_editform.php?asset_id=" . $row["asset_id"] . "'>Edit</a>";
                    echo " ";
                    echo "<a href='asset_delete.php?asset_id=" . $row["asset_id"] . "'>Delete</a>";
                    echo " ";
                    echo "<a href='asset_disposeform.php?asset_id=" . $row["asset_id"] . "'>Dispose</a>";   
                    echo " ";
                    echo "<a href='asset_print.php?asset_id=" . $row["asset_id"] . "'>Print</a></td></tr>"; 
             }
        }

       }else
       {
          echo "<tr> There is no asset in the database </tr>";
       }
       echo "</table>";
}
else
{
    echo "<script languange = 'Javascript'>
            alert('Minimum length is' .$min_length);</script>";
}

//Close connection
mysqli_close($conn);
$count = 0;

&GT;

2 个答案:

答案 0 :(得分:0)

将您的查询更改为以下内容:

  

SELECT * FROM asset WHERE(`asset_name` LIKE'%“。$ query。”%')

注意``around asset_name而不是''

答案 1 :(得分:0)

你应该在没有括号的情况下尝试这一点,有时它会拖出搜索,

$sql = "SELECT * FROM asset WHERE `asset_name` LIKE '%{$query}%'";

这就是我如何完成这项任务并且从未让我失望过!