表单提交在mysql中显示空白条目

时间:2018-07-06 14:00:22

标签: php mysql mysqli

在此处写下代码:

<?php require_once('verbinding.php') ?>
<html>
<head>
    <title>Blog Arena 2.0</title>
    <link rel="stylesheet" type="text/css" href="css/mooi.css">
</head>
<body>
      <!--- navigatie --->
      <?php include('./includes/navigatie.php') ?>
      <!--- header --->
      <?php include('./includes/header.php') ?>
        <h5> Registeer je nu ! Register now ! I cant choose English or Dutch ! </h5>
        <form action='registersubmit.php' method='post' enctype="multipart/form-data">
              <div class="test">
                  <label>Username</label> <input type='text' name='rusername' value=''><br>
              </div>
              <div class="test">
                  <label>E-mail</label> <input type='text' name='remail' value=''><br>
              </div>
                  <div class="test">
                    <label>Password</label> <input type='password' name='rpassword' value=''><br>
              </div>
              <div class="test">
                  <label>Confirm password</label>
                  <input type="password" name='rpassword_2'>
              </div>
              <div class="test">
                <input type='submit' name='save' class='knop'><br>
              </div>
        </form>
  </body>
</html>

和verbinding.php

<?php
session_start();
//verbinding met de database
$conn = mysqli_connect("localhost", "elly", "elly", "blog");

if (!$conn) {
    die("Er zijn problemen met het verbinden aan de database: " . mysqli_connect_error());
}

//als ik het goed begrijp is Base Url  verwijzing voor verschillende OS-es
//define    ('ROOT_PATH', realpath(dirname(__FILE__)));
define  ('BASE_URL', 'http://localhost/');

/* $query = "SELECT * FROM posts";
$result = $conn->query($query);

if($result ->num_rows > 0){
    echo "We have data";
} else {
    echo "we dont have data";
} */
?>

以及提交表单时运行的脚本:

<?php
session_start();

// initializing variables
$rusername = "";
$remail    = "";

var_dump($_POST);

// connect to the database
$con = mysqli_connect('localhost', 'elly', 'elly', 'blog');

// REGISTER USER
if (isset($_POST['save'])) {
    // receive all input values from the form
    $rusername = mysqli_real_escape_string($con, $_POST['rusername']);
    $remail = mysqli_real_escape_string($con, $_POST['remail']);
    $rpassword_1 = mysqli_real_escape_string($con, $_POST['rpassword_1']);
    $rpassword_2 = mysqli_real_escape_string($con, $_POST['rpassword_2']);

    // form validation: ensure that the form is correctly filled ...
    // by adding (array_push()) corresponding error unto $errors array
    if (empty($rusername)) { echo "Username is required"; }
    if (empty($remail)) { echo "Email is required"; }
    if (empty($rpassword_1)) { echo "Password is required"; }
    if ($rpassword_1 != $rpassword_2) {
        echo "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='$rusername' OR email='$remail' LIMIT 1";
    $result = mysqli_query($db, $user_check_query);
    $user = mysqli_fetch_assoc($result);

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

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

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

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

我可能已经习惯了很多时间,我花20分钟试图发布:P我对编程非常陌生。我听说在stackoverflow中人们把您烧死了,我已经准备好了。这就是我的学习方式。

我的问题是,如果将用户添加到用户表中,它将在数据库中变成空白,没有条目,但没有填写信息。有人可以帮助我吗?

只需确保这是我的桌子:

| users | CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `role` enum('Author','Admin') DEFAULT NULL,
  `password` varchar(255) NOT NULL,
  `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1 |

这是表的输出(请注意,我自己是通过终端添加Elmo的:

mysql> select * from users;
+----+----------+---------------------+------+----------+---------------------+------------+
| id | username | email               | role | password | created_at          | updated_at |
+----+----------+---------------------+------+----------+---------------------+------------+
|  1 | elmo     | elmo@sesamstraat.nl | NULL | elmo     | 2018-07-05 10:23:13 | NULL       |
|  2 |          |                     | NULL |          | 2018-07-06 14:47:04 | NULL       |
|  3 |          |                     | NULL |          | 2018-07-06 14:55:44 | NULL       |
|  4 |          |                     | NULL |          | 2018-07-06 15:27:38 | NULL       |
|  5 |          |                     | NULL |          | 2018-07-06 16:06:01 | NULL       |
|  6 |          |                     | NULL |          | 2018-07-06 16:07:55 | NULL       |
|  7 |          |                     | NULL |          | 2018-07-06 16:17:10 | NULL       |
+----+----------+---------------------+------+----------+---------------------+------------+

1 个答案:

答案 0 :(得分:0)

问题的原因是您将空字段中的值插入数据库中:

"INSERT INTO users (username, email, password)
VALUES('$username', '$email', '$password')";

但是直到那时,$username$email$password都不会在您的代码中声明。您之前填充的变量是$rusername$remail$rpassword。您需要将这些用作查询的输入。