使用PHP中的数据库值检查用户输入值

时间:2017-05-26 11:59:13

标签: php mysql

我是编程新手。我实际上想用数据库中的存储值检查用户输入值。特别是,用户提供的电子邮件和密码,打印相应电子邮件ID的详细信息。这是我的代码。请帮我纠正我的代码。

<?php
    $conn = mysqli_connect('localhost', 'root', '', 'mydb');
    $sql = "SELECT email, first FROM register";
    $result = mysqli_query($conn, $sql);

    if (mysqli_num_rows($result) > 0) {
        while($row = mysqli_fetch_assoc($result)) {
            echo "Welcome " . $row["first"]. "<br>". "Your Email Id is: " . $row["email"] . "<br>";
        }       
    } else {
        echo "0 results";
    }
    mysqli_close($conn);
?>

3 个答案:

答案 0 :(得分:0)

<?php
    $conn       = mysqli_connect('localhost', 'root', '', 'mydb');
    $username   = $_POST['username'];
    $password   = $_POST['password'];
    $query      = "select * from register where email = '$username'";
    $resultSet  = mysqli_query($query, $conn);
    if(mysqli_num_rows($resultSet) > 0){
        $row    = mysqli_fetch_assoc($resultSet);
        if($row['password'] == $password){ // if you are using encryption like md5 or anything else then you have to add in this line accordingly
            echo "Good, Logged In!";
        }else{
            echo "Oh No, password not correct!";
        }
    }else{
        echo "Please enter correct email!";
    }

答案 1 :(得分:0)

<?php
        $conn = mysqli_connect('localhost','root','','mydb');
        $sql = "SELECT email,first FROM register where email=$_POST['form_ref_name'];
        $result = mysqli_query($conn,$sql);
        if($result){
        if (mysqli_num_rows($result) > 0) {
        while($row = mysqli_fetch_assoc($result)) {
        echo "Welcome " . $row["first"]. "<br>". "Your Email Id is: " . $row["email"] . "<br>";
            }       
        }
       }
        else {
        echo "0 results";
        }
        mysqli_close($conn);
    ?>
试试这可能有所帮助。 Insted你可以使用$ row varible它包含来自DB的值。

答案 2 :(得分:0)

你有使用条件,如下所述。我认为帮助低于查询。

<?php 
$email = $_POST['email'];
$password= $_POST['password'];

$sql = mysql_query("SELECT email,first FROM register WHERE email = $email and password = $password");
?>