Click here to Skip to main content
16,006,001 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys, So I'm trying to create a PHP Signup page, of which I though I was doing well, but once I click Register on the Form instead of hte User being created in the database, it instead produces the following error:

PHP Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /var/www/davarie.com/public_html/pages/signup.php on line 53, referer: http://davarie.com/pages/signup.php

I was creating this contact form via Bootstrap, and learnt the below via a book I was reading.

Below is my Signup.php file:

PHP
<?php
include('header.php');

if ($_SERVER['REQUEST_METHOD']=='POST')
{
#statements to run
require ('../connect_db.php');
$error = array();

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

#Requires surname
if ( empty($_POST['surname']))
{$errors[] = 'Enter your surname.';}
else
{$sn = mysqli_real_escape_string($dbc,
                       trim($_POST['surname']));}

#Requires email_address
if ( empty($_POST['email']))
{$errors[] = 'Enter your email_address.';}
else
{$ea = mysqli_real_escape_string($dbc,
                       trim($_POST['email']));}

#Requires password, also the checks to ensure that password and password confirmation match starts here.
if ( !empty( $_POST['password']))
{
 if ( $_POST['password']!= $_POST['password_confirmation'])
 { $errors[] = '<div class="alert alert-danger">
    <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
    Warning! Passwords do not match.
<a href="login.php">Login</a> </div>';}

 else
 {$p = mysqli_real_escape_string($dbc,
                       trim($_POST['password']));}
}
else { $errors[] ='Enter your password.';}

#Checks to ensure email address isn't already in use.
if (empty($errors))
{
 $q = "SELECT user_id FROM users WHERE email='$ea'";
 $r = mysqli_query ($dbc,$q);
 if ( mysqli_num_rows($r)!=0)
 { $errors[] = '<div class="alert alert-danger">
    <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
    Warning! Email Address already registered.
<a href="login.php">Login</a> </div>';}
}

#Inserts the registration data into the database
if ( empty($errors))
{
$q = "INSERT INTO users
      (frist_name, surname, email, password, reg_date)
       VALUES ('$fn','$sn','$ea', SHA1('$p'), NOW())";
$r = mysqli_query ($dbc, $q);

if ($r)
{
 echo '<div class="alert alert-success">
  Success! Account has been created.
<br>
  <p1> <a href="login.php">Login</a></p>
</div>';
}

#Closes the Database connection
mysqli_close( $dbc);
exit();
}

#Displays any errors with the registration.
else
{
 echo '  <div class="alert alert-danger">
    Error! <p id="err_msg">The following error(s) occurred:<br>';
        foreach ( $errors as $msg)
        {
         echo "- $msg<br>";
        }
        echo'Please try again.</p>
  </div>';
        mysqli_close( $dbc);
}
}
?>
<br>
<br>
<br>
<div class="container">
        <div class="row centered-form">
        <div class="col-sm-10">
                <div class="panel panel-default">
                        <div class="panel-heading">
                                        <h1 class="panel-title">Sign-up <small>please fill in the details below accurately</small></h1>
                                                </div>
                                                <div class="panel-body">
                                        <form role="form" method="post">
                                                <div class="row">
                                                        <div class="col-xs-6 col-sm-6 col-md-6">
                                                                <div class="form-group">
                                        <input type="text" name="first_name" id="first_name" class="form-control input-sm" placeholder="First Name">
                                                                </div>
                                                        </div>
                                                        <div class="col-xs-6 col-sm-6 col-md-6">
                                                                <div class="form-group">
                                                                        <input type="text" name="surname" id="surname" class="form-control input-sm" placeholder="Surname">
                                                                </div>
                                                        </div>
                                                </div>

                                                <div class="form-group">
                                                        <input type="email" name="email" id="email" class="form-control input-sm" placeholder="Email Address">
                                                </div>

                                                <div class="row">
                                                        <div class="col-xs-6 col-sm-6 col-md-6">
                                                                <div class="form-group">
                                                                        <input type="password" name="password" id="password" class="form-control input-sm" placeholder="Password">
                                                                </div>
                                                        </div>
                                                        <div class="col-sm-6">
                                                                <div class="form-group">
                                                                        <input type="password" name="password_confirmation" id="password_confirmation" class="form-control input-sm" placeholder="Confirm Password">
                                                                </div>
                                                        </div>
                                                </div>

                                                <input type="submit" value="Register" class="btn btn-success btn-block">

                                        </form>
                                </div>
                        </div>
                </div>
        </div>
    </div>
  </body>

<?php include('footer.php');?>
Posted

If the mysqli return values are the same (for data requests) as I receive from the SQL version then the bool return is 'false', indicating your query fail (probably invalid in some way).

Try hard-coding queries that you know work and replace the generated ones in your code. If that fixes the problem then you have your answer (except, of course, correcting how you construct your query).



 
Share this answer
 
Hi Balboos,

Thanks for your response,

:) I think this book isn't very great, I will learn to use PDO instead of Mysqli.
 
Share this answer
 
Comments
W Balboos, GHB 4-Jan-16 14:35pm    
MySQL is a very powerful database at a great price (free). The problem is not with the platform. Getting a better book, maybe.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900