我该如何解决以下代码以消除错误?

时间:2019-06-20 12:11:02

标签: php html mysql database

我正在创建一个简单的登录/注册系统,当我要去测试所有内容时,快要完成了,每个项目都会弹出以下错误。

  

注意:未定义索引:用户名位于   第18行的C:\ xampp \ htdocs \ websitestuff \ server.php

     

通知:未定义索引:password_1 in   第19行的C:\ xampp \ htdocs \ websitestuff \ server.php

     

注意:未定义索引:password_2 in   第20行的C:\ xampp \ htdocs \ websitestuff \ server.php

     

注意:未定义索引:以电子邮件发送   第21行的C:\ xampp \ htdocs \ websitestuff \ server.php

     

致命错误:未捕获错误:调用未定义函数   C:\ xampp \ htdocs \ websitestuff \ server.php中的mysqli_fetch_query():36   堆栈跟踪:#0 C:\ xampp \ htdocs \ websitestuff \ registration.php(1):   include()#1 {main}放在C:\ xampp \ htdocs \ websitestuff \ server.php中   在第36行

我浏览了整个网络,但没有遇到我了解的解决方案。 我已经链接了server.php文件的整个代码,以防它链接到问题线以外的其他地方。

我使用的是XAMPP的最新版本,因此我假设它也具有MySQL和php更新的最新版本。这也是来自本地主机。

<?php

session_start();

//initializing variables

$username = "";
$email = "";

$errors = array();

//connect to db

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

//Register users

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

//Form Validation

if(empty($username)) {array_push($errors, "Username is required"); }
if(empty($password_1)) {array_push($errors, "Password is required"); }
if(empty($email)) {array_push($errors, "Email is required"); }
if($password_1 != $password_2){array_push($errors, "Passwords do not 
match"); }

//Check db for existing user with same username

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


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

if($user) {

    if($user['username'] === $username){array_push($errors, "Username 
    already exists");}
    if($user['email'] === $email){array_push($errors, "This email is 
    already registered to a username");}

}

//Register the user if no error

if(count($errors) == 0 ){

    $password = md5($password_1); //This will encrypt the password
    $query = "INSERT INTO user (username, password, email) VALUES 
('$username', '$password', 'email')";

    mysqli_query($db, $query);
    $_SESSION['username'] = $username;
    $_SESSION['success'] = "You are now logged in";

    header('location: index.php');

}

0 个答案:

没有答案