Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Konrad Pilch
Konrad Pilch
2,435 Points

PHP What is wrong with this ? membership

HI,

My html doesnt show . What is wrong with this code?

<?php include_once("scripts/global.php") ?>
<?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 handeling
    if((!$username) || (!$fname) || (!$lname) || (!$email) || (!$pass1) || (!$pass2)){
        $message = 'Please insert all fields in the form below!';
    }else{
        if($pass1 1= $pass2){
            $message = 'Your password fields do not match!';
        }else{
            //securing the data
            $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 = sha1($pass1);

            $email = mysqli_real_escape_string($email);

            //check for dublicates
            $user_query = mysqli_query("SELECT username FROM members WHERE username='$username'LIMIT 1") or die("Could not chekc username");
            $count_username = mysqli_num_rows($user_query);

            $email_query = mysqli_query("SELECT email FROM members WHERE email='$email'LIMIT 1") or die("Could not chekc username");
            $count_email = mysqli_num_rows($email_query);

            if($count_username > 0){
                $message = 'Your username is allready in use';
            }else if($count_email > 0)(
                $message = 'Your email is allready in use!';
            )else{
                //insert members
                $ip_address = $_SERVER['REMOTE_ADDR'];
                $query = mysqli_query("INSERT INTO members (username,firstname,email,password,ip_address,sign_up_date)VALUES('$username','$fname','$lname','$email','$pass1',$ip_address',now())") or die("Could not isert your information");
                $member_id = mysqli_insert_id();
                mkdir("users/$member_id",0755);
                $message = 'You have now been registered';

            }
        }

    }
}

?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>Membership website</title>
        <link rel="stylesheet" type="text/css" href="css/style.css">
    </head>
    <body>
        <div class="container center">
            <h1>Register to my site b filling the fields below!</h1>
            <p><?php echo("$message"); ?></p>
            <form action="register.php" method="post">
                <input type="text" name="username" placeholder="Username" /><br />
                <input type="text" name="fname" placeholder="Firstname" /><br />
                <input type="text" name="lname" placeholder="Lastname" /><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 Password" /><br />

                <input type="submit" value="Register!" />

            </form>
        </div><!--/ container -->


    </body>
</html>

It shows blank but i should be able to register now : p

Konrad Pilch
Konrad Pilch
2,435 Points

and my global is

<?php
session_start();
include_once("connect.php");
?>
Konrad Pilch
Konrad Pilch
2,435 Points

and connect it

<?php

mysqli_connect("localhost","root","root","membership") or die("Could not connect to server!");


?>

2 Answers

Konrad Pilch
Konrad Pilch
2,435 Points

Woah! I found my mistake!

If you look at the curly brackets i had this () but it shoudl be {} oh wow it took me one and half hour to figure it our and i even re-wrote it twice! Thank you for your help .

You have a typo.

if($pass1 1= $pass2) // $pass1 == $pass2
Konrad Pilch
Konrad Pilch
2,435 Points

It still doesnt work : p

Enable your error reporting, We need to see what kind of error was thrown.

Konrad Pilch
Konrad Pilch
2,435 Points

How do i do that? it doesnt show any errors , like blank. Or is there other way to write an error report?

In your global.php file add these lines:

ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
Konrad Pilch
Konrad Pilch
2,435 Points

No effects. Its blank. Its just weird , theres something wrong with it . its in the folder scripts i have no idea .

What is your OS?

Konrad Pilch
Konrad Pilch
2,435 Points

Meaning iMac? i dont know : p i just know i bough it ok November the newest and with pretty good stats.

Konrad Pilch
Konrad Pilch
2,435 Points

When this piece of code disapears, it works : p the layout shows

else{
        if($pass1 != $pass2){
            $message = 'Your password fields do not match!';
        }else{
            //securing the data
            $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 = sha1($pass1);

            $email = mysqli_real_escape_string($email);

            //check for dublicates
            $user_query = mysqli_query("SELECT username FROM members WHERE username='$username'LIMIT 1") or die("Could not chekc username");
            $count_username = mysqli_num_rows($user_query);

            $email_query = mysqli_query("SELECT email FROM members WHERE email='$email'LIMIT 1") or die("Could not chekc username");
            $count_email = mysqli_num_rows($email_query);

            if($count_username > 0){
                $message = 'Your username is allready in use';
            }else if($count_email > 0)(
                $message = 'Your email is allready in use!';
            )else{
                //insert members
                $ip_address = $_SERVER['REMOTE_ADDR'];
                $query = mysqli_query("INSERT INTO members (username,firstname,email,password,ip_address,sign_up_date)VALUES('$username','$fname','$lname','$email','$pass1',$ip_address',now())") or die("Could not isert your information");
                $member_id = mysqli_insert_id();
                mkdir("users/$member_id",0755);
                $message = 'You have now been registered';

            }
        }

    }

Basically it breaks to this code.