所以我一直在寻找论坛,但没有找到任何帮助我的东西! 这是完整的代码。错误发生在$ result = $ conn-> query($ query); 我是php的新手,所以对我很轻松,哈哈。
<?php
include 'Register.html';
if(isset($_POST['Submit']))
{
SignUp();
}
global $conn;
$servername = "localhost";
$username1 = "root";
$password1 = "";
$dbname = "fiesta_db";
// Create connection
$conn = new mysqli($servername, $username1, $password1, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
return $conn;
}
function NewUser()
{
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$register = "INSERT INTO website (username, email, password)
VALUES ($username, $email, $password)";
if ($conn->query($register) == TRUE) {
echo "Successfully registered.";
}
else {
echo "Wrong syntax";
}
}
function SignUp()
{
if(!empty($_POST['username']))
{
global $conn;
$query = "SELECT * FROM website WHERE username ='$_POST[username]' AND email ='$_POST[email]'; ";
$result = $conn->query($query);
if ($result->num_rows != 0)
{
NewUser();
}
else {
echo "Username or Email already exist!";
}
}
$conn->close();
}
?>
希望你们能提供帮助(完全不只是想增加更多细节)
<!DOCTYPE HTML>
<html>
<head>
<title>Sign-Up</title>
</head>
<body id="body-color">
<div id="Sign-Up">
<form action='register_do.php' method='post'
accept-charset='UTF-8'>
<fieldset >
<legend>Register</legend>
<input type='hidden' name='submitted' id='submitted' value='1'/>
<label for='username' >Username:</label>
<input type='text' name='username' id='username' maxlength="50" />
<label for='email' >Email Address:</label>
<input type='text' name='email' id='email' maxlength="50" />
<label for='password' >Password:</label>
<input type='password' name='password' id='password' maxlength="50" />
<input type='submit' name='Submit' value='Submit' />
</fieldset>
</form>
</div>
</body>
</html>
这是注册页面,我没有任何代码可以显示这就是一切。 凸块
答案 0 :(得分:0)
在您分配给主代码中的SignUp()
之前,您正在调用$conn
。移动该代码。
$servername = "localhost";
$username1 = "root";
$password1 = "";
$dbname = "fiesta_db";
// Create connection
$conn = new mysqli($servername, $username1, $password1, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
return $conn;
}
if(isset($_POST['Submit']))
{
SignUp();
}
您也不需要主代码中的global $conn;
语句。在函数外部分配的变量是自动全局的,您只需要函数内部的语句来允许它访问全局变量。
您在NewUser()
中的查询中也缺少引号:
$register = "INSERT INTO website (username, email, password)
VALUES ('$username', '$email', '$password')";
在SignUp()
中,您使用了错误的条件。如果用户名可用,SELECT
查询将返回0
行,因此应该是:
if ($result->num_rows == 0) {
NewUser();
} else {
echo "Username or Email already exist!";
}