为什么在server.php中出现索引错误

时间:2019-06-10 00:05:40

标签: php html forms

我在索引方面遇到问题

注意:未定义的索引:第34行的D:/ ****** server.php中的用户名

注意:未定义索引:第35行中D:/ ****** server.php中的电子邮件

注意:未定义的索引:第36行的D:/ ****** server.php中的password_1

注意:未定义的索引:第37行的D:/ ****** server.php中的password_2

警告:mysqli_fetch_assoc()期望参数1为mysqli_result,在第50行的D:/ ****** server.php中给出布尔值

?php 

session_start();

//initializing a variable
$username = '';
$email = '';

$errors = array();
//connect to DB

$db =mysqli_connect('localhost','root','','form') or die("could not connect to database");

// Register User

$username = mysqli_real_escape_string($db, $_POST['username']);
$email = mysqli_real_escape_string($db, $_POST['email']);
$password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
$password_2 = mysqli_real_escape_string($db, $_POST['password_2']);

//form validation
if(empty($username)){array_push($errors, "Username is Required");}
if(empty($email)){array_push($errors, "Email is Required");}
if(empty($password_1)) {array_push($errors, "Password is Required");}
if($password_1 != $password_2){array_push($errors, "Passowrds do not match");}    

//check db for existing user name

$user_check_query = "SELECT * FROM user WHERE username = '$username' or email = $email' LIMIT 1";

$results = mysqli_query($db, $user_check_query);
$user =mysqli_fetch_assoc($results);

if($user) 
{
     if($user['username'] === $username){array_push($errors, "User Already Exists");}
     if($user['email'] === $email){array_push($errors, "Email Already Exists");} 

}
//Register the user if no error

if(count($errors) === 0)
{
    $password = md5($password_1);  //to encrypt password
    $query = " INSERT INTO 'user' (username, email,  password) VALUES('$username', '$email', '$password')";
    mysqli_query($db,$query);
    $_SESSION['username'] = $username;
    $_SESSION['success'] = "You are now logged in";
    header('location: index.php');


}

即使我尝试过

if (isset($_POST['username']))
{
$username = $_POST['username'];
 }
if (isset($_POST['email']))
 {
 $email= $_POST['email'];
 }
 if (isset($_POST['password_1']))
 {
 $password_1= $_POST['password_1'];
 }
if (isset($_POST['password_2']))
 {
 $password_2= $_POST['password_2'];
 }

0 个答案:

没有答案