我正在尝试为网站创建登录信息。到目前为止,每当我发布到数据库时都不会显示提交的信息,唯一发布的内容是哈希密码。
<form method="post" action="submit.php">
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" id="email">
</div>
<div class="form-group">
<label for="username">Username:</label>
<input type="text" class="form-control" id="username">
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" class="form-control" id="password">
</div>
<div class="form-group">
<label for="pwd2">Re-Password</label>
<input type="password" class="form-control" id="pwd2">
</div>
<div class="form-group">
<input type="submit" class="form-control" id="submit">
</div>
</form>
提交到此 php block
<?php
$servername = "localhost";
$username = "root";
$password = "Password";
$dbname = "DBNAME";
$email = NULL;
$user = NULL;
$pass1 = NULL;
if (isset($_POST['email'])){
$email = $_POST['email'];
}
if (isset($_POST['username'])){
$user = $_POST['username'];
}
if (isset($_POST['password'])){
$pass1 = $_POST['password'];
}
$hash = password_hash($pass1, PASSWORD_BCRYPT);
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO Users (email, username, password )
VALUES ('$email', '$user', '$hash')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
最佳答案
您的表单字段缺少名称属性。如果没有它们,则不会向您的脚本发送任何值。通过执行 var_export($_POST)
可以轻松测试这一点。
<form method="post" action="submit.php">
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" name="email" id="email">
</div>
<div class="form-group">
<label for="username">Username:</label>
<input type="text" class="form-control" name="username" id="username">
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" class="form-control" name="password" id="password">
</div>
<div class="form-group">
<label for="pwd2">Re-Password</label>
<input type="password" class="form-control" name="pwd2" id="pwd2">
</div>
<div class="form-group">
<input type="submit" class="form-control" id="submit">
</div>
</form>
仅供引用,您对SQL injections持开放态度。
关于PHP 代码未提交到 mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32298615/