gpt4 book ai didi

PHP 代码没有提交到 mysql 数据库

转载 作者:行者123 更新时间:2023-11-29 01:54:44 26 4
gpt4 key购买 nike

我正在尝试为网站创建登录名。到目前为止,每当我发布到我的数据库时都不会显示提交的信息,唯一发布的是散列密码。

<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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com