Sanchit
Sanchit

Reputation: 727

PHP page to run MySQL query

I need a PHP+HTML page which would ask for two values from user i.e. password & id_category.

So far I have coded the PHP as:

<?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();
?>

I need a HTML page which would ask for those two variables and pass on to this code i.e. value of password to $password & id_category to the select statement (instead of 912)

Pls help

Upvotes: 1

Views: 154

Answers (2)

devpro
devpro

Reputation: 16117

This should be help:

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
    ?> 

Upvotes: 0

Anto S
Anto S

Reputation: 2449

You html will be something look like below

<!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>

Since I user method as POST you should get the value on form_submit.php as

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

Upvotes: 1

Related Questions