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 error

What does this error mean?

Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in /Applications/MAMP/htdocs/071YTMembership/register.php on line 25

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

Konrad Pilch
Konrad Pilch
2,435 Points

And this is my code if thats any use

register

<?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{ //code breaks 
            //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';

            }
        }//code breaks

    }
}

?>
<!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>
Konrad Pilch
Konrad Pilch
2,435 Points

This is my connect :

<?php

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


?>

AND THIS IS MY GLOBAL:

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

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

?>

So i believe it should work

1 Answer

expects exactly 2 parameters . you need to connect to the db also. you set the string but sql can not connect to the db. $example = mysqli_real_escape_string($connection, $string); notice $A this is the variable for connecting to the my sql database. it looks like the same error for both they need two arguments A connection to the data base and B the string

Konrad Pilch
Konrad Pilch
2,435 Points

Oh here i see .

I changed this to

$email = mysqli_real_escape_string($con,$email);

and not it works

Konrad Pilch
Konrad Pilch
2,435 Points

But how can i connect this ?

            $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);

The $con is the database

<?php

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


?>
Konrad Pilch
Konrad Pilch
2,435 Points

Oh i added the con

        $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);

But it could not send of the informaiton

Konrad Pilch
Konrad Pilch
2,435 Points

Actually i made a clean post : p here