www.webdeveloper.com
Results 1 to 6 of 6

Thread: Form code help

  1. #1
    Join Date
    Nov 2012
    Posts
    6

    Form code help

    I am trying to put a signup.php section on my website. The script dies somewhere in the php code, and i've been trying to figure it out for hours with no luck.

    The script dies somewhere in here because when i throw this in the file and ftp it to the server and run it, the page comes back blank.
    PHP Code:
     <?php 
    include_once("scripts/connect_to_mysql.php");
    $message ='';

    if(isset(
    $_POST['username'])){

        
    $username $_POST['username'];
        
    $fname $_POST['fname'];
        
    $lname $_POST['lname'];
        
    $email $_POST['email'];
        
    $pass1 $_POST['pass1'];
        
    $pass2 $_POST['pass2'];
        
        
    //error handling
        
    if((!$username)||(!$fname)||(!$lname)||(!$email)||(!$pass1)||(!$pass2)){
            
    $message='Please insert all fields into the form below.';
        }else{
            if(
    $pass1 != $pass2){
                
    $message ='Your password fields do not match.';
            }else{
                
    //securing the values of the variables
                
    $username preg_replace("#[^0-9a-z]#i","",$username);
                
    $fname preg_replace("#[^0-9a-z]#i","",$fname);
                
    $lname preg_replace("#[^0-9a-z]#i","",$lname);
                
    $pass1 sha($pass1);
                
    $email mysql_real_escape_string($email);
                
                
    //check duplicates
                
    $user_query mysql_query("SELECT username FROM members WHERE username='$username' LIMIT 1")or die("Could not check username");
                
    $count_username mysql_num_rows($user_query);
                
                
    $user_query mysql_query("SELECT email FROM members WHERE email='$email' LIMIT 1")or die("Could not check Email");
                
    $count_email mysql_num_rows($email_query);
                
                if(
    $count_username 0){
                    
    $message ='Your username already exists.';
                }else if(
    $count_email 0){
                    
    $message ='Your email already exists.';
                }else{
                    
    // insert users
                    
    $query mysql_query("INSERT INTO members (username, fname, lname, email, password, lastlogin, accounttype, emailactivated, joindate) VALUES('$username', '$fname', '$lname', '$email', '$password', '', '', '', now())")or die("Could not insert data.");
                    
    $member_id mysql_insert_id();
                    
    mkdir("users/$member_id",0755);
                    
    $message ='You have now been registered.';
                }
            }
    ?>
    and here is the form in the body section
    Code:
    <div>
    Register to my site by filling in the fields below.
    	<p><?php print("$message");?></p>
    	<form action="register2.php" method="post">
    	<input type="text" name="username" placeholder="Username"/><br />
            <input type="text" name="fname" placeholder="First Name"/><br />
            <input type="text" name="lname" placeholder="Last Name"/><br />
            <input type="text" name="email" placeholder="Email Address"/><br />
            <input type="password" name="pass1" placeholder="Password"/><br />
            <input type="password" name="pass2" placeholder="Validate your password"/><br />
            <input type="submit" value="Register" />
    
    
    	</form>
    </div>

  2. #2
    Join Date
    Aug 2012
    Posts
    155
    Have you turned on error reporting? If so (or when you do), what error do you get when you post the form?

    Not sure if your regular expressions in your preg_replace may be causing an issue as I am rubbish with them, but I have noticed that your password encryption is wrong, it should be
    PHP Code:
    $pass1 sha1($pass1); 

  3. #3
    Join Date
    Nov 2012
    Posts
    3
    $email_query= mysql_query("SELECT email FROM members WHERE email='$email' LIMIT 1")or die("Could not check Email");
    $count_email = mysql_num_rows($email_query);

    Please change the variable assigned the 2nd mysql query from $user_query to $email_query. I hope this helps.

  4. #4
    Join Date
    Nov 2012
    Posts
    6
    Thanks for the reply! I did fix the two errors you guys mentioned. When I turn errors on I get
    "Parse error: syntax error, unexpected end of file in /home3/stevenp0/public_html/register2.php on line 75"

  5. #5
    Join Date
    Aug 2012
    Posts
    155
    What is on line 75 (is there more code after what you have shown us as there aren't 75 lines in there). If not then you are missing 2 } at the end of your code.

  6. #6
    Join Date
    Nov 2012
    Posts
    6
    Thank you again. I was missing the 2 ) but I think that was from lots of going back and forth, changing things over and over trying to figure out what exactly was wrong in the first place. The way I fixed it was to change the code structure just a bit so that the if/else if statements weren't inside other if/else if statements. I had always assumed that this was possible, or maybe there is a reason it doesn't work in my case, I'm not sure. If any other php noobies are having trouble with similar problems I hope this helps.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles