搜索数据库单选按钮

时间:2017-10-04 06:01:26

标签: php

真的需要你们所有人的帮助。 我想使用单选按钮在三种不同类型的关键字之间进行搜索,现在我的代码出现问题。 此代码仅在第一种情况下成功,仅表示案例'名称'。另外两个案例根本不起作用。请帮帮我......

<table border="0" cellspacing="3" cellpadding="3">
    <tbody>
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <tr align="center">
            <td><input type="radio" name="RBsearch" value="name" />name
                <input type="radio" name="RBsearch" value="password" />password
                <input type="radio" name="RBsearch" value="role" />role
            </td>
        </tr>
        <tr align="center">
            <td>Search : <input type="text" name="txtSearch" value="" size="30" />
                <input type="submit" value="Search" name="btnSearch" /></td>
        </tr>
    </form>
    <?php
    $host = "localhost";  
    $user = "phpstart";   
    $password = "";    
    $db_name = "phpshop"; 

    $con = mysql_connect($host, $user, $password) or die ('unable to connect!' . mysql_error());
    mysql_select_db("phpshop", $con);

    $radio = isset($_POST['RBsearch']);
    switch ($radio) {
        case 'name' : $result = mysql_query("SELECT * FROM user  WHERE  name LIKE '$_POST[txtSearch]' ");
            break;
        case 'password' : $result = mysql_query("SELECT * FROM user WHERE password LIKE '$_POST[txtSearch]' ");
            break;
        case 'role' : $result = mysql_query("SELECT * FROM user  WHERE role LIKE '$_POST[txtSearch]' ");
            break;
        default :  $result = mysql_query("SELECT * FROM  user ");
            break;
    }
    ?>
    <tr>
        <td align="center"><table border="1" cellspacing="3" cellpadding="3">
            <thead>
                <tr>
                    <th width="100">id</th>
                    <th width="200">name</th>
                    <th width="200">email</th>
                    <th width="200">password</th>
                    <th width="200">role</th>
                </tr>
            </thead>
            <tbody>
            <?php
            while ($row = mysql_fetch_array($result)) {
                echo "<tr>";
                echo "<td align=\"center\">". $row['id'] ."</td>";
                echo "<td>". $row['name'] ."</td>";
                echo "<td>". $row['email'] ."</td>";
                echo "<td>". $row['password'] ."</td>";
                echo "<td>". $row['role'] ."</td>
                </tr>";
            }
            ?>
            </tbody>
        </table>
        </td>
        </tr>
    </tbody>
</table>

0 个答案:

没有答案