简体   繁体   中英

Form submission shows up a blank entry in mysql

Code down here:

<?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>

And 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";
} */
?>

And the script that runs when submitting the form:

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

I might have used to much spaces I am spending 20 minutes trying to post :PI am very new to programming. I hear in stackoverflow people burn you down, well I am ready. This is how I learn.

My problem is that if I add a user to the users table it will turn up blank in the database, there is an entry but no information filled in. Can anyone help me with this?

Just to be sure this is my table:

| 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 |

This is the table output (note I added Elmo myself via terminal:

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       |
+----+----------+---------------------+------+----------+---------------------+------------+

The reason for the problem is you're inserting the values from empty fields into your database:

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

However $username , $email , and $password are never declared in your code until that point. The variables you populated earlier are $rusername , $remail and $rpassword . You need to use these as the inputs to your query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM