Problem with registration system

Discussion in 'PHP' started by sarac93, Apr 21, 2018.

Thread Status:
Not open for further replies.
  1. #1
    When I try to sign in to the registration system I fill all the fields and I get this error: "User could not be added to the database!"
    I don't know what I did wrong. I created a database named "users" on phpmyadmin and I created a table with the same name where I set up the id, the username, the password and the avatar.
    Then I wrote this php script:

    <?php
    session_start();
    $_SESSION['message'] = '';
    $mysqli = new mysqli("localhost", "root", "mypass123", "users");
    //the form has been submitted with post
    if ($_SERVER["REQUEST_METHOD"] == "POST") {

    //two passwords are equal to each other
    if ($_POST['password'] == $_POST['confirmpassword']) {

    //set all the post variables
    $username = $mysqli->real_escape_string($_POST['username']);
    $email = $mysqli->real_escape_string($_POST['email']);
    $password = md5($_POST['password']); //md5 has password for security
    $avatar_path = $mysqli->real_escape_string('images/'.$_FILES['avatar']['name']);

    //make sure the file type is image
    if (preg_match("!image!",$_FILES['avatar']['type'])) {

    //copy image to images/ folder
    if (copy($_FILES['avatar']['tmp_name'], $avatar_path)){

    //set session variables
    $_SESSION['username'] = $username;
    $_SESSION['avatar'] = $avatar_path;
    //insert user data into database
    $sql = "INSERT INTO users (username, email, password, avatar) "
    . "VALUES ('$username', '$email', '$password', '$avatar_path')";

    //if the query is successsful, redirect to welcome.php page, done!
    if ($mysqli->query($sql) === true){
    $_SESSION['message'] = "Registration succesful! Added $username to the database!";
    header("location: welcome.php");
    }
    else {
    $_SESSION['message'] = 'User could not be added to the database!';
    }
    $mysqli->close();
    }
    else {
    $_SESSION['message'] = 'File upload failed!';
    }
    }
    else {
    $_SESSION['message'] = 'Please only upload GIF, JPG or PNG images!';
    }
    }
    else {
    $_SESSION['message'] = 'Two passwords do not match!';
    }
    }
    ?>
    <link href="//db.onlinewebfonts.com/c/a4e256ed67403c6ad5d43937ed48a77b?family=Core+Sans+N+W01+35+Light" rel="stylesheet" type="text/css"/>
    <link rel="stylesheet" href="form.css" type="text/css">
    <div class="body-content">
    <div class="module">
    <h1>Create an account</h1>
    <form class="form" action="form.php" method="post" enctype="multipart/form-data" autocomplete="off">
    <div class="alert alert-error"><?= $_SESSION['message'] ?></div>
    <input type="text" placeholder="User Name" name="username" required />
    <input type="email" placeholder="Email" name="email" required />
    <input type="password" placeholder="Password" name="password" autocomplete="new-password" required />
    <input type="password" placeholder="Confirm Password" name="confirmpassword" autocomplete="new-password" required />
    <div class="avatar"><label>Select your avatar: </label><input type="file" name="avatar" accept="image/*" required /></div>
    <input type="submit" value="Register" name="register" class="btn btn-block btn-primary" />
    </form>
    </div>
    </div>



    What did I do wrong?
     

    Attached Files:

    sarac93, Apr 21, 2018 IP
  2. sarac93

    sarac93 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #2
    Nevermind, I'm tired woo! I didn't create the email field on phpmyadmin and that's why I got the error. Everything works fine now! Thanks anyway.
     
    sarac93, Apr 21, 2018 IP
Thread Status:
Not open for further replies.