Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Mar 2007
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Register problems

    PHP Code:
    <?php
        ini_set 
    ("display_errors""1");
        
    error_reporting(E_ALL);
         
        function 
    register() {
            include 
    "connect.php";
            
    $con mysql_connect($host$uname$pass) or die(mysql_error());
            
    mysql_select_db($dbname$con) or die(mysql_error());
            
    $username trim($_POST['username']);
            
    $password trim($_POST['password']);
            
    $pass_conf trim($_POST['pass_conf']);
            
    $email trim($_POST['email']);
            
    //Check the Data
            
    if (empty($username)) {
                die (
    "Fill in a username!");
            }
            if (empty(
    $password)) {
                die (
    "You forgot to fill in a password!");
            }
            if (empty(
    $pass_conf)) {
                die (
    "Confirm your password");
            }
            if (empty(
    $email)) {
                die (
    "We can't register you without an email");
            }
            
    $patt "^[A-Za-z1-9 _-]+$";
            if (!
    ereg($patt$username)) {
                die(
    "Invalid characters in your username");
            }
            if (!
    ereg($patt$password)) {
                die(
    "Invalid characters in your password");
            }
            if (!
    ereg("^.+@.+$"$email)) {
                die (
    "Invalid email format.");
            }
            if (
    $password != $pass_conf) {
                die (
    "The two passwords do not match!");
            }
            
    //Check to see if username is in use already.
            
    $sql mysql_query("SELECT username from users WHERE username = '".$username."'") or die(mysql_error());
            
    $num_rows mysql_num_rows($sql);
            if (
    $num_rows 0) {
                die (
    "Username already exists!");
            }
            
    //Check to see if email is in use or not.
            
    $sql2 mysql_query("SELECT email from users WHERE email = '".$email."'") or die(mysql_error());
            
    $num_rows2 mysql_num_rows($sql2) or die(mysql_error());
            if (
    $num_rows2 0) {
                die (
    "E-mail is in use!");
            } else {
                
    $new_pass md5($password);
                
    $query "INsERT INTO users (username, password, email)
                    values ('"
    .$username."', '".$new_pass."', '".$email."')";
                
    $mysql mysql_query($query) or die (mysql_error());
                if (
    $mysql) {
                    die(
    "Works");
                } elseif(!
    $mysql) {
                    die (
    mysql_error());
                }
                
    $_SESSION['status'] = "Loggedin";
                
    $_SESSION['username'] = $username;
                
    $message "Welcome, ".$username."You are now logged in. <a href='?act=login'>Log in</a>";
                echo 
    $message;
            }
        }
        function 
    register_form() {
            echo 
    "<center><img src='My Pictures/Waffle Banner.jpg'>
                <form action='?act=register' method='POST'>
                <b>Please register below</b>
                <table align='center' border='0'>
                <tr><td>Username</td><td><input type='text' name='username'>
                </td>
                </tr>
                <tr><td>
                Password:</td><td><input type='password' name='password'>
                </td>
                </tr>
                <tr><td>Confirm your password:</td>
                <td><input type='password' name='pass_conf'></td>
                </tr>
                <tr>
                <td>
                E-mail:
                </td>
                <td>
                <input type='text' name='email'>
                </td>
                </tr>
                <tr><td></td><td>
                <input type='submit' value='Register'></td></tr>
                </table>
                </form>
                </center>"
    ;
        }
        function 
    login() {
            
    header("Location: index.php");
        }
         
        switch(
    $_GET['act']) {
            case 
    "register":
            
    register();
            break;
            case 
    "login":
            
    login();
            break;
            default:
            
    register_form();
        }
    ?>
    I know that the switch is working, but after it goes through the motions of checking the information, nothing happens. Can anyone help me?
    Last edited by Garath531; 04-01-2007 at 03:28 AM. Reason: Reformatted the code with indentations

  • #2
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Your script stops working if any of the fields. That seems a bit overkill. (Regarding the die() )

  • #3
    New Coder
    Join Date
    Mar 2007
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post

    Quote Originally Posted by aedrin View Post
    Your script stops working if any of the fields. That seems a bit overkill. (Regarding the die() )
    I don't know how else to do it. Is that what is wrong with my code?

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    1. I can't see where your login() function is ever called, apart from within the login() function itself, so that switch statement is never executed.

    2. In your switch statement you are comparing $act to named constants (which are probably undefined); what you probably meant to do is compare $act to strings. (i.e. "register", not register)

  • #5
    New Coder
    Join Date
    Mar 2007
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The login function is called when you press "Login" (<a href='?act=login'>Login</a>) I tried your suggestion, but it didn't work.

  • #6
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    The login function is called when you press "Login" (<a href='?act=login'>Login</a>)
    I hear what you are saying, and I know that you want it to work this way, but I'm not seeing the code that actually makes this happen.

    Your login() function does not get called anywhere (that I can see), and that is the only place in your code that the $_GET array is looked at.

  • #7
    New Coder
    Join Date
    Mar 2007
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Right above the switch is the login() function.

  • #8
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    You're thinking that a function is automatically called. Look at this.

    PHP Code:
    function login() {
        echo 
    "Logging in.";
    }
    echo 
    "I want to log in.";
    login(); 
    The output will be:

    Code:
    I want to log in.
    Logging in.
    Also, please take a minute to learn the proper formatting rules.

    Reading code that is not indented is twice as hard.

    I think you need to read a few more tutorials, or perhaps a book before tackling any bigger functionality.

  • #9
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Oooh, I see now, the switch is actually not inside the login() function. Aedrin's right-- you should format your code to be more readable and (imo) put the code that is not in a function at the top. Unless you are a big Pascal fan

    So now the thing to do is what I like to call the Ghetto Debug Technique-- stick a bunch of echos in the code so you can track down what's going wrong. I can't visually pick out the culprit.

  • #10
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,049
    Thanks
    2
    Thanked 317 Times in 309 Posts
    If case you have not done what Fumigator suggested in his first post in this thread, change the following two lines -
    PHP Code:
    case register
    ...
    case 
    login
    to this -
    PHP Code:
    case "register"
    ...
    case 
    "login"
    And if you would place the following two lines after your first opening <?php tag, PHP would have alerted you to this and perhaps other problems -
    PHP Code:
    ini_set ("display_errors""1");
    error_reporting(E_ALL); 
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #11
    New Coder
    Join Date
    Mar 2007
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I've isolated the problem.
    Right after I check for username and email in use, it stops to work.
    PHP Code:
    $sql=mysql_query("SELECT username from users WHERE username =
    '$username'"
    ) or die(mysql_error());
    $num_rows mysql_num_rows($sql);
    if(
    $num_rows 0) {
    die (
    "Username already exists!");
    }
    //Check to see if email is in use or not.
    $sql2=mysql_query("SELECT email from users WHERE email = '$email'") or                
    die(
    mysql_error());
    $num_rows2 mysql_num_rows($sql2) or die(mysql_error());
    if(
    $num_rows2 0) {
    die (
    "E-mail is in use!");

    Does anyone know why?


  •  

    Posting Permissions

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