PHP页面运行MySQL查询

时间:2016-01-03 11:08:01

标签: php html mysql

我需要一个PHP + HTML页面,它要求用户输入两个值,即密码和密码。 id_category。

到目前为止,我已将PHP编码为:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "db1";


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "select user_id,mobile from test_user_table where id_category like '912' or id_category like '912%' or id_category like '%912%' or id_category like '%912'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["user_id"]. " - Mobile: " . $row["mobile"]. "<br>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>

我需要一个HTML页面,它会要求这两个变量并传递给这个代码,即密码值为$ password&amp; id_category为select语句(而不是912)

请帮忙

2 个答案:

答案 0 :(得分:1)

html将如下所示

<!DOCTYPE html>
<html>
<title>Login Form</title>
<body>
    <form action="form_submit" method="post">
        <table>
            <tr>
                <td>Username</td>
                <td>:</td>
                <td><input type="text" name="username" /></td>
            </tr>
            <tr>
                <td>Password</td>
                <td>:</td>
                <td><input type="password" name="username" /></td>
            </tr>
            <tr>
                <td>Category</td>
                <td>:</td>
                <td><select name="category">
                        <option value="192">Category 1</option>
                        <option value="193">Category 2</option>
                        <option value="194">Category 3</option>
                        <option value="195">Category 4</option>
                    </select></td>
            </tr>
        </table>
    </form>
</body>
</html>

由于我将用户方法设为POST,因此您应该将form_submit.php上的值设为

<?php
     $username = $_POST['username'];
     $password = $_POST['password'];
     $category = $_POST['category'];
?>

答案 1 :(得分:0)

这应该是有帮助的:

<强> HTML:

<form name="login" method="post">
Username<input type="text" name="username"/>
Password<input type="password" name="password"/>
Category<input type="text" name="category"/>
<input type="submit" name="submit"/>
</form>

<强> PHP:

<?
if (isset ($_POST["submit"]){
$username = mysqli_real_escape_string( $_POST["username"]);
$password = mysqli_real_escape_string( $_POST["password"]);
$category = mysqli_real_escape_string( $_POST["category"]);


// YOUR QUERY
$sql = "
SELECT columns FROM table WHERE 
username = '$username' AND
password = '$password' AND (
categoryid LIKE '$category' OR 
categoryid LIKE '%$category' ....
)

 ";

}//if end
    ?>