javascript - 使用 php 和 mysql 表单上的多个搜索字段

标签 javascript php html mysql

我已经被这个问题困扰了几天了。 我要开发的系统逻辑在这里

enter image description here

这是图像的代码

    <?php 
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Search</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <!-- <link rel="stylesheet" type="text/css" href="style.css"/> -->
</head>
<body>
    <form action="process/searchprocess.php" method="GET">
        <table width="100%">
            <tr>
                <th>
                    Client Ic<br><input type="text" name="client_name" />
                </th>
                <th>
                    Client Ic<br><input type="text" name="client_ic" />
                </th>
                <th>
                    Client Address <br><input type="text" name="client_add" />
                </th>
            </tr>
        </table>
        <table width="100%">
            <tr>
                <th>
                    <br><input type="submit" value="Search" align="center" />
                </th>
            </tr>
        </table>
    </form>
</body>
</html>

这是我的流程代码

    <?php
    session_start();
    mysql_connect("localhost", "root", "") or die("Error connecting to database: ".mysql_error());

    mysql_select_db("waveevo") or die(mysql_error());
    ?>


        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title>Search results</title>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <!-- <link rel="stylesheet" type="text/css" href="style.css"/> -->
            <style>
            table tr:nth-child(even) {
                background-color: #eee;
            }
            table tr:nth-child(odd) {
               background-color:#fff;
            }
            table th {
                background-color: black;
                color: white;
            }
            </style>
        </head>
        <body>

    <?

php
    $query = $_GET['client_name'];
    $query2 = $_GET['client_ic'];
    $query3 = $_GET['client_add'];

    if ($query == null && $query2 == null && $query3 == null)
    {
        echo "Please at least insert one the value";
    }

    else
    {     
        $query = htmlspecialchars($query);
        $query2 = htmlspecialchars($query2);
        $query3 = htmlspecialchars($query3);

        $query = mysql_real_escape_string($query);
        $query2 = mysql_real_escape_string($query2);
        $query3 = mysql_real_escape_string($query3);

        $raw_results = mysql_query("SELECT * FROM client WHERE ('client_name' LIKE '%".$query."%') OR ('client_ic' LIKE '%".$query2."%') OR ('client_add_1' && ' ' && 'client_add_2' && ' ' && 'client_add_3' && ' ' && 'client_add_4' LIKE '%".$query3."%')") or die(mysql_error());;

        if(mysql_num_rows($raw_results) > null){ // if one or more rows are returned do following

            while($results = mysql_fetch_array($raw_results)){
                ?>
                <table width="100%">
                    <tr>
                        <th>ID</th>
                        <th>Name</th>
                        <th>IC</th>
                        <th>Mobile</th>
                        <th>Address</th>
                        <th>Marital Status</th>
                        <th>Race</th>
                        <th>Asset Type</th>
                        <th>Bank</th>
                        <th>Amount</th>
                        <th>Nationality</th>
                        <th>Limit</th>
                    </tr>
                    <tr>
                        <td><?php echo $results["client_id"]; ?></td>
                        <td><?php echo $results["client_name"]; ?></td>
                        <td><?php echo $results["client_ic"]; ?></td>
                        <td><br><?php echo $results["client_mobile_1"]."<br>".$results["client_mobile_2"]."<br>".$results["client_mobile_3"]; ?></td>
                        <td><?php echo $results["client_add_1"]."<br>".$results["client_add_2"]."<br>".$results["client_add_3"]."<br>".$results["client_city"]."<br>".$results["client_postcode"]; ?></td>
                        <td><?php echo $results["client_marital_status_id"]; ?></td>
                        <td><?php echo $results["client_race_id"]; ?></td>
                        <td><?php echo $results["client_asset_type_id"]; ?></td>
                        <td><?php echo $results["client_bank_id"]; ?></td>
                        <td><?php echo $results["amount"]; ?></td>
                        <td><?php echo $results["client_nationality_id"]; ?></td>
                        <td><?php echo $results["client_limit"]; ?></td>
                    </tr>
                </table>

            <?php
            }

        }



        else{ // if there is no matching rows do following
            echo "No results";
        }
    }
?>
</body>

所以我尝试输入客户端IC的值,例如1234,数据库中没有数据与我刚才输入的值匹配,但结果仍然显示有,我可以知道为什么吗,因为我已经没有解决这个问题的方法

最佳答案

那是因为当你使用通配符时 LIKE '%".$query."%' 如果你的变量 $query 为空,你就会得到全部,因为你与一切都像“%%”。 您需要更改这句话:

$raw_results = mysql_query("SELECT * FROM client WHERE ('client_name' LIKE '%".$query."%') OR ('client_ic' LIKE '%".$query2."%') OR ('client_add_1' && ' ' && 'client_add_2' && ' ' && 'client_add_3' && ' ' && 'client_add_4' LIKE '%".$query3."%')") or die(mysql_error());;

有这样的事情

$sql_query="SELECT * FROM client WHERE ";
$other=false;    

if ($query != null and $query!="") {
    $sql_query=$sql_query."('client_name' LIKE '%".$query."%')";
    $other=true;
}

if ($query2 != null and $query2!="") {
    if ($other) {
        $sql_query=$sql_query." OR ";    
    }
    $sql_query=$sql_query."('client_ic' LIKE '%".$query2."%')";
    $other=true;
}
if ($query3 != null and $query3!="") {
    if ($other) {
        $sql_query=$sql_query." OR ";    
    }
    $sql_query=$sql_query."('client_add_1' && ' ' && 'client_add_2' && ' ' && 'client_add_3' && ' ' && 'client_add_4' LIKE '%".$query3."%')";
    $other=true;
}    
if ($other) {
    $raw_results = mysql_query($sql_query) or die(mysql_error());
}

关于javascript - 使用 php 和 mysql 表单上的多个搜索字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44237228/

相关文章:

php - 如何在我的网站上显示来自 Facebook 的个人资料?

php - 如何将登录的用户数据输入 Laravel Controller

html - Dreamweaver 缩小代码问题

html - 中心 DIV 相对于菜单 DIV 旁边的父 DIV

javascript - turfjs 具有多边形差异的意外形状()

javascript - Javascript元素创建 Canvas 时出错

javascript - 为什么 grunt-contrib-jasmine 挂起?

php - 我永远无法执行 SQL 安全查询?

javascript - 使用 addClass() 切换 Canvas 外菜单

javascript - Chartjs 工具提示 anchor 在条形图上的位置