当我单击index.php页面上的提交按钮时,绝对没有发生任何让我感到惊讶的事情,因为我正在遵循教程并且代码完全相同。我检查了好几遍,但无济于事。我注意到的一件事是,当我进入页面 http://localhost:8888/phplessons/signup.php 它实际上在我的数据库中创建了一行,但是该行只有id,它是主键和自动递增的字段,而第一个,最后一个,用户名和密码字段是空的。我只能假设我的按钮有问题?
索引.php代码:
<?php
include 'db.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Playground</title>
<meta name="viewport" content="width=device-width, initial-scale=1 shrink-to-fit=no">
</head>
<body>
<form action="signup.php" method="POST">
<input type="text" name="first" placeholder="First Name"></form>
<input type="text" name="last" placeholder="Last Name"></form>
<input type="text" name="username" placeholder="Username"></form>
<input type="password" name="password" placeholder="Password"></form>
<button type="submit">Sign Up</button>
</form>
</body>
</html>
注册.php代码:
<?php
include 'db.php';
$first = $_POST['first'];
$last = $_POST['last'];
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "INSERT INTO user (id, first, last, username, password)
VALUES (NULL, '$first','$last','$username','$password')";
$result = mysqli_query($conn, $sql);
?>
更正表单 html。
<form action="signup.php" method="POST">
<input type="text" name="first" placeholder="First Name">
<input type="text" name="last" placeholder="Last Name">
<input type="text" name="username" placeholder="Username">
<input type="password" name="password" placeholder="Password">
<button type="submit">Sign Up</button>
</form>
<?php
include 'db.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Playground</title>
<meta name="viewport" content="width=device-width, initial-scale=1 shrink-to-fit=no">
</head>
<body>
<form action="signup.php" method="POST">
<input type="text" name="first" placeholder="First Name">
<input type="text" name="last" placeholder="Last Name">
<input type="text" name="username" placeholder="Username">
<input type="password" name="password" placeholder="Password">
<button type="submit">Sign Up</button>
</form>
</body>
</html>
正确结束表单标记