0

How do i limit the user registration in the registration page? i want to limit the user that can register to only 2 users. and if another user want to register, they will have a notice "Registration is Full".

Help me, i dont know what to do now, im looking for the answer for the whole week.

here's the php code of the registration process,

<?php

include('connection.php');

$name=$_POST['name'];
$username=$_POST['username'];
$password=$_POST['password'];
$password2=$_POST['password2'];


$sql=mysqli_query($conn,"INSERT INTO admin (name,username,password,password2) VALUES ('$name','$username','$password','$password2')");

if($sql == TRUE)
{
    echo '<script language="javascript">';
    echo 'alert("Register Successfully");';
    echo 'window.location.href="index_admin.php";';
    echo'</script>';
}

else
{
    echo "Error: ".$sql."<br>".$conn -> error;
}
    $conn -> close();


?>
Martin
  • 22,212
  • 11
  • 70
  • 132
  • Why would you wanna do that? you gonna limit registration until when? – Masivuye Cokile Dec 06 '18 at 12:10
  • 4
    You are _wide open_ to SQL injection attacks. Use parameterized prepared statements instead of injecting unescaped user data into your query like that – M. Eriksson Dec 06 '18 at 12:12
  • 3
    Well then you will have to `COUNT` the current rows in the `admin` table before allowing a new user to register. ___Bit odd that an ADMIN can register themselves though___ – RiggsFolly Dec 06 '18 at 12:13
  • 6
    You should also _never_ store passwords in plain text. You should _only_ store password hashes generated with PHP's built in `password_hash()` and verify the hash with `password_verify()`. I also don't see the point of storing `password2`. Isn't that just to verify that the user have entered the correct password? – M. Eriksson Dec 06 '18 at 12:14
  • until it reach 2 users,. i only want just 2 user that can register – Royale Bliss Dec 06 '18 at 12:14
  • Before adding do a select count users on your table if they equal to two show message else if less register....... this does not make any sense though – Masivuye Cokile Dec 06 '18 at 12:18
  • surely [this one?](https://stackoverflow.com/questions/39758422/mysql-insert-only-if-a-condition-is-true) – Martin Dec 06 '18 at 13:55

2 Answers2

0

Answer to your question: before inserting into database do a select count like

select count(*) from admin;

if returned rows are <=1 , do insert , otherwise do not insert.

you can also use different query and PHP mysqli_num_rows()

this might Help you PHP Manual

Now. advise. you are not checking several thing before inserting

  1. always check data received should not be empty
  2. always check duplicate username
  3. always check escape characters

so please think what else can break your code.

Martin
  • 22,212
  • 11
  • 70
  • 132
Ali
  • 552
  • 6
  • 17
-1

If I understand your question correctly.....

You should do a query to select everything from your table then do a count of the result.

Then wrap your entire query inside an if() condition.

if($num_results < 2){
    // your code here
}else{
    // there are already two entries in your db
}
Peter
  • 29,454
  • 5
  • 48
  • 60
Paddy Hallihan
  • 1,624
  • 3
  • 27
  • 76