我刚刚开始学习PHP和Mysql,我试图创建注册表单,它需要5个字符串并发送到我已经在phpMyAdmin中创建的数据库。问题是,在填写表单并按下提交按钮后,数据库中没有任何内容。 即使我将输入字段留空并按提交,也不会显示错误消息。 我通过执行小查询来测试连接:从数据库插入和选择数据,它似乎工作正常。 你能不能给我一些线索,这段代码可能有什么问题? 我现在好好与它斗争好几天了。
<?php include_once ('connect.php');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Signup Page</title>
</head>
<body>
<?php
if(isset($_POST['submit_signup'])) {
$user_firstname = $db->real_escape_string(htmlentities($_POST['user_firstname']));
$user_lastname = $db->real_escape_string(htmlentities($_POST['user_lastname']));
$user_username = $db->real_escape_string(htmlentities($_POST['user_username']));
$user_email = $db->real_escape_string(htmlentities($_POST['user_email']));
$user_password = sha1($_POST['user_password']);
if ($user_firstname == '' || $user_lastname == '' || $user_email == '' || $user_username == '' || $user_password == '') {
$error = "Please fill in all fields!";
} else {
$duplicates_query = $db->query("SELECT user_username FROM sd_users WHERE user_username = '$user_username'");
$duplicate = $duplicates_query->num_rows;
if($duplicate == 1) {
$error = "Username:" . $user_username . "already exist, please pick something else!";
} else {
$result = $db->query("INSERT INTO `sd_users` (`idusers`, `user_firstname`, `user_lname`, `user_username`, `user_password`, `user_email`)
VALUES (NULL, '$user_firstname', '$user_lastname', '$user_username', '$user_password', '$user_email', NOW())");
}
}
}
?>
<div class="signup-form">
<form action="signup.php" method="post">
<input type="text" name="user_firstname" placeholder="First Name">
<input type="text" name="user_lastname" placeholder="Second Name">
<input type="text" name="user_email" placeholder="Email Address">
<input type="text" name="user_username" placeholder="Username">
<input type="password" name="user_password" placeholder="Password">
<button type="submit" name="submit_signup">Sign Up</button>
</div>
</body>
</html>
这是我的connect.php文件:
<?php
$db = new mysqli('localhost', 'root', '', 'supportdesk');
if($db->connect_errno) {
echo $db->connect_error;
}
?>