php - SQL数据库不存储用户输入

标签 php html mysql database

我创建了一个注册表单,其中包含用户名、用户名、电子邮件和密码。我还使用 XAMPP 控制面板创建了一个 sql 数据库,将数据库命名为“registration”,并创建了一个名为“users”的表来存储所有输入。

当用户输入这些数据时,他们应该看到登录页面,并且在后台数据应该存储在数据库中。但是当我打开 phpmyadmin 来检查表时,没有保存任何数据。

下面是我用来将用户输入发送到数据库(即我的“server.php”文件)的代码:

<?php
session_start();

// initializing variables
$name = "";
$email = "";
$username = "";
$errors = array(); 

// connect to the database
$db = mysqli_connect('localhost', 'root', 'root', 'registration');

// REGISTER USER
if (isset($_POST['reg_user'])) {
  // receive all input values from the form
  $name = mysqli_real_escape_string($db, $_POST['name']); 
  $email = mysqli_real_escape_string($db, $_POST['email']);
  $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']);

  // form validation: ensure that the form is correctly filled ...
  // by adding (array_push()) corresponding error unto $errors array
  if (empty($name)) { array_push($errors, "Name is required");  
  }
  if (empty($email)) { array_push($errors, "Email is required"); 
  }
  if (empty($username)) { array_push($errors, "Username is required"); }
  if (empty($password_1)) { array_push($errors, "Password is required"); }
  if ($password_1 != $password_2) {
    array_push($errors, "The two passwords do not match");
  }

  // first check the database to make sure 
  // a user does not already exist with the same username and/or email
  $user_check_query = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1";
  $result = mysqli_query($db, $user_check_query);
  $user = mysqli_fetch_assoc($result);

  if ($user) { // if user exists
    if ($user['username'] === $username) {
      array_push($errors, "Username already exists");
    }

    if ($user['email'] === $email) {
      array_push($errors, "email already exists");
    }
  }

  // Finally, register user if there are no errors in the form
  if (count($errors) == 0) {
    $password = md5($password_1);//encrypt the password before saving in the database

    $query = "INSERT INTO users (name, email, username, password) 
              VALUES('$name', '$email', '$username', '$password')";
    mysqli_query($db, $query);
    $_SESSION['username'] = $username;
    $_SESSION['success'] = "You are now logged in";
    header('location: login.php');
  }
}

// ... 
// ... 

// LOGIN USER
if (isset($_POST['login_user'])) {
  $username = mysqli_real_escape_string($db, $_POST['username']);
  $password = mysqli_real_escape_string($db, $_POST['password']);

  if (empty($username)) {
    array_push($errors, "Username is required");
  }
  if (empty($password)) {
    array_push($errors, "Password is required");
  }

  if (count($errors) == 0) {
    $password = md5($password);
    $query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
    $results = mysqli_query($db, $query);
    if (mysqli_num_rows($results) == 1) {
      $_SESSION['username'] = $username;
      $_SESSION['success'] = "You are now logged in";
      header('location: https://georginahughes48.wixsite.com/makeupyourmind');
    }else {
        array_push($errors, "Wrong username/password combination");
    }
  }
}

?>

如果需要任何进一步的代码来帮助我解决此问题,请告诉我。提前致谢!

最佳答案

在连接代码后添加此行

if($db->connect_errno)
{
    echo "Error: ( " .$db->errorno. " )". $db->error;
    die;
}

只需替换此代码

 if (count($errors) == 0) {
    $password = md5($password_1);//encrypt the password before saving in the database

    $query = "INSERT INTO users (name, email, username, password) 
              VALUES('$name', '$email', '$username', '$password')";
    if( mysqli_query($db, $query))
    {
       $_SESSION['username'] = $username;
       $_SESSION['success'] = "You are now logged in";
       header('location: login.php');
   }
   else
   {
      echo mysqli_error($db);
   }
  }
}

检查是否有错误。

关于php - SQL数据库不存储用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49958107/

相关文章:

php - 用于 API 响应的 Laravel 自定义包装器

c# - 在 SQL 中如何检查一个值是 1 还是 0?

javascript - 展平嵌套的 HTML DOM 节点

html - 如何在没有答案的情况下使用 WebRTC?

javascript - 在 HTML/javascript 中获取设备的唯一 token

mysql - 在mysql select语句中将公共(public)列值作为所有结果的标题

mysql - 在mysql中插入多行

php - 使用 SELECT NOT IN 查找不存在的 ID?

php - 仅删除 PHP 中的空文件夹和子文件夹

php - Zend 2 : How to throw a 404 error from controller