简体   繁体   English

MySQL中的PHP注册表格

[英]PHP registration form in mysql

I am having problem in when I building the registration form with php and mysq. 我在用php和mysq构建注册表时遇到问题。 I have two files, contactus.php and home.php. 我有两个文件,contactus.php和home.php。 The code for contactus.php is below: contactus.php的代码如下:

    <?php
     session_start();
     $db = mysqli_connect("localhost", "wadca2user", "password123", "p1514432db");     
     if(isset($_POST['register_btn'])){
         session_start();         
         $username  = mysql_real_escape_string($_POST['username']);
         $email     = mysql_real_escape_string($_POST['email']);
         $password  = mysql_real_escape_string($_POST['password']);
         $password2 = mysql_real_escape_string($_POST['password2']);

         if($password == $password2){
             $password = md5($password); // stored before
             $sql      = "INSERT INTO users(username,email,password) Values('$username','$email','$password')";
             mysqli_query($db, $sql);
             $_SESSION['message'] = "Your are now logged in";   
             $_SESSION['username'] = $username;
             header("location: home.php");
         }else{
             $_SESSION['message'] = "The two passwords do not match";
         }
     }
?>
<!DOCTYPE html>
<html>
    <head>
        <link href="css/maincss.css" rel="stylesheet" type="text/css"/>
    </head>
    <body>
        <div class="header">
            <h1>Register</h1>
        </div>

        <form method="post" action="contactus.php">
            <table>
                <tr>
                    <td>Username:</td>
                    <td><input type="text" name="username" class="textInput"></td>
                </tr>
                <tr>
                    <td>Email:</td>
                    <td><input type="email" name="email" class="textInput"></td>
                </tr>
                <tr>
                    <td>Password:</td>
                    <td><input type="password" name="password" class="textInput"></td>
                </tr>
                <tr>
                    <td>Password again:</td>
                    <td><input type="password" name="password2" class="textInput"></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="submit" name="register_btn" value="Register"></td>
                </tr>
            </table>
        </form>
    </body>
</html>

The code for home.php is below: 以下是home.php的代码:

    <?php
     session_start();
?>
<!DOCTYPE html>
<html>
    <head>
        <link href="css/maincss.css" rel="stylesheet" type="text/css"/>
    </head>
    <body>
        <div class="header">
            <h1>Register</h1>
        </div>
        <h1>Home</h1>
        <div>
            <h3>Welcome<?php echo $_SESSION['username']; ?></h3>
        </div>
    </body>
</html>

After I click on submit, it is supposed to go to home.php. 单击提交后,应该转到home.php。 However, it does not succeed. 但是,它不会成功。 I am not sure where is my problem. 我不确定我的问题在哪里。

Here is mysql 'users' table 这是mysql的“用户”表 在此处输入图片说明

Use PDO, this (below) should do the job, it's secure against sql injection (check prepared request for more). 使用PDO,这(下面)应该可以完成这项工作,它可以防止sql注入(检查准备好的请求以获取更多信息)。 You must not use MD5, it's deprecated, try sha1() or sha256(). 请勿使用已过时的MD5,请尝试使用sha1()或sha256()。 Edit : you also have password_hash() which is quite nice. 编辑:您也有password_hash(),这是相当不错的。

<?php
session_start();
$servername = "localhost";
$username = "wadca2user";
$password = "password123";
$conn = null;
try {
    $conn = new PDO("mysql:host=$servername;dbname=p1514432db", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully";
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }
if(isset($_POST['register_btn']) && !is_null($conn)){
     $username  = $_POST['username'];
     $email     = $_POST['email'];
     $password  = $_POST['password'];
     $password2 = $_POST['password2'];

     if($password === $password2){
         $password = md5($password); // stored before
         $request = $conn->prepare("INSERT INTO users (username,email,password) VALUES (:username, :email, :password)");
         $request->bindParam(':username', $username);
         $request->bindParam(':email', $email);
         $request->bindParam(':password', $password);
         $request->execute();
         $_SESSION['message'] = "Your are now logged in";   
         $_SESSION['username'] = $username;
         header("location: home.php");
     }else{
         $_SESSION['message'] = "The two passwords do not match";
     }
 }
?> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM