-1

I've just started to learn PHP - Not very good. I have created a form (see below) which I would like people to sign up to but I would like to limit this to a certain number of members. For example 4 users can sign up when it reaches that amount it will hide the form and display a message saying "sorry, you have reached the user account limit."

<form action="users.php?new=1" method="post">
    <input type="hidden" name="action" value="newUser"/>
    <input type="hidden" name="id" value="0"/>
<fieldset>
    <legend>Add new user</legend>

    <?php if(isset($msg)) { echo $msg;}?>

    <p>
        <label>Full name: </label>
        <input name="name"  value="<?php if(isset($name)) { echo $name;}?>" style="width:400px;"/>
    </p>

     <p>
        <label>Email: </label>
        <input name="email"  value="<?php if(isset($email)) { echo $email;}?>" style="width:400px;"/>
    </p>

     <p>
        <label>Password: </label>
        <input name="password" type="password"  value="" style="width:400px;"/>
    </p>
     <p>
        <label>Phone number: </label>
        <input name="phone"  value="<?php if(isset($phone)) { echo $phone;}?>" style="width:400px;"/>
    </p>

                    <p>

<label>User level:</label>
        <select name="access" class="dropdown">
            <option value="no" <?php if(isset($access)) { if ($access =='no'){ echo ' selected="selected" ';}}?>>User</option>
            <option value="yes" <?php if(isset($access)) { if ($access =='yes'){ echo ' selected="selected" ';}}?>>Admin</option>
         </select>

Note: Admin users will have full access to all areas.

Please can someone help me! Code would be lovely!

Thanks, Charlie

Marc B
  • 340,537
  • 37
  • 382
  • 468

2 Answers2

0
<?php        
    $connection=mysqli_connect("host", "username", "password", "database");
    $sql="SELECT COUNT(*) from database_users";
    $stmt = $connection->prepare($sql);
    $stmt->execute();
    $res = $stmt->get_result();
    $users = $res->fetch_array(MYSQLI_ASSOC);

    if ($users['COUNT(*)'] < 4) {
?>

    // html goes here, outside of the php tags

<?php
    } else {

        echo "Sorry, you have reached the user account limit.";

    };
?>
d.abyss
  • 164
  • 1
  • 4
  • 24
  • `myslqi_connect` ok. OP's going to get a rude awakening. – Funk Forty Niner Feb 24 '14 at 15:06
  • $users = $res->fetch_array(MYSQLI_ASSOC)) - It doesn't like this line of code? – CharlieW95 Feb 24 '14 at 15:19
  • @CharlieW95 The second closing bracket could be the culprit! Get rid of that ")" I've updated the code by removing the extra bracket and adding a semi colon – d.abyss Feb 24 '14 at 15:22
  • @danielsmile Thank you! It fixed that problem, When I insert the form into the // Insert your form code it thinks the first part of the form is PHP and is coming up with errors? – CharlieW95 Feb 24 '14 at 15:26
  • @CharlieW95 I'll update the code to be more html friendly. You need to seperate the PHP and HTML code by closing the PHP tags. Don't worry, this doesn't stop the PHP code running:) – d.abyss Feb 24 '14 at 15:29
  • @danielsmile thank you so much for your help! All sorted thank you so much! – CharlieW95 Feb 24 '14 at 15:35
  • @CharlieW95 No problem pal, hopefully you understand my code. Feel free to mark my answer as the accepted answer if you felt it solved your problem:) – d.abyss Feb 24 '14 at 15:36
  • @danielsmile I was actually looking, if there was a way to credit you on here, thing is I have only just signed up can you explain to me how to do this for you (sorry real bad noob) :| haha – CharlieW95 Feb 24 '14 at 15:39
  • @CharlieW95 At the top left of my answer, underneath the rating (the zero with arrows above and below it) there will be a grey tick. Clicking that turns the Tick green and marks it as the accepted answer. – d.abyss Feb 24 '14 at 15:41
  • @danielsmile Think i found it with the tick! Thank you once again mate such a good helper! Top man – CharlieW95 Feb 24 '14 at 15:41
  • @danielsmile I have just got to my iMac at home and I have this error.. Fatal error: Call to undefined method mysqli_stmt::get_result() in /home/stmdevco/public_html/... on line 119 Any ideas? – CharlieW95 Feb 24 '14 at 22:27
  • @CharlieW95 http://stackoverflow.com/a/8343970/3169285 perhaps you might be experiencing this issue – d.abyss Feb 25 '14 at 09:03
0

Using whatever database method you're using, you should take a look into the database and see how many results are returned. If there are 4 or more, then don't display the form, if not, then display it.

Seeing as you're new to PHP, the chances of your database language will be mysql_*, which is not very good, but unfortunately it's widespread across the internet.

Please look into PDO or mysqli_* if you haven't already.

If you're using mysqli:

<?php
    $con=mysqli_connect("host", "username", "password", "database");

    if (mysqli_connect_errno($con))
    {
        echo "Failed to connect: " . mysqli_connect_error();
    }

    $sql= "SELECT * FROM table";
    if ($result=mysqli_query($con,$sql){
        $rowcount = mysqli_num_rows($result);
    }

Then you can use $rowcount in an if to see if it is more than 4.

eg.

if ($rowcount > 4){
    echo "Sorry, no more users can sign up";
}else{
    //form here
}
Albzi
  • 14,793
  • 5
  • 39
  • 59