skip to Main Content

After running both services: Apache and MySQL,
When I try to register with some data, it is actually not pushed into the database which I selected. I mean MySQL Database does not recieves the input from the index.php form element.

Database Details:

hostname: localhost; database: registration_db; username: root; password: ”;
table name: users

I am creating a basic project which achieves register and login functionalities with PHP and MySQL.
I use Xampp Server with PHP version 7.2.4 and Apache 2.4.33.

My project files are in the following directory: C/Xampp/htdocs/CORE

db_connect.php

<?php
$con = @mysqli_connect("localhost", "root", "", "registration_db") or die ("error connecting to db");

?>

index.php

<?php 
require'db_connect.php';

$username = "";
$email    = "";
$errors = array();
    if (isset($_POST['username1']) && isset($_POST['pass1'])){



  $username = mysqli_real_escape_string($con, $_POST['username1']);

  $password_1 = mysqli_real_escape_string($con, $_POST['pass1']);
  $password_2 = mysqli_real_escape_string($con, $_POST['pass2']);

  // form validation: ensure that the form is correctly filled ...
  // by adding (array_push()) corresponding error unto $errors array
  if (empty($username)) { array_push($errors, "Username is required"); }
  if (empty($email)) { array_push($errors, "Email is required"); }
  if (empty($password_1)) { array_push($errors, "Password is required"); }
  if ($password_1 != $password_2) {
    array_push($errors, "The two passwords do not match");
  }

  // first check the database to make sure 
  // a user does not already exist with the same username and/or email
  $user_check_query = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1";
  $result = mysqli_query($con, $user_check_query);
  $user = mysqli_fetch_assoc($result);

  if ($user) { // if user exists
    if ($user['username'] === $username) {
      array_push($errors, "Username already exists");
    }

    if ($user['email'] === $email) {
      array_push($errors, "email already exists");
    }
  }

  // Finally, register user if there are no errors in the form
  if (count($errors) == 0) {
    $password = md5($password_1);//encrypt the password before saving in the database

    $sql = "INSERT INTO users (id, username, email, password) 
              VALUES('$id', $username', '$email', '$password1')";

    mysqli_query($con, $query);
    $_SESSION['username'] = $username;
    $_SESSION['success'] = "You are now logged in";
    header('location: index.php');
  }
}
    }
