Php表单没有向MySql数据库提交数据

时间:2015-09-06 19:52:15

标签: php mysql html5

我试图创建一个允许用户创建自己帐户的注册系统,该脚本表示信息已成功发送到数据库,但事实上并非如此。这是代码。

的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
}
}
?>

我知道我应该将代码升级到MySqli或PDO。

1 个答案:

答案 0 :(得分:0)

对不起,评论中的内容很难阅读......以下内容对我有用:

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')";