使用" PHP格式"无法连接/提交到MySQL

时间:2017-02-13 12:45:10

标签: php mysql forms

我有两个问题。 基本故事:我创建了一个SIMPLE注册和登录系统 问题1:如果我尝试注册一个新帐户,那么它会说"用户注册失败"。目前它应该说,因为mysql无法从表单中获取正确的信息。但问题是我不知道为什么。一切似乎都正确...... 问题2:如果我尝试使用现有帐户登录,那么浏览器似乎只刷新页面而没有别的......
注册php代码:

 <?php
   require ('insert.php');
  // If values posted, insert into the database.
    if (isset($_POST['username']) && isset($_POST['password'])){
        $name = $_POST['name'];
        $email = $_POST['email'];
        $username = $_POST['username'];
        $password = $_POST['password'];

        // nimi refers to name, it's correct
        $query = "INSERT INTO `user` (nimi, email, username, password) 
                    VALUES('$name', '$email', '$username', '$password')";

        //POST retrieves the data.
        $result = mysqli_query($connection, $query);

        if($result){
            $smsg = "User Created Successfully.";
        } else {
            $fmsg = "User Registration Failed";
        }
    }

    mysqli_close($connection);
    ?>
     <html>
    ...
    <body>
    ...
    <div>

        <form method="POST" class="form-horizontal" role="form">

        <!-- Status, how registering went -->
        <?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 } ?>


        <!-- Registration form starts -->
            <h2>Form</h2><br>


                <label for="Name"></label>
                <input name="name" type="text" id="name" maxlength="40" placeholder="Ees- ja perenimi" class="form-control" autofocus> <!-- lopp -->

                <label for="email"></label>
                <input name="email" type="email" id="email" maxlength="65" placeholder="Email" class="form-control"> <!-- lopp -->


                <label for="Username"></label>
                <input name="username" type="text" id="userName" maxlength="12" placeholder="Kasutajatunnus/kasutajanimi" class="form-control" required> <!-- lopp -->

                <label for="Password"></label>
                <input name="password" type="password" id="password" maxlength="12" placeholder="Parool" class="form-control" required>

<button type="submit" class="btn btn-primary btn-block">Join</button>
            </form> <!-- /form -->

        </div> <!-- ./container -->
    ...
    </body>
</html>

登录:

<?php
session_start();
require ('insert.php');

//Is username and password typed?
if (isset($_POST['username']) and isset($_POST['password'])){
    //Making vars from inputs
    $username = $_POST['username'];
    $password = $_POST['password'];
    //Checking existent of values.
    $query = "SELECT * FROM `liikmed` 
                WHERE username='$username' 
                and password='$password'";

    $result = mysqli_query($connection, $query) 
                    or die(mysqli_error($connection));
    $count = mysqli_num_rows($result);
    //3.1.2 If values equal, create session.
    if ($count == 1){
        $_SESSION['username'] = $username;
    } else {
        //If credentials doesn't match.
        $fmsg = "Invalid Login Credentials.";
    }
}
//if user logged in, welcome with message
if (isset($_SESSION['username'])){
    $username = $_SESSION['username'];
    echo "Hai " . $username . "";
    echo "This is the Members Area";
    echo "<a href='logout.php'>Logout</a>";

}else{}
?>

<html>
...
<body>
...
<div id="bg"></div>


    <form method="POST" class="form-horizontal">
        <h2>Login</h2><br>

        <label for="User"></label>
        <input name="username" type="text" maxlength="15" placeholder="Username" class="form-control" required autofocus>

        <label for="Password"></label>
        <input name="password" type="password" maxlength="50" placeholder="Password" class="form-control" required autofocus>

        <button type="submit" class="btn btn-primary btn-block">Enter</button>

</form>   
</div>
...
</body>
</html>

最后是php数据库连接文件(名为insert.php):

<?php
$connection=mysqli_connect("localhost","root","pw");
if (!$connection){
    die("Database Connection Failed" . mysqli_error($connection));
}
$select_db = mysqli_select_db($connection, 'my_database');
if (!$select_db){
    die("Database Selection Failed" . mysqli_error($connection));
}
?>

2 个答案:

答案 0 :(得分:2)

首先,在您的登录PHP代码中,您只启动了session,但如果登录成功,您没有告诉从哪里指示。在代码中添加header。那是;

if ($count == 1){
    $_SESSION['username'] = $username;
    header("Location: page.php"); //the page you want it to go to
}

您的注册PHP代码看起来不错。如果你在那里拼错了任何东西,请检查你的数据库表。

答案 1 :(得分:0)

设置$ _SESSION [&#39;用户名&#39;]的逻辑要求用户名和密码组合在数据库中存在一次。 这可能听起来很愚蠢,但你可以确认是这种情况(即确认你没有创建相同的用户名和密码组合)。 改变逻辑是> 1也会临时解决这个问题。所以你的代码

    if ($count == 1){
    $_SESSION['username'] = $username;
}

应该成为

    if ($count > 1){
    $_SESSION['username'] = $username;
}