?>
<!DOCTYPE HTML>
<html>
    <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>CSR Nexus &mdash; CSR nexus is a dynamic organization focused solely on social responsibilities.</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="CSR Nexus" />
    <meta name="keywords" content="CSR Nexus, CSR, Nexus" />
    <meta name="author" content="CSR Nexus Team" />



    <!-- Facebook and Twitter integration -->
    <meta property="og:title" content=""/>
    <meta property="og:image" content=""/>
    <meta property="og:url" content=""/>
    <meta property="og:site_name" content=""/>
    <meta property="og:description" content=""/>
    <meta name="twitter:title" content="" />
    <meta name="twitter:image" content="" />
    <meta name="twitter:url" content="" />
    <meta name="twitter:card" content="" />

    <link href="https://fonts.googleapis.com/css?family=Roboto:300,400,700" rel="stylesheet">

    <!-- Animate.css -->
    <link rel="stylesheet" href="css/animate.css">
    <!-- Icomoon Icon Fonts-->
    <link rel="stylesheet" href="css/icomoon.css">
    <!-- Themify Icons-->
    <link rel="stylesheet" href="css/themify-icons.css">
    <!-- Bootstrap  -->
    <link rel="stylesheet" href="css/bootstrap.css">

    <!-- Magnific Popup -->
    <link rel="stylesheet" href="css/magnific-popup.css">

    <!-- Owl Carousel  -->
    <link rel="stylesheet" href="css/owl.carousel.min.css">
    <link rel="stylesheet" href="css/owl.theme.default.min.css">

    <!-- Theme style  -->
    <link rel="stylesheet" href="css/style.css">

    <!-- Modernizr JS -->
    <script src="js/modernizr-2.6.2.min.js"></script>
    <!-- FOR IE9 below -->
    <!--[if lt IE 9]>
    <script src="js/respond.min.js"></script>
    <![endif]-->

    </head>
    <body>

    <div class="gtco-loader"></div>

    <div id="page">


    <div class="page-inner">
    <nav class="gtco-nav" role="navigation">
        <div class="gtco-container">

            <div class="row">
                <div class="col-sm-4 col-xs-12">
                    <div id="gtco-logo"><a href="index.html">CSR Nexus </a></div>
                </div>
                <div class="col-xs-8 text-right menu-1">
                    <ul>

                        <li class="has-dropdown">
                            <a href="#">Organization</a>
                            <ul class="dropdown">
                                <li><a href="registration/index.html">Register</a></li>
                                <li><a href="organization login/index.html">Login</a></li>

                            </ul>
                        </li>

                        <li><a href="contact.html">Contact</a></li>

                    </ul>
                </div>
            </div>

        </div>
    </nav>

    <header id="gtco-header" class="gtco-cover" role="banner" style="background-image: url(images/img_4.jpg)">
        <div class="overlay"></div>
        <div class="gtco-container">
            <div class="row">
                <div class="col-md-12 col-md-offset-0 text-left">


                    <div class="row row-mt-15em">
                        <div class="col-md-7 mt-text animate-box" data-animate-effect="fadeInUp">
                            <span class="intro-text-small">Welcome to CSR Nexus</span>
                            <h1>CSR nexus is a dynamic organization focused solely on social responsibilities.</h1> 
                        </div>
                        <div class="col-md-4 col-md-push-1 animate-box" data-animate-effect="fadeInRight">
                            <div class="form-wrap">
                                <div class="tab">
                                    <ul class="tab-menu">
                                        <li class="active gtco-first"><a href="#" data-tab="signup">Sign up</a></li>
                                        <li class="gtco-second"><a href="#" data-tab="login">Login</a></li>
                                    </ul>
                                    <div class="tab-content">
                                        <div class="tab-content-inner active" data-content="signup">
                                            <form id="registration" method="POST">
                                                <?php include('errors.php'); ?>
                                                <?php if(isset($smsg)){ ?><div class="alert alert-success" role="alert"> <?php echo $smsg; ?> </div><?php } ?>
                                                <?php if(isset($fmsg)){ ?><div class="alert alert-danger" role="alert"> <?php echo $fmsg; ?> </div><?php } ?>
                                                <div class="row form-group">
                                                    <div class="col-md-12">
                                                        <label for="username">Username or Email</label>
                                                        <input type="text" class="form-control" id="username1" >
                                                    </div>
                                                </div>
                                                <div class="row form-group">
                                                    <div class="col-md-12">
                                                        <label for="password">Password</label>
                                                        <input type="password" class="form-control" id="pass1" >
                                                    </div>
                                                </div>
                                                <div class="row form-group">
                                                    <div class="col-md-12">
                                                        <label for="password2">Repeat Password</label>
                                                        <input type="password" class="form-control" id="pass2">
                                                    </div>
                                                </div>

                                                <div class="row form-group">
                                                    <div class="col-md-12">
                                                        <input type="submit" name="submit" class="btn btn-primary" value="Sign up">
                                                    </div>
                                                </div>
                                            </form> 
                                        </div>

                                        <div class="tab-content-inner" data-content="login">
                                            <form id="login" method="post" action="login.php">
                                                <?php include('errors.php'); ?> 
                                                <div class="row form-group">
                                                    <div class="col-md-12">
                                                        <label for="username">Username or Email</label>
                                                        <input type="text" class="form-control" id="username">
                                                    </div>
                                                </div>
                                                <div class="row form-group">
                                                    <div class="col-md-12">
                                                        <label for="password">Password</label>
                                                        <input type="password" class="form-control" id="password">
                                                    </div>
                                                </div>

                                                <div class="row form-group">
                                                    <div class="col-md-12">
                                                        <input type="submit" class="btn btn-primary" value="Login">
                                                    </div>
                                                </div>
                                            </form> 
                                        </div>

                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>


                </div>
            </div>
        </div>
    </header>




    <!-- jQuery -->
    <script src="js/jquery.min.js"></script>
    <!-- jQuery Easing -->
    <script src="js/jquery.easing.1.3.js"></script>
    <!-- Bootstrap -->
    <script src="js/bootstrap.min.js"></script>
    <!-- Waypoints -->
    <script src="js/jquery.waypoints.min.js"></script>
    <!-- Carousel -->
    <script src="js/owl.carousel.min.js"></script>
    <!-- countTo -->
    <script src="js/jquery.countTo.js"></script>
    <!-- Magnific Popup -->
    <script src="js/jquery.magnific-popup.min.js"></script>
    <script src="js/magnific-popup-options.js"></script>
    <!-- Main -->
    <script src="js/main.js"></script>

    </body>
</html>

2

Answers


  1. You missed an ' single quote around $username

    $sql = "INSERT INTO users (id, username, email, password) 
                  VALUES('$id', $username', '$email', '$password1')";
    

    Correted

    $sql = "INSERT INTO users (id, username, email, password) 
                  VALUES('$id', '$username', '$email', '$password1')";
    

    You are also missing the names of all the input fields in your form as Below:

    <input type="text" class="form-control" id="username1" name="username1">
    <input type="password" class="form-control" id="pass1" name="pass1">
    <input type="password" class="form-control" id="pass2" name="pass2">
    

    And There also no input filed for email add filed for email:

    <input type="email" class="form-control" id="email" name="email">
    
    Login or Signup to reply.
  2. you are missing name attribute in your html.

    Change

    <input type="text" class="form-control" id="username1" >
    

    With

    <input type="text" class="form-control" id="username1" name="username1">
    

    And you are missing action attribute in the form.your registration from shoul be

    <form id="registration" method="POST" action="action.php">
    <?php include('errors.php'); ?>
    <?php if(isset($smsg)){ ?><div class="alert alert-success" role="alert"> <?php echo $smsg; ?> </div><?php } ?>
    <?php if(isset($fmsg)){ ?><div class="alert alert-danger" role="alert"> <?php echo $fmsg; ?> </div><?php } ?>
    <div class="row form-group">
    <div class="col-md-12">
    <label for="username">Username or Email</label>
    <input type="text" class="form-control" id="username1" name="username1">
    </div>
    </div>
    <div class="row form-group">
    <div class="col-md-12">
    <label for="password">Password</label>
    <input type="password" class="form-control" id="pass1" name="pass1">
    </div>
    </div>
    <div class="row form-group">
    <div class="col-md-12">
    <label for="password2">Repeat Password</label>
    <input type="password" class="form-control" id="pass2" name="pass2">
    </div>
    </div>
    
    <div class="row form-group">
    <div class="col-md-12">
    <input type="submit" name="submit" class="btn btn-primary" value="Sign up">
    </div>
    </div>
    </form> 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search