注册成功但数据库中没有数据

时间:2016-11-01 14:03:29

标签: php mysql

当我使用代码插入数据时,我收到了您已成功注册的消息,即数据已插入表中,但我无法在本地数据库中找到数据。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Registration</title>
    <link rel="stylesheet" href="css/style.css" />
</head>
<body>
    <?php
    require('db.php');

    if (isset($_REQUEST['username'])) {
        $username = "username";
        $email = "email";
        $password = "password";
        $trn_date = date("Y-m-d H:i:s");
        $query = "INSERT into `users` (username, password, email, trn_date)
        VALUES ('$username', '".md5($password)."', '$email', '$trn_date')";
        $result = mysqli_query($con, $query);
        if($result) {
            echo "<div class='form'>
            <h3>You are registered successfully.</h3>
            <br/>Click here to <a href='login.php'>Login</a></div>";
        }
    } else {
        ?>
        <div class="form">
            <h1>Registration</h1>
            <form name="registration" action="" method="post">
                <input type="text" name="username" placeholder="Username" required />
                <input type="email" name="email" placeholder="Email" required />
                <input type="password" name="password" placeholder="Password" required />
                <input type="submit" name="submit" value="Register" />
            </form>
        </div>
        <?php }
        ?>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

几个问题:

  1. action中的<form>标记为空。它可以工作,但你应该指定脚本名称。
  2. 您为$username$email$password分配了错误的值。他们应该从$_REQUEST
  3. 获得价值 发生错误时,
  4. mysqli_query会返回TRUE。因此,您会看到显示成功的消息。
  5. 检查MySQL错误,然后找到问题的解决方案。

答案 1 :(得分:0)

您必须先了解有关php的更多信息。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Registration</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<?php
require('db.php');

if (isset($_POST['username'])){
$username= $_POST["username"];
$email = $_POST["email"];
$password = $_POST["password"];
$trn_date = date("Y-m-d H:i:s");
$query = "INSERT into `users` (username, password, email, trn_date)
VALUES ('$username', '".md5($password)."', '$email', '$trn_date')";
    $result = mysqli_query($con,$query);


    if($result){
        echo "<div class='form'>
<h3>You are registered successfully.</h3>
<br/>Click here to <a href='login.php'>Login</a></div>";
    }
}else{
?>
<div class="form">
<h1>Registration</h1>
<form name="registration" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="username" placeholder="Username" required />
<input type="email" name="email" placeholder="Email" required />
<input type="password" name="password" placeholder="Password" required />
<input type="submit" name="submit" value="Register" />
</form>
</div>
<?php } ?>
</body>
</html>