php形式undefined vairable

时间:2016-06-09 13:32:44

标签: php mysqli

有人可以帮助我(新手提醒)我将登录/注册/管理页面放在一起。

当我认为我已经在这个特定页面上定义了变量时,我不知道为什么我会得到未定义的变量。

  

注意:未定义的变量:第34行的F:\ Uni \ log-on \ mysql-project-users-add.php中的fname

     

注意:未定义的变量:在第42行的F:\ Uni \ log-on \ mysql-project-users-add.php中插入

<?php

$host = 'localhost';
$user = 'tim_williams';
$pass = 'baroness';
$db  = 'php_db05';

$link = mysqli_connect($host, $user, $pass, $db);

if(!$link) {
  die("Database connection failed: " . mysqli_connect_error());
}


if(isset($_POST['submit'])) {
  $fname     = mysqli_real_escape_string($link, $_POST['fname']);
  $lname     = mysqli_real_escape_string($link, $_POST['lname']);
  $email     = mysqli_real_escape_string($link, $_POST['email']);
  $password1 = mysqli_real_escape_string($link, $_POST['password1']);
  $password2 = mysqli_real_escape_string($link, $_POST['password2']);
  if($password1 != $password2) {
    echo "Error : Passwords must match each other";
  }else{
    $password = sha1(mysqli_real_escape_string($link, $_POST['password1']));
    $username  = strtolower($fname.substr($lname,0,1));
    $dateTime   = date('Y-m-d g:i:s',time());
  }
}





if ($fname && $lname && $email && $password && $username && $dateTime) {
    $qry  =  "INSERT INTO registeredusers (UserID, UserName, FirstName,  Surname, EmailAddress, Password, LastLogin)
              VALUES ('','$username','$fname', '$lname', '$email',  '$password','$dateTime')";
    $insert = mysqli_query($link, $qry);

}


if($insert) {
  header("Location: mysql-project-users-login.php");
  exit;
}

mysqli_close($link);


?>


<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="./css/layout.css" media="screen" type="text/css">
    <link rel="stylesheet" href="./css/menu.css" media="screen" type="text/css">
    <meta charset="utf-8">
    <title>New User Registration</title>
  </head>
  <body>
    <div class="holder">

      <div class="header"></div>
      <div class="navbar">
        <nav>
          <ul>
            <li><a href="mysql-project-users-login.php">Login</a></li>
            <li><a href="mysql-project-users-add.php">Register</a></li>
          </ul>
        </nav>
      </div>
      <div class="content">
        <div class="pageheading">
          <h1>Register</h1>
        </div>
        <div class="contentleft">
          <h2>Welcome to my site</h2><br />
          <h6>Please register an account with us to access main content and more.</h6>
        </div>
        <div class="contentright">
          <form class="registerform" action="" method="post">
            <input class="styletxtfield" type="text" name="fname" placeholder="First Name" value=""><input class="styletxtfield forminput" type="text" name="lname" placeholder="Last Name" value=""><br /><br />
            <input class="styletxtfield" type="text" name="email" placeholder="Email address" value=""><br /><br />
            <input class="styletxtfield" type="password" name="password1" placeholder="Password" value=""><input class="styletxtfield forminput" type="password" name="password2" placeholder="Confirm Password" value=""><br /><br />
            <input type="submit" name="submit" value="submit">
          </form>
        </div>

      </div>
      <div class="footer"></div>

    </div>

</body>
</html>

1 个答案:

答案 0 :(得分:1)

没有,当你没有发送表格时,也就是你第一次进入你的页面,那么你没有任何地方初始化这些变量。您必须添加if命令:

if ($fname && $lname && $email && $password && $username && $dateTime) {
...
}

在这个IF中:

if(isset($_POST['submit'])) {