我正在尝试使用HTML和PHP验证注册表单,并将数据插入SQL数据库。注册后,我希望我的用户被定向到他们的个人仪表板。除了提交表单时的重定向之外,我可以让一切正常工作。
这是页面开头的 PHP:
<?php
// define variables and set to empty values
$emailErr = $passErr = $confirmErr = $email = $pass = $confirm = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
}
if (empty($_POST["pass"])) {
$passErr = "You must have a password";
} else {
$pass = test_input($_POST["pass"]);
}
if (empty($_POST["confirm"])) {
$confirmErr = "You must confirm your password";
} else {
$confirm = test_input($_POST["confirm"]);
}
if ($pass != $confirm) {
$confirmErr = "Your passwords must match";
}
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$email = test_input($_POST["email"]);
$pass = test_input($_POST["pass"]);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
还有我的 HTML 表单:
<div class="container">
<form class="form-signin" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<h2 class="form-signin-heading">Register below:</h2>
<span class="error"><?php echo $emailErr;?></span>
<input type='email' class="form-control" placeholder="enter email..." name='email'/>
<span class="error"><?php echo $passErr;?></span>
<input type='password' class="form-control" placeholder="enter password" name='pass'/><span class="error"><?php echo $confirmErr;?></span>
<input type='password' class="form-control" placeholder="confirm password" name='confirm'/>
<input type='submit' class="btn btn-success btn-block" name="submit" value="submit">
<h5>Already have an account? <a href="login.html">Log in</a>.</h5>
</form>
</div>
注意使用
<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>
上
<form action="">
参数。使用此功能我无法获得重定向。
我在 HTML 后面包含了我的 SQL 条目,这些条目工作正常,但 header(); 标签似乎不起作用。看这里:
<?php
$email = $_POST['email'];
$pass = md5($_POST['pass']);
// Database connection
require 'database.php';
$sql = "SELECT email FROM users WHERE email = '".$email."'";
$result = $conn->query($sql);
$row_count = $result->num_rows;
if ($row_count == 1) {
// user already exists
header('Location: login.php');
} elseif ($row_count > 1) {
// just to double check multiple entries aren't input into the DB
echo 'There are too many records with that name!';
} else {
// enter user into DB
$sql = "INSERT INTO users (email, pass)
VALUES ('".$email."', '".$pass."')";
if ($conn->query($sql) === TRUE) {
header('Location: dashboard.php');
exit();
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
mysqli_close($conn);
}
?>
知道我做错了什么吗?
嗨,
我已经看到您在使用login的代码中编写代码.php用于从标头标签重定向。在HTML文件中,您正在使用登录名.html用于检查帐户的文件是否存在。请检查您的登录文件的文件扩展名。您可以使用以下链接获取标头函数详细信息:PHP 标头(位置: ...): 强制更改地址栏中的 URL
如果我理解正确,您可以做的是使用以下链接重定向回您的页面:
<form name="form-lignin" method="post" action="this-php-page.php">
然后,您可以在同一页面上创建一个部分,该部分仅在表单已提交且表单将被隐藏时才会显示。该部分也可以为每个用户进行个性化设置。
您可以通过向表单添加隐藏字段来执行此操作。
<input type='hidden' name='submission' value="something"/>
然后你可以添加这个 php :
if ($_POST["submission"]=="something") {
*Dashboard content?*
} else {
*The actual form...*
}
希望这对:)有所帮助
1.您需要确保代码已运行
if ($conn->query($sql) === TRUE) {
echo 'is run!';exit;
header('Location: dashboard.php');
exit();
}
您可以使用 if ($conn->query($sql)) instand if ($conn->query($sql) === TRUE)
2.注意,如果"exit;"代码运行,"mysqli_close($conn);"将无用。
你首先需要把所有的 php 代码放在 html 上面,以便在执行验证脚本后将 html 输出发送到浏览器。
然后
1 捕获表单值
2 验证表单值
3 如果一切都好。然后清理输入并将其插入数据库。
4 如果插入成功,则使用标题('位置:http://example.com')将其重定向到另一个页面;
就是这样