简体   繁体   中英

How to check if the user exists in the database(ms sql) in php?

I'm developing an app in php and ms sql. There's this page in the app, which has only one usser input field that takes the username of the user. Not I want to add a feature that checks if the user exists in the database then it directs him to the next page else gives an error.

<?php
require_once './pages/header.php';
require_once './functions/queries.php';



?>
<div class="container" >
            <form class="cmxform" id="Form">
                <div id="FormResult" class="hide" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <div id="resultFormContent"></div>
                </div>  
                <div class="form-group col-md-12">
                    * Indicates required field
                </div>
                <div class="form-row">
                    <div class="form-group col-md-4">
                        <label for="fName">First Name *</label>
                        <input type="text" class="form-control" id="User" name="User" required>
                    </div>
                </div>
                <div class="form-group col-md-12">
                    <button class="btn btn-info btn-primary" id="registerSubmit" type="submit">Submit</button>
                </div> 
            </form>


</div>

queries.php

<?php

require_once("db-connect.php");
class Queries{

public static function getUser($Id){
           $conn = DB::databaseConnection();
        $sql = "SELECT User FROM Admins WHERE Id = :Id";
        $stmt = $conn->prepare($sql);
        $stmt->bindParam(':Id', $Id);
        if ($stmt->execute()) {
            return $stmt->fetch(PDO::FETCH_ASSOC);
        } else {
            return null;
        } 

        }
}

The page which I want to direct the user to if he exists in the database is "read.php".

Your form need an attribute action to send data to a php page.

<form action='some-file.php'>...</form>

You need to create this file and get parameters from request and use in your function.

$userName = $_REQUEST['User'];

if (getUser($userName)) {
...
} else {
...
}

Check http://www.php.net/manual/pt_BR/function.header.php for redirect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM