SQL数据库不存储用户输入

时间:2018-04-21 16:56:25

标签: php html mysql database

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

当用户输入此数据时,应该显示登录页面,并在后台将数据存储在数据库中。但是当我打开phpmyadmin检查表时,没有保存数据。 / p>

下面是我用来将用户输入发送到数据库的代码,这是我的'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");
    }
  }
}

?>

如果需要进一步的代码来帮助我解决这个问题,请告诉我。谢谢!

3 个答案:

答案 0 :(得分:1)

连接代码后添加此行

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);
   }
  }
}

检查是否有任何错误。

答案 1 :(得分:0)

在try catch块中尝试你的代码。用这个替换你的注册部分:

    // Finally, register user if there are no errors in the form
    try {
      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');
      }
   } catch (Exception $e) {
        echo 'Caught exception: ',  $e->getMessage(), "\n";
   }

如果有任何中断,这会给你错误,否则你的代码似乎没问题。

答案 2 :(得分:0)

第1步:更改代码,如下所示:

        <?php
        session_start();

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

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

        if (!$db) {
            die("Connection failed: " . mysqli_connect_error());
        }

        // 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')";
                if( mysqli_query($db, $query))
                {
                   $_SESSION['username'] = $username;
                   $_SESSION['success'] = "You are now logged in";
                   header('location: login.php');
               }
               else
               {
                  echo mysqli_error($db);
               }
          }
        }

        // ... 
        // ... 

        // 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");
            }
          }
        }

        ?>

假设mysql没有密码。

第2步: 重新检查mysql连接和表名称大小写和拼写

第3步: 检查表中的可空字段,字段类型和长度。如果您有主键检查是否存在自动增量。希望这会有所帮助。