检查配置文件字段是否已填写


Checking profile fields are filled out

我有一个配置文件页面,它正在提取登录用户的详细信息,我已经停止了密码的输入。因为我希望他们重新键入它作为他们更新细节的一部分。

我想要的是确保他们填写了字段,并且表单不能因缺少密码或名称等而更新。

<!-- loads the php file into here -->
<?php  
/* This script pulls the existing name input and displays it when the user logs in. */
session_start();
include("connection.php");
$query="SELECT * FROM users WHERE id='".$_SESSION['id']."' LIMIT 1";
$result = mysqli_query($link,$query);
$row = mysqli_fetch_array($result);

?>

<Style>

.alert{
display:none;
}

</Style>


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>profile</title>
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href='https://fonts.googleapis.com/css?family=Lato:400,300,100,300italic' rel='stylesheet' type='text/css'>
    <link rel="stylesheet" type="text/css" href="resources/css/profilestyles.css">  
  <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js">        </script>
  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->

</head>

 <body data-spy="scroll" data-target=".navbar-collapse">
<div class="navbar navbar-default navbar-fixed-top">
<div class="container">
    <div class="navbar-header">
        <button class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
        </button>
        <a class="navbar-brand">MyBday</a>
    </div>
        <div class="collapse navbar-collapse">

           <form class="navbar-form navbar-right" method="post">
            <ul class="nav nav-pills">
              <li role="presentation"><a href="mainpage.php">My connections</a></li>
              <li role="presentation"><a href="#">World connections</a></li>
                <li role="presentation"  class="active"><a href="profile.php">Profile</a></li>    
              <li role="presentation"><a href="#">Messages</a></li>
              <li role="presentation"><a href="index.php?logout=1">Logout</a></li>
            </ul>

            </form>
        </div>

</div>  
</div>

<div class="container">
<h1>Edit Profile</h1>
<hr>
<div class="row">
  <!-- left column -->
  <div class="col-md-3">
    <div class="text-center">
      <img src="//placehold.it/100" class="avatar img-circle" alt="avatar">
      <h6>Upload a different photo...</h6>
      <input class="form-control" type="file">
    </div>
  </div>
  <!-- edit form column -->
  <div class="col-md-9 personal-info">
    <div class="alert alert-success alert-dismissable">
      <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button> 
      <strong>Profile updated.</strong>
    </div>
    <h3>Personal info</h3>
    <form class="form-horizontal" role="form" action="edit_profile.php" method="post">
      <div class="form-group">
        <label class="col-lg-3 control-label name">name:</label>
        <div class="col-lg-8">
         <input class="form-control" value="<?php echo $row['name'];?>" type="text" name="name">
        </div>
      </div>
      <div class="form-group">
        <label class="col-lg-3 control-label">Email:</label>
        <div class="col-lg-8">
          <input class="form-control" value="<?php echo $row['email'];?>" type="text" name="email">
        </div>
      </div>
      <div class="form-group">
        <label class="col-lg-3 control-label">DOB:</label>
        <div class="col-lg-8">
          <input class="form-control" value="<?php echo $row['DOB'];?>" type="date" name="DOB">
        </div>
      </div>
      <div class="form-group">
        <label class="col-lg-3 control-label">Country</label>
        <div class="col-lg-8">
          <input class="form-control" value="<?php echo $row['country'];?>" type="text" name="country">
        </div>
      </div>
      <div class="form-group">
        <label class="col-md-3 control-label">Re-enter password:</label>
        <div class="col-md-8">
          <input class="form-control" value="" placeholder="At least 8 characters and 1 cap letter" type="password" name="password">
        </div>
      </div>
      <div class="form-group">
        <label class="col-md-3 control-label">Confirm password:</label>
        <div class="col-md-8">
          <input class="form-control" value="" placeholder="At least 8 characters and 1 cap letter" type="password" name="password">
        </div>
      </div>
      <div class="form-group">
        <label class="col-md-3 control-label"></label>
        <div class="col-md-8">
          <input class="btn btn-primary" value="Save Changes" type="submit">
          <span></span>
          <input class="btn btn-default" id="updated" value="Cancel" type="reset">
        </div>
      </div>
    </form>
  </div>

     <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
    <script>
     $("#updated").click(function(){
  $(".alert").hide().show('medium');
});
     </script>
 </body>
 </html>

我有这个脚本,我已经从登录中复制了它,但不确定我是如何将它实现到这个配置文件页面中的。

// check name is valid
    if (!$_POST['name']) $error.="<br />Please enter your name";

// check town is valid
//    if (!$_POST['town']) $error.="<br />Please enter your town/city.";
// check country is valid
    if (!$_POST['country']) $error.="<br />Please enter your country.";

// check DOB is valid
    if (!$_POST['DOB']) $error.="<br />Please enter your Date of birth.";        
// check email and password exist and are valid
    if (!$_POST['email']) $error.="<br />Please enter your email";
// if email exists check it is valid if not display error message
    if (!$_POST['email']) $error.="<br />Please enter your email";
            else if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))  $error.="<br />Please enter a valid email";
// password validation
    if (!$_POST['password']) $error.="<br />Please enter your password";
    else { 
// if they have entered a password check how long it is
        if (strlen($_POST['password'])<8) $error.="<br />please enter a password with at least 8 characters";
// check if it doesn't have a cap letter in it            
        if(!preg_match('/[A-Z]/', $_POST['password'])) $error.= "<br     />Please include min 1 capital letter in your password.";
            }
    if ($error) $error = "There were error(s) in your sign up details:".$error;

正如我认为看到php很明显的那样,sql对我来说是全新的,所以尽我所能去理解它,所以要温和地解释,就像我5岁一样。

提前谢谢。

您只需将required添加到输入字段中,然后在输入您添加required的所有值之前,它们就无法提交,更多关于必填项的信息:

http://www.w3schools.com/tags/att_input_required.asp

编辑:

使用php进行检查如下所示:

/ check name is valid
if (!$_POST['name'] || $_POST['name']=="") $error.="<br />Please enter your name";

因此,只需添加到每个字段|| $_POST['nameofinput']=="",即服务器端检查+来自html 的required