Вопрос

I have a syntax error in my php script and cannot figure out why this is failing. Here is my code:

PHP

<?php

if ($_SERVER [ 'REQUEST_METHOD' ] == 'POST' )

    {
        require ('connect_db.php');
        $errors = array();



if (empty($_POST[ 'first_name']))   

    {$errors[] = 'Enter your first name.';}

else

    {$fn = mysqli_real_escape_string($dbc,
                            trim($_POST[ 'first_name']));}

if (empty($_POST[ 'last_name']))

    { $errors[] = 'Enter your last name' ; }

else

    {$fn = mysqli_real_escape_string($dbc,
                            trim($_POST[ 'last_name']));}

if (empty($_POST[ 'email']))

    { $errors[] = 'Enter your email address' ; }

else

    {$fn = mysqli_real_escape_string($dbc,
                            trim($_POST[ 'email']));}   


    }

if ( !empty($_POST[ 'pass1'])) 

    {

    if ( $_POST[ 'pass1' ] != $_POST [ 'pass2' ] )
    { $errors[] = 'Passwords do not match.' ; }
    else
    { $p = mysqli_real_escape_string($dbc,
                            trim($_POST[ 'pass1']));}
    }
    else { $errors[] = 'Enter your password.' ;}

if ( empty( $errors ))

    {
        $q = "SELECT user_id FROM users WHERE email='$e'" ;
        $r = mysqli_query ($dbc , $q );
        if (mysqli_num_rows( $r ) ! = 0 )
        { $errors[] = 'Email address already registered.
                        <a href="login.php">Login</a>'; }
    }

if (empty( $errors ))

    {

        $q = "INSERT INTO users
            (first_name, last_name, email, pass, reg_date)
            VALUES ('$fn', $In', '$e', SHA1('$p'), NOW() )" ;

        $r = mysqli_query ( $dbc, $q );

if ( $r )

    {
        echo '<h1>Registered!</h1>
              <p>You are now registered</p>
              <p><a href="login.php">Login</a></p>';

    }

    mysqli_close( $dbc );
    exit();

    }

else

    {
        echo '<h1>Error!</h1>
              <p id="error_msg">The following error(s) occurred<br>' ;
              foreach ( $errors as $msg )

    {
        echo " - $msg<br>" ;

    }

        echo 'Please try again.</p>' ;
        mysqli_close( $dbc );
    }

?>

Here is my HTML form

<h1>Register</h1>

<form action="register.php" method="post">
<p>
First name: <input type="text" name="first_name"
value="<?php if ( isset( $_POST[ 'first_name' ] ))
        echo $_POST[ 'first_name' ] ; ?>" />

 Last name: <input type="text" name="last_name"
value="<?php if ( isset( $_POST[ 'first_name' ] ))
        echo $_POST[ 'last_name' ] ; ?>" />

</p><p>

Email address: <input type="text" name="email"
value="<?php if ( isset( $_POST[ 'email' ] ))
        echo $_POST[ 'email' ] ; ?>" />

</p><p>

Password: <input type="password" name="pass1"
value="<?php if ( isset( $_POST[ 'pass1' ] ))
        echo $_POST[ 'pass1' ] ; ?>" />    

Confirm password: <input type="password" name="pass2"
value="<?php if ( isset( $_POST[ 'pass2' ] ))
        echo $_POST[ 'pass2' ] ; ?>" />      

</p><p>

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

</form>

The failing line is:

if ( empty( $errors ))

    {
        $q = "SELECT user_id FROM users WHERE email='$e'" ;
        $r = mysqli_query ($dbc , $q );
        if (mysqli_num_rows( $r ) ! = 0 )
        { $errors[] = 'Email address already registered.


            <a href="login.php">Login</a>'; }
}

If I take this line out the form displays but obviously will nt update my database without it. Are there any suggestions on what is happening here?

Это было полезно?

Решение

Remove the space between ! and =

It should be

if (mysqli_num_rows( $r ) != 0 )

Другие советы

$e isn't defined anywhere that I can see. Try making sure that you're setting $e equal to something.

If that doesn't fix it, please post what exact error message you're getting.

Also, please use a consistent bracket/indenting style. It took awhile to figure out because the brackets weren't lining up, which is a real important thing especially in PHP. I've fixed your indenting here:

<?php

if ($_SERVER [ 'REQUEST_METHOD' ] == 'POST' ){
    require ('connect_db.php');
    $errors = array();

    if (empty($_POST[ 'first_name'])){
        $errors[] = 'Enter your first name.';
    }
    else {
        $fn = mysqli_real_escape_string($dbc,
        trim($_POST[ 'first_name']));
    }

    if (empty($_POST[ 'last_name'])){ 
        $errors[] = 'Enter your last name' ; 
    }
    else {
        $fn = mysqli_real_escape_string($dbc,
        trim($_POST[ 'last_name']));
    }

    if (empty($_POST[ 'email'])){ 
        $errors[] = 'Enter your email address' ; 
    }
    else {
        $fn = mysqli_real_escape_string($dbc,
        trim($_POST[ 'email']));
    }   

}

if ( !empty($_POST[ 'pass1'])) {

    if ( $_POST[ 'pass1' ] != $_POST [ 'pass2' ] ){ 
        $errors[] = 'Passwords do not match.' ; 
    }
    else { 
        $p = mysqli_real_escape_string($dbc,
        trim($_POST[ 'pass1']));
    }
}
else { 
    $errors[] = 'Enter your password.' ;
}

if ( empty( $errors )){
    $q = "SELECT user_id FROM users WHERE email='$e'" ;
    $r = mysqli_query ($dbc , $q );
    if (mysqli_num_rows( $r ) ! = 0 ){ 
        $errors[] = 'Email address already registered.
        <a href="login.php">Login</a>'; 
    }
}

if (empty( $errors )){

    $q = "INSERT INTO users
        (first_name, last_name, email, pass, reg_date)
        VALUES ('$fn', $In', '$e', SHA1('$p'), NOW() )" ;

    $r = mysqli_query ( $dbc, $q );

    if ( $r ){
        echo '<h1>Registered!</h1>
              <p>You are now registered</p>
              <p><a href="login.php">Login</a></p>';
    }

    mysqli_close( $dbc );
    exit();
}
else {
    echo '<h1>Error!</h1>
          <p id="error_msg">The following error(s) occurred<br>' ;
    foreach ( $errors as $msg ){
        echo " - $msg<br>" ;
    }

    echo 'Please try again.</p>';
    mysqli_close( $dbc );
}

?>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top