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 WHy this does not work?

HI,

This doesnt work. It said it coudnt send the information( of course i wrote that) but it doesnt . I dont see why .

<?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 != $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($con, $email);

            //check for dublicates
            $user_query = mysqli_query($con, "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($con, "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($con, "INSERT INTO members (username,firstname,lastname,email,password,ip_address,sign_up_date)VALUES('$username','$fname','$lname','$email','$pass1',$ip_address',now())") or die("Could not insert 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>

connection:

<?php

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


?>

global.php:

<?php
session_start();
include_once("connect.php");

ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);

?>

2 Answers

Has mysqli not got a function where you can pull out the specific error message?

I would first investigate that so you can narrow down what's wrong!

see this - if you use this function, you should be able to see the actual mysqli error.

or die(mysqli_error);
Konrad Pilch
Konrad Pilch
2,435 Points

It pulls out this Warning: mysqli_query() expects at least 2 parameters, 1 given in /Applications/MAMP/htdocs/071YTMembership/register.php on line 30 Could not check username

It sounds like you're not setting $con properly to me or there is a mysql syntax error?

<?php

$query = "SELECT username FROM members WHERE username='$username'LIMIT 1";

// add a space between $username' and LIMIT.
// add curly braces around variables in strings.

$query =  "SELECT username FROM members WHERE username = '{$username}' LIMIT 1";

What happens if you:

<?php

var_dump($con);
exit;

?>

inside of your post block?

<?php

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

Is it set properly?

Also, to improve the readability of your code, I would suggest this:

<?php

$user_query = mysqli_query($con, "SELECT username FROM members WHERE username='$username'LIMIT 1");

if ( ! $user_query) {
    die("Could not check username");
}

Otherwise you're going to end up with some incredibly long lines!

Konrad Pilch
Konrad Pilch
2,435 Points

It says : Warning: mysqli_query() expects at least 2 parameters, 1 given in /Applications/MAMP/htdocs/071YTMembership/register.php on line 38 Could not chekc username

Thouch thsi is on line 38 now : p and i coudnt work on pc and today , now i have time to check this , sorry for late reply

Konrad Pilch
Konrad Pilch
2,435 Points

It says : Warning: mysqli_query() expects at least 2 parameters, 1 given in /Applications/MAMP/htdocs/071YTMembership/register.php on line 38 Could not chekc username

Thouch thsi is on line 38 now : p and i coudnt work on pc and today , now i have time to check this , sorry for late reply

Konrad Pilch
Konrad Pilch
2,435 Points

It says : Warning: mysqli_query() expects at least 2 parameters, 1 given in /Applications/MAMP/htdocs/071YTMembership/register.php on line 38 Could not chekc username

Thouch thsi is on line 38 now : p and i coudnt work on pc and today , now i have time to check this , sorry for late reply