SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Guru themightystephen's Avatar
    Join Date
    Mar 2005
    Location
    England
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Why is this code not quite working...?

    OK, I have a page which adds users to a page however I found that it still adds a user if the email field has been filled even if the others have not.

    I don't know what to do to sort this problem out.

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Administrator Options - Add User</title>
    <link rel="stylesheet" href="userstyle.css" />
    </head>
    <body>
    <h1>Administrator Options - Add User</h1>
    <?php require('menu.inc'); ?>
    <p>View our <a href="viewusers.php">list of members</a>. Please enter a new user into the database:</p>
    <table border="0">
    <form action="<?= $_SERVER['php_self']?>" method="post">
    <tr><td>Username (no spaces):</td><td><input type="text" name="username" maxlength="20" /></td></tr>
    <tr><td>Password (no more than 8 characters):</td><td><input type="password" name="password" maxlength="8" /></td></tr>
    <tr><td>E-mail:</td><td><input type="text" name="email" maxlength="255" /></td></tr>
    <tr><td colspan="2">
    <input type="submit" value="Submit your Data" /></td></tr>
    </form>
    </table>
    </body>
    </html>
    <?php
    if($_POST['username'] == '' || empty($_POST['username'])) {
        echo 
    '<p class="incorrectdata">You did not enter a username.</p>';  
    }
    if(
    $_POST['password'] == '' || empty($_POST['password'])) {
        echo 
    '<p class="incorrectdata">You did not enter a password.</p>'
    }
    if(
    $_POST['email'] == '' || empty($_POST['email'])) { 
        echo 
    '<p class="incorrectdata">You did not enter an e-mail address.</p>';
        exit;
    }
    if(!
    preg_match('#[a-zA-Z\_\-0-9]*#'$_POST['username'])) {
    echo 
    'Please enter alphanumerics, hyphens and underscores only. No spaces.';
    }

    else { 
    $username=$_POST['username'];   // assign POST values to variables... 
    $password=$_POST['password'];
    $email=$_POST['email'];
    $joindate=$_POST['joindate'];
    }

    require(
    'connectselectdb.inc');

    // query to see if username exists 
    $query "SELECT username FROM users WHERE username = '$username'"
    $result mysql_query($query) or die(mysql_error()); 
    if(
    mysql_num_rows($result) >= 1) {
    echo 
    '<p class="incorrectdata">The username you have chosen already exists! Please choose another one.</p>'
    exit; 
    }

    // else insert data 
    else { 
    $query "INSERT INTO users VALUES('id','$username','$password','$email',NOW())"
    $result mysql_query($query) or die(mysql_error()); 
    if(
    mysql_affected_rows() == 0) { 
    echo 
    '<p>There was an error adding your information.</p>'
    exit; 

    else { 
    echo 
    '<p>Success. A new user has been added.'
    }
    }

    mysql_close($dbcnx);
    ?>
    If you have any other ideas on how to make my code more efficient, then your suggestions are welcome.

    Thanks.
    Get your heelys now at flywalk.co.uk - But what are heelys?
    Heelys are simply shoes with wheels in the heels!

    Flywalk.co.uk - The UK Heelys Retailer

  2. #2
    SitePoint Guru babyboy808's Avatar
    Join Date
    Nov 2004
    Location
    dublin
    Posts
    602
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want to output errors of user's input, throw their errors into an array, then output the whole thing...

  3. #3
    SitePoint Guru themightystephen's Avatar
    Join Date
    Mar 2005
    Location
    England
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry I still a beginner with PHP. How would I go about doing this?
    Get your heelys now at flywalk.co.uk - But what are heelys?
    Heelys are simply shoes with wheels in the heels!

    Flywalk.co.uk - The UK Heelys Retailer

  4. #4
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    california
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would personally have all that check stuff at the top of your script. Also, I would add a check to see if $_POST['submit'] has been set (you'd have to name your submit button to 'submit' or any other variable first), to see if the form has actually been submitted before you make all the checks in the first place. It would look something like:
    Code:
    <?php
    if (isset($_POST['submit']) {
    if($_POST['username'] == '' || empty($_POST['username'])) {
        echo '<p class="incorrectdata">You did not enter a username.</p>';  
    }
    if($_POST['password'] == '' || empty($_POST['password'])) {
        echo '<p class="incorrectdata">You did not enter a password.</p>';
    }
    if($_POST['email'] == '' || empty($_POST['email'])) {
        echo '<p class="incorrectdata">You did not enter an e-mail address.</p>';
        exit;
    }
    if(!preg_match('#[a-zA-Z\_\-0-9]*#', $_POST['username'])) {
    echo 'Please enter alphanumerics, hyphens and underscores only. No spaces.';
    }
    
    else {
    $username=$_POST['username'];   // assign POST values to variables...
    $password=$_POST['password'];
    $email=$_POST['email'];
    $joindate=$_POST['joindate'];
    }
    
    require('connectselectdb.inc');
    
    // query to see if username exists
    $query = "SELECT username FROM users WHERE username = '$username'";
    $result = mysql_query($query) or die(mysql_error());
    if(mysql_num_rows($result) >= 1) {
    echo '<p class="incorrectdata">The username you have chosen already exists! Please choose another one.</p>';
    exit;
    }
    
    // else insert data
    else {
    $query = "INSERT INTO users VALUES('id','$username','$password','$email',NOW())";
    $result = mysql_query($query) or die(mysql_error());
    if(mysql_affected_rows() == 0) {
    echo '<p>There was an error adding your information.</p>';
    exit;
    }
    else {
    echo '<p>Success. A new user has been added.';
    }
    }
    }
    
    mysql_close($dbcnx);
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Administrator Options - Add User</title>
    <link rel="stylesheet" href="userstyle.css" />
    </head>
    <body>
    <h1>Administrator Options - Add User</h1>
    <?php require('menu.inc'); ?>
    <p>View our <a href="viewusers.php">list of members</a>. Please enter a new user into the database:</p>
    <table border="0">
    <form action="<?= $_SERVER['php_self']?>" method="post">
    <tr><td>Username (no spaces):</td><td><input type="text" name="username" maxlength="20" /></td></tr>
    <tr><td>Password (no more than 8 characters):</td><td><input type="password" name="password" maxlength="8" /></td></tr>
    <tr><td>E-mail:</td><td><input type="text" name="email" maxlength="255" /></td></tr>
    <tr><td colspan="2">
    <input type="submit" value="Submit your Data" /></td></tr>
    </form>
    </table>
    </body>
    </html>

  5. #5
    SitePoint Guru themightystephen's Avatar
    Join Date
    Mar 2005
    Location
    England
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The above script doesn't seem to work...I also added the ) you missed on the isset bit. I get this error even after adding the bracket and name...

    Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in c:\program files\apache group\Apache\htdocs\php site\adduser.php on line 48

    Any ideas what's wrong?
    Get your heelys now at flywalk.co.uk - But what are heelys?
    Heelys are simply shoes with wheels in the heels!

    Flywalk.co.uk - The UK Heelys Retailer

  6. #6
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you need to call exit; if you want to stop your script

    PHP Code:
    if($_POST['username'] == '' || empty($_POST['username'])) {
        echo 
    '<p class="incorrectdata">You did not enter a username.</p>';  
        
    // stop the script, no username
        
    exit;
    }
    if(
    $_POST['password'] == '' || empty($_POST['password'])) {
        echo 
    '<p class="incorrectdata">You did not enter a password.</p>';
        
    // stop the script, no password
        
    exit;
    }
    if(
    $_POST['email'] == '' || empty($_POST['email'])) {
        echo 
    '<p class="incorrectdata">You did not enter an e-mail address.</p>';
        exit;
    }
    if(!
    preg_match('#[a-zA-Z\_\-0-9]*#'$_POST['username'])) {
    echo 
    'Please enter alphanumerics, hyphens and underscores only. No spaces.';
        
    // stop the script, bad username
        
    exit;


  7. #7
    SitePoint Guru themightystephen's Avatar
    Join Date
    Mar 2005
    Location
    England
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah that does work...but...I want to show all the fields that the user hasnt entered into. For example if they dont enter any data and press submit then something like this will appear:

    You did not enter a username
    You did not enter a password
    You did not enter an e-mail address

    However when I add the exit; command to each one it will only show the first one that doesnt exist. So if I entered nothing again then I would get this instead which is not what i want.

    You did not enter a username
    Get your heelys now at flywalk.co.uk - But what are heelys?
    Heelys are simply shoes with wheels in the heels!

    Flywalk.co.uk - The UK Heelys Retailer

  8. #8
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chill2k66
    I would personally have all that check stuff at the top of your script. Also, I would add a check to see if $_POST['submit'] has been set (you'd have to name your submit button to 'submit' or any other variable first), to see if the form has actually been submitted before you make all the checks in the first place.
    It would be a better practise if you used a hidden form field, not a submit button value. The submit button value may not be passed on some browsers if a user hits enter key instead of the submit button.

  9. #9
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by themightystephen
    yeah that does work...but...I want to show all the fields that the user hasnt entered into. For example if they dont enter any data and press submit then something like this will appear:

    You did not enter a username
    You did not enter a password
    You did not enter an e-mail address

    However when I add the exit; command to each one it will only show the first one that doesnt exist. So if I entered nothing again then I would get this instead which is not what i want.

    You did not enter a username
    Create a variable before the validation check and assign an initial value of 0. Then whenever the validation fails, increase the value of the variable. In the end, check if this variable equals to 0 and only then perform any queries, that way you'll avoid mysql errors and won't have to use exits.

  10. #10
    SitePoint Guru themightystephen's Avatar
    Join Date
    Mar 2005
    Location
    England
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I seem to be getting this error...I know I haven't missed out any obvious things.

    Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in c:\program files\apache group\Apache\htdocs\php site\adduser2.php on line 67
    Get your heelys now at flywalk.co.uk - But what are heelys?
    Heelys are simply shoes with wheels in the heels!

    Flywalk.co.uk - The UK Heelys Retailer

  11. #11
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by themightystephen
    I seem to be getting this error...I know I haven't missed out any obvious things.

    Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in c:\program files\apache group\Apache\htdocs\php site\adduser2.php on line 67
    Could you post a source code?

  12. #12
    SitePoint Guru themightystephen's Avatar
    Join Date
    Mar 2005
    Location
    England
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I must admit I wasn't certain how to go about what you said but I gave it a go so you may find mistakes anyway.

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Administrator Options - Add User</title>
    <link rel="stylesheet" href="userstyle.css" />
    </head>
    <body>
    <h1>Administrator Options - Add User</h1>
    <?php require('menu.inc'); ?>
    <p>View our <a href="viewusers.php">list of members</a>. Please enter a new user into the database:</p>
    <table border="0">
    <form action="<?= $_SERVER['php_self']?>" method="post">
    <tr><td>Username (no spaces):</td><td><input type="text" name="username" maxlength="20" /></td></tr>
    <tr><td>Password (no more than 8 characters):</td><td><input type="password" name="password" maxlength="8" /></td></tr>
    <tr><td>E-mail:</td><td><input type="text" name="email" maxlength="255" /></td></tr>
    <tr><td colspan="2">
    <input type="submit" value="Submit your Data" /></td></tr>
    </form>
    </table>
    </body>
    </html>
    <?php
    $validationcheck
    =0;

    if(empty(
    $_POST['username'])) {
        echo 
    '<p class="incorrectdata">You did not enter a username.</p>';
        
    $validationcheck=1;
    }
    if(empty(
    $_POST['password'])) {
        echo 
    '<p class="incorrectdata">You did not enter a password.</p>';
        
    $validationcheck=1;
    }
    if(empty(
    $_POST['email'])) { 
        echo 
    '<p class="incorrectdata">You did not enter an e-mail address.</p>';
        
    $validationcheck=1;
    }

    if(
    $validationcheck==0) {
    $username=$_POST['username'];   // assign POST values to variables... 
    $password=$_POST['password'];
    $email=$_POST['email'];
    $joindate=$_POST['joindate'];

    require(
    'connectselectdb.inc');

    // query to see if username exists 
    $query "SELECT username FROM users WHERE username = '$username'"
    $result mysql_query($query) or die(mysql_error()); 
    if(
    mysql_num_rows($result) >= 1) {
    echo 
    '<p class="incorrectdata">The username you have chosen already exists! Please choose another one.</p>'
    exit; 
    }

    // else insert data 
    else { 
    $query "INSERT INTO users VALUES('id','$username','$password','$email',NOW())"
    $result mysql_query($query) or die(mysql_error()); 
    if(
    mysql_affected_rows() == 0) { 
    echo 
    '<p>There was an error adding your information.</p>'
    exit; 

    else { 
    echo 
    '<p>Success. A new user has been added.'
    }
    }
    }
    mysql_close($dbcnx);
    ?>
    Get your heelys now at flywalk.co.uk - But what are heelys?
    Heelys are simply shoes with wheels in the heels!

    Flywalk.co.uk - The UK Heelys Retailer

  13. #13
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The last line of a script is the one to blame. Since you've put require('connectselectdb.inc') under the if($validationcheck==0), you should put mysql_close($dbcnx) there as well. Now it doesn't connect to the database but tries to disconnect, thus the error.

    Here's it fixed:
    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Administrator Options - Add User</title>
    <link rel="stylesheet" href="userstyle.css" />
    </head>
    <body>
    <h1>Administrator Options - Add User</h1>
    <?php require('menu.inc'); ?>
    <p>View our <a href="viewusers.php">list of members</a>. Please enter a new user into the database:</p>
    <table border="0">
    <form action="<?= $_SERVER['php_self']?>" method="post">
    <tr><td>Username (no spaces):</td><td><input type="text" name="username" maxlength="20" /></td></tr>
    <tr><td>Password (no more than 8 characters):</td><td><input type="password" name="password" maxlength="8" /></td></tr>
    <tr><td>E-mail:</td><td><input type="text" name="email" maxlength="255" /></td></tr>
    <tr><td colspan="2">
    <input type="submit" value="Submit your Data" /></td></tr>
    </form>
    </table>
    </body>
    </html>
    <?php
    $validationcheck
    =0;

    if(empty(
    $_POST['username'])) {
        echo 
    '<p class="incorrectdata">You did not enter a username.</p>';
        
    $validationcheck=1;
    }
    if(empty(
    $_POST['password'])) {
        echo 
    '<p class="incorrectdata">You did not enter a password.</p>';
        
    $validationcheck=1;
    }
    if(empty(
    $_POST['email'])) {
        echo 
    '<p class="incorrectdata">You did not enter an e-mail address.</p>';
        
    $validationcheck=1;
    }

    if(
    $validationcheck==0) {
    $username=$_POST['username'];   // assign POST values to variables...
    $password=$_POST['password'];
    $email=$_POST['email'];
    $joindate=$_POST['joindate'];

    require(
    'connectselectdb.inc');

    // query to see if username exists
    $query "SELECT username FROM users WHERE username = '$username'";
    $result mysql_query($query) or die(mysql_error());
    if(
    mysql_num_rows($result) >= 1) {
    echo 
    '<p class="incorrectdata">The username you have chosen already exists! Please choose another one.</p>';
    exit;
    }

    // else insert data
    else {
    $query "INSERT INTO users VALUES('id','$username','$password','$email',NOW())";
    $result mysql_query($query) or die(mysql_error());
    if(
    mysql_affected_rows() == 0) {
    echo 
    '<p>There was an error adding your information.</p>';
    exit;
    }
    else {
    echo 
    '<p>Success. A new user has been added.';
    }
    }
    mysql_close($dbcnx);
    }
    ?>
    Also, I don't think you wanna output the error messages after closing the body and html tags.

  14. #14
    SitePoint Guru themightystephen's Avatar
    Join Date
    Mar 2005
    Location
    England
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you.

    It basically does function correctly however I still want all 3 of the errors appearing when users don't enter data for any of them (or 2 errors if 2 blank fields).

    Like this:
    You did not enter a username
    You did not enter a password
    You did not enter an e-mail address

    I don't know how to get the relevant error messages to appear rather than just 1.
    Get your heelys now at flywalk.co.uk - But what are heelys?
    Heelys are simply shoes with wheels in the heels!

    Flywalk.co.uk - The UK Heelys Retailer

  15. #15
    SitePoint Guru themightystephen's Avatar
    Join Date
    Mar 2005
    Location
    England
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, it does work...false alarm.

    Sorry about that and thank you for all your help.
    Get your heelys now at flywalk.co.uk - But what are heelys?
    Heelys are simply shoes with wheels in the heels!

    Flywalk.co.uk - The UK Heelys Retailer

  16. #16
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by themightystephen
    Actually, it does work...false alarm.

    Sorry about that and thank you for all your help.
    You are very welcome


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •