0

Im trying to make a register system that will allow the user to make their own account, the script says that the information was sent to the database successfully, but in actual fact it's not. here is the code.

Index.php

    <?php
include('login.php'); // Includes Login Script
if(isset($_SESSION['login_user'])){
header("location: home.php");
}
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Network TV Login Page</title>
    <link href="css/bootstrap.css" rel="stylesheet">
  </head>
  <body style="background:center no-repeat fixed url('https://upload.wikimedia.org/wikipedia/commons/b/b5/Melbourne_by_night.jpg'); background-size: cover">
    <div id="body"> <!-- Start body div -->
        <div id="nav"> <!-- Start of nav-->
        <nav class="navbar navbar-default navbar-fixed-top"> <!-- Start of nav class-->
      <div class="container"> <!-- Start of nav container -->
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">Network TV</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
        </div><!--/.nav-collapse -->
      </div> <!-- End  of nav container -->
    </nav> <!-- End of nav class-->
        </div> <!-- End of nav -->

    <center> <!-- center login/ registration forum -->
    <div id="Login"> <!-- Start login/ registration div --->
    </div> <!-- End login/ registration div-->
  <div class="container">
        <div class="row">
            <div class="col-md-6 col-md-offset-3">
                <div class="panel panel-login">
                    <div class="panel-heading">
                        <div class="row">
                            <div class="col-xs-6">
                                <a href="#" class="active" id="login-form-link"><u>Login</u></a>
                            </div>
                            <div class="col-xs-6">
                                <a href="#" id="register-form-link"><u>Register</u></a>
                            </div>
                        </div>
                        <hr>
                    </div>
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-lg-12">
                                <form id="login-form" action="" method="post" role="form" style="display: block;">
                                    <div class="form-group">
                                        <input id="name" name="username" placeholder="Username" type="text" tabindex="1" class="form-control">
                                    </div>
                                    <div class="form-group">
                                        <input id="password" name="password" placeholder="Password" type="password"   tabindex="2" class="form-control">
                                    </div>
                                    <div class="form-group">
                                        <div class="row">
                                            <div class="col-sm-6 col-sm-offset-3">
                                                <input class="form-control btn btn-success" name="submit" type="submit" value="Log In" id="submit" tabindex="3" >
                                            </div>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <div class="row">
                                            <div class="col-lg-12">
                                                <div class="text-center">
                                                    <a href="recover.php" tabindex="5" class="forgot-password">Forgot Password?</a>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </form>
                                <form id="register-form" action="index.php" method="post" role="form" style="display: none;">
                                    <div class="form-group">
                                        <input type="text" name="newusername" id="newusername" tabindex="1" class="form-control validate-input" placeholder="Username" value="" autocomplete="off">
                                    </div>
                                    <div class="form-group">
                                        <input type="email" name="newemail" id="newemail" tabindex="2" class="form-control validate-input" placeholder="Email Address" value="" autocomplete="off">
                                    </div>
                                    <div class="form-group">
                                        <input type="password" name="newpassword1" id="password" tabindex="10" class="form-control" placeholder="Password" autocomplete="off">
                                    </div>
                                    <div class="form-group">
                                        <input type="password" name="newpassword1" id="password" tabindex="10" class="form-control" placeholder="Password" autocomplete="off">
                                    </div>
                                    <div class="form-group">
                                        <div class="row">
                                            <div class="col-sm-6 col-sm-offset-3">
                                                <input type="submit" name="register-submit" id="register-submit" tabindex="12" class="form-control btn btn-success" value="Register">
                                            </div>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    </center> <!-- Stop center -->




    <div> <!-- End body div -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <style>
    body {
    padding-top: 90px;
}
.panel-login {
    border-color: #ccc;
    -webkit-box-shadow: 0px 2px 3px 0px rgba(0,0,0,0.2);
    -moz-box-shadow: 0px 2px 3px 0px rgba(0,0,0,0.2);
    box-shadow: 0px 2px 3px 0px rgba(0,0,0,0.2);
}
.panel-login>.panel-heading {
    color: #00415d;
    background-color: #fff;
    border-color: #fff;
    text-align:center;
}
.panel-login>.panel-heading a{
    text-decoration: none;
    color: #666;
    font-weight: bold;
    font-size: 15px;
    -webkit-transition: all 0.1s linear;
    -moz-transition: all 0.1s linear;
    transition: all 0.1s linear;
}
.panel-login>.panel-heading a.active{
    color: #029f5b;
    font-size: 18px;
}
.panel-login>.panel-heading hr{
    margin-top: 10px;
    margin-bottom: 0px;
    clear: both;
    border: 0;
    height: 1px;
    background-image: -webkit-linear-gradient(left,rgba(0, 0, 0, 0),rgba(0, 0, 0, 0.15),rgba(0, 0, 0, 0));
    background-image: -moz-linear-gradient(left,rgba(0,0,0,0),rgba(0,0,0,0.15),rgba(0,0,0,0));
    background-image: -ms-linear-gradient(left,rgba(0,0,0,0),rgba(0,0,0,0.15),rgba(0,0,0,0));
    background-image: -o-linear-gradient(left,rgba(0,0,0,0),rgba(0,0,0,0.15),rgba(0,0,0,0));
}
.panel-login input[type="text"],.panel-login input[type="email"],.panel-login input[type="password"] {
    height: 45px;
    border: 1px solid #ddd;
    font-size: 16px;
    -webkit-transition: all 0.1s linear;
    -moz-transition: all 0.1s linear;
    transition: all 0.1s linear;
}
.panel-login input:hover,
.panel-login input:focus {
    outline:none;
    -webkit-box-shadow: none;
    -moz-box-shadow: none;
    box-shadow: none;
    border-color: #ccc;
}
.btn-login {
    background-color: #59B2E0;
    outline: none;
    color: #fff;
    font-size: 14px;
    height: auto;
    font-weight: normal;
    padding: 14px 0;
    text-transform: uppercase;
    border-color: #59B2E6;
}
.btn-login:hover,
.btn-login:focus {
    color: #fff;
    background-color: #53A3CD;
    border-color: #53A3CD;
}
.forgot-password {
    text-decoration: underline;
    color: #888;
}
.forgot-password:hover,
.forgot-password:focus {
    text-decoration: underline;
    color: #666;
}

.btn-register {
    background-color: #1CB94E;
    outline: none;
    color: #fff;
    font-size: 14px;
    height: auto;
    font-weight: normal;
    padding: 14px 0;
    text-transform: uppercase;
    border-color: #1CB94A;
}
.btn-register:hover,
.btn-register:focus {
    color: #fff;
    background-color: #1CA347;
    border-color: #1CA347;
}

    </style>
  <script>
  $(function() {

    $('#login-form-link').click(function(e) {
        $("#login-form").delay(100).fadeIn(100);
        $("#register-form").fadeOut(100);
        $('#register-form-link').removeClass('active');
        $(this).addClass('active');
        e.preventDefault();
    });
    $('#register-form-link').click(function(e) {
        $("#register-form").delay(100).fadeIn(100);
        $("#login-form").fadeOut(100);
        $('#login-form-link').removeClass('active');
        $(this).addClass('active');
        e.preventDefault();
    });

});

  </script>
  </body>
</html>
<?php
If($_POST){
    mysql_connect("localhost","root","Oliver");
    mysql_select_db("users");
    if(isset($_POST['register-submit'])){
    $user_name = $_POST['newusername'];
    $password = $_POST['newpassword1'];
    $email = $_POST['newemail'];
    }
    $passwordmd5 = md5($password);
    $query = "insert into username (username,password,email) values ('$user_name','$passwordmd5','$email')";
    $query = "DELETE FROM username WHERE username = ''";

    mysql_connect("localhost","root","Oliver");
    mysql_select_db("videos");
    $query = "DELETE FROM videos WHERE name = ''";

    if (mysql_query($query)){
        echo 'registration successful';
        exit;
    }

}
?>

