数据库错误名称未获取

时间:2017-10-12 21:07:25

标签: php html mysql html5

<html>
    <head>
        <style>
            input
            {
                position:relative;
                height:5%;
                width:25%;
                border:2px solid;
                background-color:#219869;
                border-radius:10px;
                font-weight:bold;
                text-align:center;
                font-size:19px;
            }
        </style>
    </head>

    <body bgcolor='#b5eff5'>
        <form method="POST" action='criminal_search.php'>
            <select name='s1'>
                <option value="ID">ID</option>
                <option value="Name">Name</option>
                <option value="Phone Number">Phone Number</option>
                <option value="Date">Date</option>
            </select>
            <input type=text name='i1' id='i1'></input>
            <input type=submit value=submit name="submit"></input>
        </form>
    </body>
    <?php
        if(isset($_POST['submit']))
        {
            $dbname = "policainformation";
            $conn = mysqli_connect('localhost','root','',$dbname);
            if (!$conn)
            {
                die("Connection failed: " . mysqli_connect_error());
            }
            else
            {
                $option1=$_POST['s1'];
                if($option1=="ID")
                {
                    $option1="report_id";
                }
                else
                if($option1=="Name")
                {
                    $option1="reporter_name";
                }
                else
                if($option1=="Phone Number")
                {
                    $option1="reporter_ph";
                }
                else
                if($option1=="Date")
                {
                    $option1="report_date";
                }

                $option2=$_POST['i1'];
                if($option1!="Name")
                $sql="select * from criminal where $option1 in ($option2)";
                else
                $sql="select * from criminal where $option1 in '$option2'"; 
                $result=mysqli_query($conn, $sql) or die("<script type='text/javascript'>alert('No Records Found!')</script>");
                if($result)

                if (mysqli_num_rows($result) > 0)
                {
                    while($row = mysqli_fetch_assoc($result))
                    {
                        echo $row["report_id"].$row["reporter_name"].$row["reporter_ph"].$row["reporter_email"].$row["reporter_address"].$row["report_date"].$row["suspect_name"].$row["reason_of_report"].$row["proof"]."<br>";
                    }
                }
                else
                {
                    echo "<script type='text/javascript'>alert('No Records Found!')</script>";
                }
            }
        }
    ?>
</html>

这是我的HTML代码。我试图检索值并在网页上显示。除了NAME选项,一切正常。当我使用名称搜索时,即当我在PHP代码中使用名称触发查询时,它不起作用。它没有发现任何记录。请帮我。我还提供了表格结构以供参考。

https://gyazo.com/1c69aad3491c00d876128e198e39f176

https://screenshots.firefox.com/psi7MgWUjapXavID/localhost

1 个答案:

答案 0 :(得分:0)

T-SQL IN使用括号()而不是引号&#39;

$sql="select * from criminal where $option1 in ($option2)";
else
$sql="select * from criminal where $option1 = '$option2'";