Hello, I got a problem with my registering system:
<?php
if ($user -> LoggedIn())
{
header('Location: home');
echo 'No access.';
die();
}
if (isset($_POST['reg']))
{
$username = $_POST['username'];
$password = $_POST['password'];
$rpassword = $_POST['rpassword'];
$email = $_POST['email'];
$errors = array();
$checkUsername = $odb -> prepare("SELECT COUNT(*) FROM `users` WHERE `username` = :username");
$checkUsername -> execute(array(':username' => $username));
$countUsername = $checkUsername -> fetchColumn(0);
if ($checkUsername > 0)
{
$errors[] = 'Username is already taken.';
}
if (!ctype_alnum($username) || strlen($username) < 4 || strlen($username) > 15)
{
$errors[] = 'Username Must Be Alphanumberic And 4-15 characters in length';
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$errors[] = 'Email is invalid';
}
if (empty($username) || empty($password) || empty($rpassword) || empty($email))
{
$errors[] = 'Please fill in all fields';
}
if ($password != $rpassword)
{
$errors[] = 'Passwords do not match';
}
if (empty($errors))
{
$insertUser = $odb -> prepare("INSERT INTO `users` VALUES(NULL, :username, :password, :email, 0, 0, 0, 0)");
$insertUser -> execute(array(':username' => $username, ':password' => SHA1($password), ':email' => $email));
echo '<div class="alert alert-block alert-success fade in"><button data-dismiss="alert" class="close" type="button">×</button><h4 class="alert-heading">SUCCESS</h4> <p>You have successfully registered, now you can login.</p></div><meta http-equiv="refresh" content="2;url=login">';
}
else
{
echo '<div class="alert alert-block alert-error fade in"><button data-dismiss="alert" class="close" type="button">×</button><h4 class="alert-heading">ERROR</h4> <p>';
foreach($errors as $error)
{
echo '-'.$error.'<br>';
}
echo '</p></div>';
}
}
?>
I always get the error that username is taken, as well as these errors:
Notice: Object of class PDOStatement could not be converted to int in C:\xampp\htdocs\register.php on line 62
Note: Line 62 is: if ($checkUsername > 0)
Copyright © 2024 Q2A.ES - All rights reserved.
Answers & Comments
Verified answer
Try
$checkUsername = $odb -> prepare("SELECT COUNT(*) FROM users WHERE username = '" . $username. "'");
Have fun.
i got here across phpmyadmin stressful and went back to the mysql command line interface. there is also some techniques noted as MySQL Workbench and MySQL Administrator that are ok you opt for to be able to make a right away connection to the mysql server a port 3306 (except you replaced it). the in effortless words component which could be preventing you from using phpmyadmin or the different device is you do not have the mysql permissions set up acceptable (including installation an admin element consumer).
On line 62, did you mean:
if ($countUsername > 0)
instead of
if ($checkUsername > 0)