dsfsdf7852 2018-09-13 16:36 采纳率: 100%
浏览 39

如何检查用户是否存在于php中的数据库(ms sql)?

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".

  • 写回答

1条回答 默认 最新

  • doubang4881 2018-09-13 17:23
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法