0

Okay so my problem is that I am trying to restrict people from registering with the same email or username, but i have no idea how to do it. Someone help me please D:

<?php
require 'database/connection.php';
?>

<?php
if(isset($_POST['submit'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $name = $_POST['name'];
    $email = $_POST['email'];

    $sql = $con->query("INSERT INTO `system` (`user_id`, `username`, `password`, `email`, `name`) VALUES (NULL, '{$username}', '{$password}', '{$email}', '{$name}')");
header('Location: login.php');
}
?>

<html>
    <body>
        <div class="registerform">
            <form method="post">
                <input name="username" type="username" placeholder="Username" required="required">
                <input name="password" type="password" placeholder="Password" required="required">
                <input name="name" type="name" placeholder="Full name" required="required">
                <input name="email" type="email" placeholder="Email" required="required">
                <input type="submit" name="submit" value="Register">
                </form>
        </div>
    </body>
</html>
Drew
  • 24,851
  • 10
  • 43
  • 78
  • Do you have a database setup yet? If so, the only thing you have to do is use SELECT to check if the username/email is taken before inserting. – Crecket Nov 27 '15 at 19:23
  • 2
    You could just apply a unique column on your sql and handle the error in your php application. – Liam Sorsby Nov 27 '15 at 19:23
  • 2
    Oh and please don't use queries like that, its extremely easy for a user to use sql injection here. Use prepared statements instead – Crecket Nov 27 '15 at 19:24
  • One problem, im new to php and i dont know how to use select? can u give me an example please? And yes, i have databse setup – Richard Olsen Sandberg Nov 27 '15 at 19:24
  • @RichardOlsenSandberg. learn sql queries soon. else you cant grow up in your career. – Vigneswaran S Nov 27 '15 at 19:29
  • See this answer http://stackoverflow.com/q/17381779/2266087 – EnduroDave Nov 27 '15 at 19:32
  • 1
    you are storing your password as clear text. Here is my [Exciting New Answer](http://stackoverflow.com/a/33962601) on how to fix that with one-way hashes. – Drew Nov 27 '15 at 20:00

1 Answers1

1

This would be the query

  $sql = $con->query("SELECT * FROM users WHERE email=$email OR password=$password");

Though this is totally not safe, but it answers your question :/

You really should use prepared statements, something like

    // Set and execute database query
    $sql = "SELECT * FROM users WHERE email = :email OR password = :password"; 
    $query = $database->prepare($sql);
    $query->execute(array(':password'  => $password,
                          ':email' => $email));


    if ($query->rowCount() >= 1) {
        // User Exists
    }else{
        // User Does Not Exists
    }

EDIT

Here is a helpful answer on dealing with passwords thanks to @Drew stackoverflow.com/q/17381779/2266087

Community
  • 1
  • 1
VIDesignz
  • 4,703
  • 3
  • 25
  • 37
  • 1
    Nice answer VI. Point out the clear text password issue, which I did under the comments of Question – Drew Nov 27 '15 at 19:56
  • @Drew I don't want to give him a full lesson right now...haha. My time is too precious. – VIDesignz Nov 27 '15 at 20:04
  • That is why I write up a good answer or two imo and link :) But ya gotta at least mention it – Drew Nov 27 '15 at 20:06