0

I am creating a register page for a website.

here is my sql code :

| Field    | Type         | Null | Key | Default | Extra          |

| id       | int(11)      | NO   | PRI | NULL    | auto_increment |
| name     | varchar(50)  | NO   |     | NULL    |                |
| course   | varchar(50)  | NO   |     | NULL    |                |
| year     | int(1)       | NO   |     | NULL    |                |
| password | varchar(500) | NO   |     | NULL    |                |
| contact  | int(20)      | NO   |     | NULL    |                |
| email    | varchar(200) | NO   |     | NULL    |                |

php file is :

    <div class="container" style="padding:20px">
    <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading"><h2>Registration Form</h2>
                </div>
                <div class="panel-body">
                    <div class="col-lg-6">
                        <form>
                    <div class="form-group">
                    <label>Name :</label>
                            <input class="form-control" type="string"
                            id="inputname" name="name" placeholder="Your Name" required>
                    </div>
                     <div class="form-group">
                           <label for="sel1">Select Course :</label>
                              <select class="form-control" id="course" name="course" required>
                                <option>B.A. Programme </option>
                                <option>B.Com </option>
                                <option>B.Com(Hons.)</option>
                                <option>B.A.(Hons) Economics</option>
                                <option>B.A. (Hons.) English</option>
                                <option>B.A. (Hons.) History</option>
                                <option>B.A. (Hons.) Hindi</option>
                                <option>B.A. (Hons.) Political Science</option>
                                <option>B.A. (Hons.) Sanskrit</option>
                                <option>B.Sc. (Hons.) Mathematics</option>
                                <option>B.Sc.(Hons) Statistics</option>
                                <option>B.Sc.(Hons) Computer Science</option>
                                <option>B.Sc. Mathematical Science</option>
                              </select>
                    </div> 
                    <div class="form-group">
                           <label for="sel1">Year :</label>
                              <select class="form-control" id="year" name="year" required>
                                <option>1 </option>
                                <option>2</option>
                                <option>3</option>
                              </select>
                    </div>
                    <div class="form-group">
                    <label>Password : </label>
                            <input class="form-control" type="password"
                            id="password" name="password" placeholder="Password" autocomplete="off" required>
                    </div> 
                    <div class="form-group">
                    <label>Confirm Password  : </label>
                            <input class="form-control" type="text"
                            id="confirm_password" name="confirm_password" placeholder="Confirm password" required>
                    </div>  
                    <div class="form-group">
                    <label>Contact No. : </label>
                            <input class="form-control" type="phonenum"
                            id="inputnumber" name="contact" placeholder="Your Contact Number" required>
                    </div>
                    <div class="form-group">
                    <label>Email : </label>
                            <input class="form-control" type="email"
                            id="inputname" name="email" placeholder="Your Email Id" required>
                    </div>
                    <input type="submit" name="submit" value="Register" class="btn btn-default" required>
                    </form>
                    </div>

                    <div class="col-lg-6" style="margin-top:10px">
                        <div class="panel panel-warning" >
                              <div class="panel-heading"><h3>Information</h3></div>
                              <div class="panel-body">
                                <ul>
                                <li>Complete the from to register with us.</li>  
                                <li>Fill out the Details carefully.</li>  
                                <li>Fill in your name as it is in your college ID.</li>  
                                <li>Use your correct Email ID and Conatct No. so that we can contact to you.</li>  
                                </ul>
                              </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
        var password = document.getElementById("password")
      , confirm_password = document.getElementById("confirm_password");

    function validatePassword(){
      if(password.value != confirm_password.value) {
        confirm_password.setCustomValidity("Passwords Don't Match");
      } else {
        confirm_password.setCustomValidity('');
      }
    }

    password.onchange = validatePassword;
    confirm_password.onkeyup = validatePassword;
