mysql中的PHP注册表

标签 php mysql

我在使用 php 和 mysq 构建注册表单时遇到问题。 我有两个文件,contactus.php 和 home.php。 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>

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>

在我点击提交之后,它应该会转到 home.php。但是,它没有成功。我不确定我的问题在哪里。

这是 mysql 'users' 表 enter image description here

最佳答案

使用 PDO,这个(下面)应该可以完成工作,它可以安全地防止 sql 注入(inject)(检查准备好的请求了解更多)。 您不能使用 MD5,它已被弃用,请尝试 sha1() 或 sha256()。 编辑:您还有 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";
     }
 }
?> 

关于mysql中的PHP注册表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41787527/

相关文章:

php - MySQL information_schema.tables 没有创建新条目

mysql - Play框架-通过URL中的参数将数据保存到数据库

php - 在页面中使用变量并在另一个页面中使用该变量

php - Codeigniter:我想在我的用户创建用户名时为他们创建用户 URL

php - 如何在MySQL中查找不同组中的重复对

php - 我该如何解决这个 php/mysql 错误?

PHP 显示从给定时间到特定事件的小时数

php - 两个代码理论上是相同的,但实际上却是不同的……怎么可能

php - 如何使用 ajax 将数组从 php 返回到 javascript

mysql - select in where子句,获取当前父select列