0

I have 2 files connect.php (The connection is successful) to connect to the database. The register.php whiche includes the connect file. The code doesnt give any errors, it just doesnt send the information into the actual databse. Below is the code from each file and an image of the database I have setup.

Here is the actual code ran (I combined the connect.php with register.php for this it is just the connection to the database.)

<html>
<head>
    <title>User Registeration Using PHP & MySQL</title>

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" >

<link rel="stylesheet" href="styles.css" >

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>

<?php

  $connection = mysqli_connect('localhost', 'root', 'password', 'login');
if (!$connection){
    die("Database Connection Failed" . mysqli_error($connection));
}
$select_db = mysqli_select_db($connection, 'test');
if (!$select_db){
    die("Database Selection Failed" . mysqli_error($connection));
}

    // If the values are posted, insert them into the database.
    if (isset($_POST['username']) && isset($_POST['password'])){
        $username = $_POST['username'];
        $email = $_POST['email'];
        $password = md5($_POST['password']);

        $query = "INSERT INTO `user` (username, password, email) VALUES ('$username', '$password', '$email')";
        $result = mysqli_query($connection, $query);
        if($result){
            $smsg = "User Created Successfully.";
        }else{

            $fmsg ="User Registration Failed";
        }
    }
    ?>

<body>

<div class="container">
      <form class="form-signin" method="POST">

      <?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 } ?>
        <h2 class="form-signin-heading">Please Register</h2>
        <div class="input-group">
      <span class="input-group-addon" id="basic-addon1">@</span>
      <input type="text" name="username" class="form-control" placeholder="Username" required>
    </div>
        <label for="inputEmail" class="sr-only">Email address</label>
        <input type="email" name="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus>
        <label for="inputPassword" class="sr-only">Password</label>
        <input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required>
        <div class="checkbox">
          <label>
            <input type="checkbox" value="remember-me"> Remember me
          </label>
        </div>
        <button class="btn btn-lg btn-primary btn-block" type="submit">Register</button>
        <a class="btn btn-lg btn-primary btn-block" href="login.php">Login</a>
      </form>
</div>

</body>

</html>
chris85
  • 23,846
  • 7
  • 34
  • 51
MuslimTwin
  • 15
  • 3
  • 1
    Add an `action="xxx"` to the form element. And urgently see [How can I prevent SQL injection in PHP?](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Alex K. Mar 07 '17 at 15:48
  • 2
    You are open to SQL injections. What is `$smsg`/`$fmsg` currently being returned as? – chris85 Mar 07 '17 at 15:48
  • Please read http://stackoverflow.com/help/mcve – nerdlyist Mar 07 '17 at 15:48
  • Are the contents of your variables making it in to your query? – Andy Mar 07 '17 at 15:51
  • MD5'ing a password raw is not a good way to protect your users. Look in to using salts http://crypto.stackexchange.com/questions/1776/can-you-help-me-understand-what-a-cryptographic-salt-is – Andy Mar 07 '17 at 15:53
  • i have checked your code and working fine for me. check your db structure and your query filed name – Shafiqul Islam Mar 07 '17 at 16:14

0 Answers0