</script>
<?php
    include "assets/backend/db.php";
    if(isset($_POST['name'])) {
        $name=$_POST['name'];
    }
    $name = $mysqli->escape_string($_POST['name']);
    if(isset($_POST['course'])) {
        $course=$_POST['course'];
    }
    if(isset($_POST['year'])) {
        $year=$_POST['year'];
    }
    if(isset($_POST['password'])) {
        $password=$_POST['password'];
    }
    $password = $mysqli->escape_string(password_hash($_POST['password'], md5));
    if(isset($_POST['contact'])) {
        $contact=$_POST['contact'];
    }
        $contact = $mysqli->escape_string($_POST['contact']);
    if(isset($_POST['email'])) {
        $email=$_POST['email'];
    }
    $email = $mysqli->escape_string($_POST['email']);
    // Check if user with that email already exists
    $result = $mysqli->query("SELECT * FROM users WHERE email='$email'") or die($mysqli->error());
    // We know user email exists if the rows returned are more than 0
    if ( $result->num_rows > 0 ) {
    echo  'User with this email already exists!';
    header("location: error.php");
    }
    else { // Email doesn't already exist in a database, proceed...
        $sql="INSERT INTO `user`(`name`, `course`, `year`, `password`, `contact`, `email`) VALUES ('$name','$course','$year','$password','$contact','$email')";
         // Add user to the database
        if ( $mysqli->query($sql) ){
            echo "You are successfully registered";   
        }
        else {
            echo "Registration failed";
            header("location:join.php");
        } 
    }

?>

Errors that I get:

Notice: Undefined index: name in C:\xampp\htdocs\PlacementCell\assets\php\register_form.php on line 103

Notice: Undefined index: password in C:\xampp\htdocs\PlacementCell\assets\php\register_form.php on line 113

Notice: Use of undefined constant md5 - assumed 'md5' in C:\xampp\htdocs\PlacementCell\assets\php\register_form.php on line 113

Warning: password_hash() expects parameter 2 to be long, string given in C:\xampp\htdocs\PlacementCell\assets\php\register_form.php on line 113

Notice: Undefined index: contact in C:\xampp\htdocs\PlacementCell\assets\php\register_form.php on line 117

Notice: Undefined index: email in C:\xampp\htdocs\PlacementCell\assets\php\register_form.php on line 121

Fatal error: Call to undefined method mysqli::error() in C:\xampp\htdocs\PlacementCell\assets\php\register_form.php on line 123

Help me fix this: :)

Marc Delisle
  • 8,879
  • 3
  • 29
  • 29
  • 2
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – B. Desai Jul 09 '17 at 09:49
  • I was able to correct some of error. errors I am left with are : Notice: Use of undefined constant md5 - assumed 'md5' in C:\xampp\htdocs\PlacementCell\assets\php\register_form.php on line 113 Warning: password_hash() expects parameter 2 to be long, string given in C:\xampp\htdocs\PlacementCell\assets\php\register_form.php on line 113 Fatal error: Call to undefined method mysqli::error() in C:\xampp\htdocs\PlacementCell\assets\php\register_form.php on line 123 –  Jul 09 '17 at 09:53
  • See the manual of `password_hash`, http://php.net/manual/en/function.password-hash.php – B. Desai Jul 09 '17 at 09:55
  • mysqli:error is not a method, it is a variable. You might want to change php.ini error_reporting to `E_ALL & ~E_NOTICE`, so you won't get those notices, which aren't really important in PHP. –  Jul 09 '17 at 10:14
  • $password = $mysqli->escape_string( password_hash( "$password" , PASSWORD_BCRYPT)); I used this. Although there are no errors but the code is not working. –  Jul 09 '17 at 10:31
  • see full code here : https://pastebin.com/v5MeYZrP –  Jul 09 '17 at 10:32
  • I got the code working but this block of code is not working: if ( $result->num_rows > 0 ) { echo 'User with this email already exists!'; } with an error : Trying to get property of non-object in C:\xampp\htdocs\PlacementCell\assets\php\register_form.php on line 126 –  Jul 09 '17 at 10:43

1 Answers1

0

Okay, I finally got everything working.
Some issues wore : I was typing the name of my table wrong. I was using "users" on the place of "user"
Some semicolon mistake and some brackets mistake but I was finally able to correct them all.

