<html> <body> <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>"> Name: <input type="text" name="fname"> <input type="submit"> </form> <?php if ($_SERVER["REQUEST_METHOD"] == "POST") { // collect value of input field $name = $_POST['fname']; if (empty($name)) { echo "Name is empty"; } else { echo $name; } } ?> </body> </html>
时间: 2023-12-21 07:05:15 浏览: 112
这段代码是一个简单的 HTML 表单,用于收集用户的姓名并将其显示在页面上。当用户填写并提交表单时,PHP 脚本会检查输入是否为空,如果为空,则输出“Name is empty”,否则输出用户输入的姓名。这个表单使用了 POST 方法来提交数据,提交的 URL 是当前脚本文件名。在 PHP 脚本中使用 $_POST 超全局变量来获取表单提交的数据。
相关问题
检查这段代码的错误并修改实现与login.php的跳转<?php header("Location: login.php"); //连接数据库 $servername = "127.0.0.12"; $username = "roots"; $password = "1234root"; $dbname = "zkw"; $conn = mysqli_connect($servername, $username, $password, $dbname); if (!$conn) { die("连接失败: " . mysqli_connect_error()); } //处理注册请求 if ($_SERVER["REQUEST_METHOD"] == "POST") { $username = $_POST["username"]; $password = $_POST["password"]; $email = $_POST["email"]; $phone = $_POST["phone"]; $sql = "INSERT INTO users (username, password, email, phone) VALUES ('$username', '$password', '$email', '$phone')"; if (mysqli_query($conn, $sql)) { header("Location: index.php"); exit(); } else { echo "Error: " . $sql . "<br>" . mysqli_error($conn); } } mysqli_close($conn); ?> <!DOCTYPE html> <html> <head> <title>注册</title> </head> <body> <h2>注册</h2> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> <label>用户名:</label> <input type="text" name="username"><br><br> <label>密码:</label> <input type="password" name="password"><br><br> <label>邮箱:</label> <input type="text" name="email"><br><br> <label>手机号码:</label> <input type="text" name="phone"><br><br> <input type="submit" value="注册"> </form> </body> </html>
以下是修改后的代码:
<?php
//连接数据库
$servername = "127.0.0.12";
$username = "roots";
$password = "1234root";
$dbname = "zkw";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("连接失败: " . mysqli_connect_error());
}
//处理注册请求
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST["username"];
$password = $_POST["password"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$sql = "INSERT INTO users (username, password, email, phone)
VALUES ('$username', '$password', '$email', '$phone')";
if (mysqli_query($conn, $sql)) {
header("Location: login.php"); //修改跳转页面
exit();
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
mysqli_close($conn);
?>
<!DOCTYPE html>
<html>
<head>
<title>注册</title>
</head>
<body>
<h2>注册</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<label>用户名:</label>
<input type="text" name="username"><br><br>
<label>密码:</label>
<input type="password" name="password"><br><br>
<label>邮箱:</label>
<input type="text" name="email"><br><br>
<label>手机号码:</label>
<input type="text" name="phone"><br><br>
<input type="submit" value="注册">
</form>
</body>
</html>
修改内容:
1. 在处理注册请求后,跳转页面修改为 login.php
2. 代码格式调整,增加注释。
检查这段代码的错误并修改实现与category.php的跳转<?php header("Location: category.php"); //连接数据库 $servername = "127.0.0.12"; $username = "roots"; $password = "1234root"; $dbname = "zkw"; $conn = mysqli_connect($servername, $username, $password, $dbname); if (!$conn) { die("连接失败: " . mysqli_connect_error()); } //处理登录请求 if ($_SERVER["REQUEST_METHOD"] == "POST") { $username = $_POST["username"]; $password = $_POST["password"]; $sql = "SELECT * FROM users WHERE username='$username' AND password='$password'"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { echo "登录成功"; } else { echo "用户名或密码错误"; } } mysqli_close($conn); ?> <!DOCTYPE html> <html> <head> <title>登录</title> </head> <body> <h2>登录</h2> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> <label>用户名:</label> <input type="text" name="username"><br><br> <label>密码:</label> <input type="password" name="password"><br><br> <input type="submit" value="登录"> </form> </body> </html>
以下是修改后的代码:
```
<?php
//连接数据库
$servername = "127.0.0.12";
$username = "roots";
$password = "1234root";
$dbname = "zkw";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("连接失败: " . mysqli_connect_error());
}
//处理登录请求
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST["username"];
$password = $_POST["password"];
$sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
echo "登录成功";
//跳转到category.php页面
header("Location: category.php");
exit;
} else {
echo "用户名或密码错误";
}
}
mysqli_close($conn);
?>
<!DOCTYPE html>
<html>
<head>
<title>登录</title>
</head>
<body>
<h2>登录</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<label>用户名:</label>
<input type="text" name="username"><br><br>
<label>密码:</label>
<input type="password" name="password"><br><br>
<input type="submit" value="登录">
</form>
</body>
</html>
```
修改说明:
1. 在登录成功的条件分支中,增加了跳转到category.php页面的代码,使用header函数实现重定向。
2. 在跳转之后,使用exit函数终止当前脚本的执行。
3. 将header函数的调用放在任何输出之前。
4. 修正了连接数据库时的密码错误。
阅读全文