0% found this document useful (0 votes)
37 views1 page

Dbfunctions

The document is a PHP script that handles user registration for an admin panel. It checks if the posted username and password are valid, ensures the password confirmation matches, and verifies that the username is not already taken. If all conditions are met, it hashes the password and inserts the new user into the database, redirecting to a login page upon success.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views1 page

Dbfunctions

The document is a PHP script that handles user registration for an admin panel. It checks if the posted username and password are valid, ensures the password confirmation matches, and verifies that the username is not already taken. If all conditions are met, it hashes the password and inserts the new user into the database, redirecting to a login page upon success.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

<?

php
include("db.php");
include("functions.php");

if($_SERVER['REQUEST_METHOD'] == "POST")
{
//something was posted
$username = $_POST['username'];
$password = $_POST['password'];
$password_conf = $_POST['password_conf'];
// CHECK IF CONFIRMED PASSWORD IS THE SAME AS PASSWORD
if ($password == $password_conf) {
// CHECK IF INPUT IS NOT EMPTY AND USERNAME IS NOT NUMERIC
if(!empty($username) && !empty($password) && !is_numeric($username))
{
// $var = "SELECT count(*) FROM admin";
// $query = $con2-> query($var);

// CHECK IF USERNAME IS ALREADY TAKEN


$queryString = "SELECT count(*) as countusers FROM admin WHERE username =
'$username'";
$result = mysqli_query($con, $queryString);
while($row = mysqli_fetch_assoc($result))
{
//
if($row["countusers"] != 0)
{
$_SESSION['userexists'] = "Username Already Exists!";
header("location: register.php");
exit;
}
else {// ACTUAL PROCESS IF USER ARE NOT REGISTERED
$user_id = rand();

$password = password_hash($password, PASSWORD_DEFAULT);


$sql = "INSERT INTO admin (userid, username, password) VALUES ( '$user_id',
'$username', '$password')";
if (mysqli_query($con, $sql)) {
$_SESSION['regsuccess'] = "Admin Added!";
header("location: login.php");
exit;
}
else {
echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
}
}

You might also like