Here is my Code:

    <div class="container" style="padding:20px">
    <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading"><h2>Registration Form</h2>
                </div>
                <div class="panel-body">
                    <div class="col-lg-6">
                        <form action="" method="post">
                    <div class="form-group">
                    <label>Name :</label>
                            <input class="form-control" type="string"
                            id="inputname" name="name" placeholder="Your Name" required>
                    </div>
                     <div class="form-group">
                           <label for="sel1">Select Course :</label>
                              <select class="form-control" id="course" name="course" required>
                                <option>B.A. Programme </option>
                                <option>B.Com </option>
                                <option>B.Com(Hons.)</option>
                                <option>B.A.(Hons) Economics</option>
                                <option>B.A. (Hons.) English</option>
                                <option>B.A. (Hons.) History</option>
                                <option>B.A. (Hons.) Hindi</option>
                                <option>B.A. (Hons.) Political Science</option>
                                <option>B.A. (Hons.) Sanskrit</option>
                                <option>B.Sc. (Hons.) Mathematics</option>
                                <option>B.Sc.(Hons) Statistics</option>
                                <option>B.Sc.(Hons) Computer Science</option>
                                <option>B.Sc. Mathematical Science</option>
                              </select>
                    </div> 
                    <div class="form-group">
                           <label for="sel1">Year :</label>
                              <select class="form-control" id="year" name="year" required>
                                <option>1 </option>
                                <option>2</option>
                                <option>3</option>
                              </select>
                    </div>
                    <div class="form-group">
                    <label>Password : </label>
                            <input class="form-control" type="password"
                            id="password" name="password" placeholder="Password" autocomplete="off" required>
                    </div> 
                    <div class="form-group">
                    <label>Confirm Password  : </label>
                            <input class="form-control" type="text"
                            id="confirm_password" name="confirm_password" placeholder="Confirm password" required>
                    </div>  
                    <div class="form-group">
                    <label>Contact No. : </label>
                            <input class="form-control" type="phonenum"
                            id="inputnumber" name="contact" placeholder="Your Contact Number" required>
                    </div>
                    <div class="form-group">
                    <label>Email : </label>
                            <input class="form-control" type="email"
                            id="inputname" name="email" placeholder="Your Email Id" required>
                    </div>
                    <input type="submit" name="submit" value="Register" class="btn btn-default" required>
                    </form>
                    </div>

                    <div class="col-lg-6" style="margin-top:10px">
                        <div class="panel panel-warning" >
                              <div class="panel-heading"><h3>Information</h3></div>
                              <div class="panel-body">
                                <ul>
                                <li>Complete the from to register with us.</li>  
                                <li>Fill out the Details carefully.</li>  
                                <li>Fill in your name as it is in your college ID.</li>  
                                <li>Use your correct Email ID and Conatct No. so that we can contact to you.</li>  
                                </ul>
                              </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
        var password = document.getElementById("password")
      , confirm_password = document.getElementById("confirm_password");

    function validatePassword(){
      if(password.value != confirm_password.value) {
        confirm_password.setCustomValidity("Passwords Don't Match");
      } else {
        confirm_password.setCustomValidity('');
      }
    }

    password.onchange = validatePassword;
    confirm_password.onkeyup = validatePassword;
</script>

<?php 
    include "assets/backend/db.php";
    if(isset($_POST['name'])) {
        $name=$_POST['name'];

    $name = $mysqli->escape_string($_POST['name']);
    if(isset($_POST['course'])) {
        $course=$_POST['course'];

    if(isset($_POST['year'])) {
        $year=$_POST['year'];

    if(isset($_POST['password'])) {
        $password=$_POST['password'];

    $password = $mysqli->escape_string( password_hash( "$password" , PASSWORD_BCRYPT));
    if(isset($_POST['contact'])) {
        $contact=$_POST['contact'];

        $contact = $mysqli->escape_string($_POST['contact']);
    if(isset($_POST['email'])) {
        $email=$_POST['email'];

    $email = $mysqli->escape_string($_POST['email']);
    // Check if user with that email already exists
    $result = $mysqli->query("SELECT * FROM user WHERE email='$email'") or die($mysqli->error());
    $result->num_rows;
    // We know user email exists if the rows returned are more than 0
    if ( $result->num_rows > 0 ) {
    echo  'User with this email already exists!';

    }
    else { // Email doesn't already exist in a database, proceed...
        $sql="INSERT INTO `user`(`name`, `course`, `year`, `password`, `contact`, `email`) VALUES ('$name','$course','$year','$password','$contact','$email')";
         // Add user to the database
        if ( $mysqli->query($sql) ){
            echo "You are successfully registered";   
        }
        else {
            echo "Registration failed";
            header("location:join.php");
        } 
    }
    }
    }
    }
    }
    }
    }

?>

This was a fun learning process.