我目前正在从事一个具有注册表格的项目,该表格在理论上应据我所知进行注册,但是我似乎无法确定自己在做错什么。
下面的代码是注册表格的前端部分
<form style="border: 3px solid #ddd; border-radius: 10px; padding: 10px;" class="uk-form-stacked js-register">
<div class="uk-margin">
<label class="uk-form-label" for="form-stacked-text"><b>Username:</b></label>
<div class="uk-form-controls">
<input id="user_name" style="border-radius: 10px;" class="uk-input" name="form-stacked-text" type="text" required='required' placeholder="Insert Username">
</div>
</div>
<div class="uk-margin">
<label class="uk-form-label" for="form-stacked-text"><b>Email:</b></label>
<div class="uk-form-controls">
<input id="user_email" class="uk-input" style="border-radius: 10px;" name="form-stacked-text" type="email" required='required' placeholder="Insert Email">
</div>
</div>
<div class="uk-margin">
<label class="uk-form-label" for="form-stacked-text"><b>Password:</b></label>
<div class="uk-form-controls">
<input id="user_pass" class="uk-input" style="border-radius: 10px;" name="user_pass" type="Password" required='required' placeholder="Insert Password">
</div>
</div>
<div class="uk-margin">
<label class="uk-form-label" for="form-stacked-text"><b>Confirm Password:</b></label>
<div class="uk-form-controls">
<input id="user_pass_check" class="uk-input" style="border-radius: 10px;" name="user_pass_check" type="Password" required='required' placeholder="Confirm Password">
</div>
</div>
<div class="uk-margin">
<div class="uk-form-controls">
<div class="captcha">
<div align="center" class="g-recaptcha" data-sitekey="-" data-callback="recaptcha_callback"></div>
</div>
<br>
</div>
</div>
<div class="uk-margin uk-alert-danger js-error" style='display: none;'></div>
<div class="uk-margin">
<label><center>Already have an account? <a href='/~za1180x/comp1687/login.php'>login now!</a></center></label>
<br>
<center><button id="registerbtn" name="registerbtn" class="uk-button uk-button-default" type="submit"><b>REGISTER</b></button></center>
</div>
</form>
负责将信息提交到数据库的后端代码如下
if($_SERVER['REQUEST_METHOD'] != 'POST')
{
$return = [];
$email = Filter::String( $_POST['user_email']);
$password = Filter::String( $_POST['user_pass']);
$username = Filter::String( $_POST['user_name']);
$user_found = User::Find($email, $username);
mysqli_report ( MYSQLI_REPORT_ALL);
if($user_found) {
// User exists
// We can also check to see if they are able to log in.
$return['error'] = "You already have an account";
$return['is_logged_in'] = false;
} else {
// User does not exist, add them now.
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
// make sure the user CAN be added AND is added.
$addUser = $con->prepare("INSERT INTO users(user_name, user_email, user_pass) VALUES(:user_name, LOWER(:user_email), :user_pass)");
$addUser->bindParam(':username', $username, PDO::PARAM_STR);
$addUser->bindParam(':email', $email, PDO::PARAM_STR);
$addUser->bindParam(':password', $password, PDO::PARAM_STR);
$addUser->execute();
$user_id = $con->lastInsertId();
$_SESSION['user_id'] = (int) $user_id;
$return['is_logged_in'] = true;
$return['redirect'] = 'C:\wamp64\www\index.php?message=welcome';
}
// return the proper information back to Javascript to redirect us.
echo json_encode($return, JSON_PRETTY_PRINT);
} else {
//Kill the script. Redirect the user.
exit('Invalid URL');
}
下面的代码是我创建的用于建立数据库连接的类。
class DB {
protected static $con;
private function __construct(){
try {
self::$con = new PDO( 'mysql:charset=latin1;host=localhost;port=3306;dbname=webenterprise', 'root', ''); //change connection string
self::$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
self::$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
self::$con->setAttribute( PDO::ATTR_PERSISTENT, false );
self::$con->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch (PDOException $e) {
echo "Could not connect to database."; exit;
}
}
public static function getConnection() {
//If this instance has not been started, start it.
if (!self::$con) {
new DB();
}
//Return the writeable db connection
return self::$con;
}
}
答案 0 :(得分:1)
我可以看到其他问题:
method
属性(GET是默认方法); action
属性; name
属性与您正在使用的$_POST
变量不符(可能与id混在一起?); if($_SERVER['REQUEST_METHOD'] != 'POST')
开始后端代码,但之后使用了$_POST
变量:您应该改用if($_SERVER['REQUEST_METHOD'] == 'POST')
。也许我错过了一些。