我有一个用PDO编写的注册脚本。注册后,用户应该被重定向,但相反,他们会保持在同一页面上。
这是我的数据库连接:
$db_username = "username";
$db_password = "password";
$con = new PDO("mysql:host=localhost;dbname=database", $db_username, $db_password);
这是我的注册脚本:
<?php
if(isset($_POST['submit'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
if (empty($username)) {
$errorusername = 'Please enter a username';
}else{
if (empty($password)) {
$errorpassword = 'Please enter a password';
}else{
if (empty($email)) {
$erroremail = 'Please enter an email.';
}else{
$password = md5($password);
$checkusername = $stmt = $con->prepare("SELECT * FROM users WHERE username=':username'");
if (mysqli_num_rows($checkusername) == 1)
{
echo "Username already exists.";
}else{
$status = 'Hello there!';
$about = 'Hello!';
$stmt = $con->prepare("INSERT INTO users (username, password, email, status, about) VALUES (:username, :password, :email, :status, :about)");
$stmt->bindParam(':username', $_POST['username']);
$stmt->bindParam(':password', md5($_POST['password']));
$stmt->bindParam(':email', $_POST['email']);
$stmt->bindParam(':status', $status);
$stmt->bindParam(':about', $about);
$stmt->execute();
header('Location: index.php');
}
}
}
}
}
?>
<form method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="email" name="email">
<?php echo $errorusername = !empty($errorusername) ? $errorusername : ''; ?>
<?php echo $errorpassword = !empty($errorpassword) ? $errorpassword : ''; ?>
<?php echo $erroremail = !empty($erroremail) ? $erroremail : ''; ?>
<input type="submit" name="submit">
</form>
另外,我只是将我的代码从MYSQLI切换到PDO - 是否有任何明显的错误,因为我对它没有充分的经验。
答案 0 :(得分:0)
使用您的选择语句,如下所示 -
192.168.71.129 gitlabhost