Login.php

    <?php
session_start(); // Starting Session
$error=''; // Variable To Store Error Message
if (isset($_POST['submit'])) {
if (empty($_POST['username']) || empty($_POST['password'])) {
$error = "Username or Password is invalid";
}
else
{
// Define $username and $password
$username = $_POST['username'];
$password = md5($_POST['password']);
// Establishing Connection with Server by passing server_name, user_id and password as a parameter
$connection = mysql_connect("localhost", "root", "Oliver");
// To protect MySQL injection for Security purpose
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
// Selecting Database
$db = mysql_select_db("users", $connection);
// SQL query to fetch information of registerd users and finds user match.
$query = mysql_query("select * from username where password='$password' AND username='$username'", $connection);
$rows = mysql_num_rows($query);
if(isset($_SESSION['login_user']))
session_destroy();
if ($rows == 1) {
$_SESSION['login_user']=$username; // Initializing Session
header("location: profile.php"); // Redirecting To Other Page
} else {
$error = "Username or Password is invalid";
}
mysql_close($connection); // Closing Connection
}
}
?>

I know I should upgrade the code to MySqli or PDO.

Oliver Ketley
  • 83
  • 1
  • 1
  • 13

1 Answers1

0

Sorry, this was getting hard to read in the comments... The below worked for me:

if(isset($_POST['register-submit'])){
        $name = $_POST['newusername'];
        $pass = md5($_POST['newpassword1']);
        $email = $_POST['newemail'];
    }
$query = "INSERT INTO `username` (`username`, `email`, `password`) VALUES ('$name', '$email', '$pass')";
0x736a64
  • 126
  • 6
  • hey, if i substitute the variables for actual information is works, for example. $query = "insert into username (username,password,email) values ('12235346','1234','1234')"; it works perfectly – Oliver Ketley Sep 06 '15 at 20:00
  • sorry, i misread... your db is named "users..." Can you check to make sure the POST values are being set for username, password and email, like: $user_name = $_POST['newusername']; $password = $_POST['newpassword1']; $email = $_POST['newemail']; var_dump($user_name)... – 0x736a64 Sep 06 '15 at 20:03
  • never done var_dump what exactly do you need to me to write? – Oliver Ketley Sep 06 '15 at 20:05
  • I figured it out, this is the reply, string(4) "sfsf" string(6) "sfsdfs" string(16) "fsdfsf@gmail.com" registration successful – Oliver Ketley Sep 06 '15 at 20:07
  • if( isset( $_POST['register-submit'] ) ) { $user_name = $_POST['newusername']; $password = $_POST['newpassword1']; $email = $_POST['newemail']; } var_dump($user_name); //will output the value of $user_name if it has one You can also make sure the $_POST['newusername'] has a value by using if isset($_POST['newusername']) $user_name = $_POST['newusername']; – 0x736a64 Sep 06 '15 at 20:09
  • This is the reply. string(8) "username" string(8) "username" string(8) "password" string(15) "Email@email.com" registration successful – Oliver Ketley Sep 06 '15 at 20:12
  • It could also be the single quotes wrapping your variables, but that should still send strings along to the db. It's good that values are being set though. – 0x736a64 Sep 06 '15 at 20:13
  • removed the quotes that too does not work :/ – Oliver Ketley Sep 06 '15 at 20:15
  • have you tried to limit the logic to only running the insert query before the following two delete queries? – 0x736a64 Sep 06 '15 at 20:25
  • Yes I tried that too :/ this is strange – Oliver Ketley Sep 06 '15 at 20:28
  • I would make sure you have logging enabled for mysql and try to see if anything helpful is showing there: https://dev.mysql.com/doc/refman/5.1/en/query-log.html – 0x736a64 Sep 06 '15 at 20:32
  • I have another site running on my network, using the same code, nearly apart form entry fields and less information included in the query, that works fine .. – Oliver Ketley Sep 06 '15 at 20:34