簡體   English   中英

PHP注冊表格未保存

[英]PHP Registration Form not saving

我有一個HTML注冊表格,該表格允許新用戶注冊到該站點。

<form action="register.php" method="POST" class="register-form">
    <h1>Create Account</h1>
    <label>
        <span>First Name :</span>
        <input id="firstname" type="text" name="firstname" placeholder="Your First Name" autocomplete="off" required/>
    </label>
    <label>
        <span>Surname :</span>
        <input id="surname" type="text" name="surname" placeholder="Your Surname" autocomplete="off" required/>
    </label>
    <label>
        <span>Username :</span>
        <input id="username" type="text" name="username" placeholder="Your Chosen Username" autocomplete="off" required/>
    </label>
    <label>
        <span>Email :</span>
        <input id="email" type="email" name="email" placeholder="Your Email Address" autocomplete="off" required/>
    </label>
    <label>
        <span>Password :</span>
        <input id="password" type="password" name="password" placeholder="Your Chosen Password" autocomplete="off" required/>
    </label>
    <hr>
    <input name="action" type="hidden" value="signup" />
    <input type="submit" class="btn register btn-success btn-lg" name="submit" value="Register">

</form>

哪個去register.php:

<?php
$connection = mysql_connect('localhost', 'root', 'password');
if (!$connection) {
    die("Database Connection Failed" . mysql_error());
}

$select_db = mysql_select_db('gmaps1');
if (!$select_db) {
    die("Database Selection Failed" . mysql_error());
}

// If the values are posted, insert them into the database.
if (isset($_POST['username']) && isset($_POST['password'])) {
    $firstname = $_POST['firstname'];
    $surname = $_POST['surname'];
    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = $_POST['password'];


    $query = "INSERT INTO `users` (firstname, surname, username, password, email) VALUES ('$firstname', '$surname', '$username', '$password', '$email')";
    $result = mysql_query($query);
    if ($result) {
        header("Location: thank_you.html");
    } else {
        echo 'User Not Created';
    }
}
?>

但是,當我單擊“注冊”按鈕時,它不會保存數據並返回“未創建用戶”。 使用MySQLi而不是使用MySQLi會更好,還是有更好的方法呢?

錯誤檢查解決了我的問題-“字段'active'沒有默認值”

表“用戶”中有一個非活動字段。 我擺脫了它,它工作正常。 它一定是錯誤添加的。

  • 您不會收到“數據庫連接失敗”的消息,因此您已成功連接數據庫
  • 最好使用MySQLi或PDO(我的選擇)
  • 至少使用mysql_real_escape ,也可以使用trim()但這僅是安全性的起點
  • 檢查是否所有數據庫字段的命名方式都與您在插入語句中處理它們的方式完全相同
  • 做一個echo $query ,打開phpMyAdmin(例如),然后將輸出復制粘貼到SQL字段中並發送->您可能會得到一個MySQL錯誤,可以分析
  • 最好存儲散列的密碼。 嘗試插入MD5($password) (有更好的選擇!),並在登錄時進行比較:

    if(MD5($ inputPassword)== $ passwordhashFromDatabase){}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM