Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中验证唯一的用户ID,如果不是唯一的,则返回错误_Php_Mysql_Validation_Userid - Fatal编程技术网

在PHP中验证唯一的用户ID,如果不是唯一的,则返回错误

在PHP中验证唯一的用户ID,如果不是唯一的,则返回错误,php,mysql,validation,userid,Php,Mysql,Validation,Userid,我试图强迫用户选择一个唯一的用户名,如果他们不选择,则返回一条错误消息。所有其他验证(密码匹配等)正在工作,但验证未使用的用户名只会返回ID注册失败消息 我已经按照要求用HTML更新了这个 我只想让它告诉用户我在这些情况下概述的错误 <?php require('connect.php'); if(isset($_POST) & !empty($_POST)){ // If the values are posted, insert them int

我试图强迫用户选择一个唯一的用户名,如果他们不选择,则返回一条错误消息。所有其他验证(密码匹配等)正在工作,但验证未使用的用户名只会返回ID注册失败消息

我已经按照要求用HTML更新了这个

我只想让它告诉用户我在这些情况下概述的错误

            <?php
require('connect.php');
if(isset($_POST) & !empty($_POST)){   

// If the values are posted, insert them into the database.
// if (isset($_POST['app_id']) && isset($_POST['password'])){

    $app_id = mysqli_real_escape_string($connection, $_POST['app_id']);
    $first = mysqli_real_escape_string($connection, $_POST['first']);
    $last = mysqli_real_escape_string($connection, $_POST['last']);
    $gender = mysqli_real_escape_string($connection, $_POST['gender']);
    $birth = mysqli_real_escape_string($connection, $_POST['birth']);
    $email = mysqli_real_escape_string($connection, $_POST['email']);
    $password = mysqli_real_escape_string($connection, md5($_POST['password']));
    $confirmpassword = mysqli_real_escape_string($connection, md5($_POST['confirmpassword']));

    if($password == $confirmpassword){
        $fmsg = "";

    //username validation
        $newidvalq = "SELECT * FROM 'user' WHERE app_id='$app_id'";
        $newidres = mysqli_query($connection, $newidvalq);
        $idcount = 0;
        $idcount = mysqli_num_rows($newidres);
        if($idcount >= 1){
            $fmsg .= "That app ID is already being used, please try a different ID";
        }

    //email validation
        $emailvalq = "SELECT * FROM 'user' WHERE email='$email'";
        $emailres = mysqli_query($connection, $emailvalq);
        $emailcount = 0;
        $emailcount = mysqli_num_rows($emailres);
        if($emailcount >= 1){
        $fmsg .= "That email is already being used";
        }

    //DB Insert
    $query = "INSERT INTO `user` (app_id, first, last, gender, birth, password, email) VALUES ('$app_id', '$first', '$last', '$gender', '$birth', '$password', '$email')";          

    //Result Validation 
    $result = mysqli_query($connection, $query);
    if($result){    
        $smsg = "app ID Created Successfully"; 
      }else{
        $fmsg .= "app Registration Failed";
      }
    }else{
        $fmsg = "Your Passwords do not match";
    }   
}
 ?>
<html>
<head>
<title>User Registeration Using PHP & MySQL</title>

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" >

<link rel="stylesheet" href="styles.css" >

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <form class="form-signin" method="POST">

  <?php if(isset($smsg)){ ?><div class="alert alert-success" role="alert"> <?php echo $smsg; ?> </div><?php } ?>
  <?php if(isset($fmsg)){ ?><div class="alert alert-danger" role="alert"> <?php echo $fmsg; ?> </div><?php } ?>
    <h2 class="form-signin-heading">Please Register</h2>
    <div class="input-group">
  <span class="input-group-addon" id="basic-addon1">@</span>

  <input type="text" name="app_id" class="form-control" placeholder="app ID" value="<?php if(isset($app_id) & !empty($app_id)) {echo $app_id;} ?>" required>
</div>

    <input type="text" name="first" class="form-control" placeholder="First Name" value="<?php if(isset($first) & !empty($first)) {echo $first;} ?>"required>

    <input type="text" name="last" class="form-control" placeholder="Last Name" value="<?php if(isset($last) & !empty($last)) {echo $last;} ?>"required>

    <input type="text" name="gender" class="form-control" placeholder="Gender" value="<?php if(isset($gender) & !empty($gender)) {echo $gender;} ?>"required>

    <input type="date" name="birth" class="form-control" placeholder="Birthday" required>

    <label for="inputEmail" class="sr-only">Email Address</label>
    <input type="email" name="email" id="inputEmail" class="form-control" placeholder="Email address" value="<?php if(isset($email) & !empty($email)) {echo $email;} ?>"required autofocus>


    <label for="inputPassword" class="sr-only">Password</label>
    <input type="password" name="password" id="inputPassword" class="form-control" placeholder="Enter a Password" required>
    <label for="inputPassword" class="sr-only">RetypePassword</label>
    <input type="password" name="confirmpassword" id="inputPassword" class="form-control" placeholder="Confirm Your Password" required>

    <div class="checkbox">
      <label>
        <input type="checkbox" value="remember-me"> Remember me
      </label>
    </div>
    <button class="btn btn-lg btn-primary btn-block" type="submit">Register</button>
    <a class="btn btn-lg btn-primary btn-block" href="login.php">Login</a>
  </form>
</div>

</body>

</html>

使用前先用0分配
$idcount
,然后在if条件下使用
($idcon>=1)

根据您的逻辑,如果用户ID和电子邮件也存在,那么您插入的数据根据您的需要毫无意义

将插入块括在:

如果($fmsg==”){..to here..}

所以,若有错误,将不会向数据库插入任何记录,并显示错误

将动作属性添加到表单标记,如:

<form class="form-signin" method="POST" action="">


在某些版本的html而不是html5中,它是必需的。

db表格字段用户id是否为整数?如果您将其与字符串用户名进行比较,那么它将始终失败。您可能需要一个新的表列来以正确的格式存储用户名。我是否在某个地方将user\u id声明为整数?user表中的user\u id是什么数据类型?您是否也可以发布html代码?我做了您建议的更改,仍然没有任何改动。超级怪异。检查您的查询和数据库当用户ID和电子邮件不在db plus use action=“”中时,您是否可以发送输出