Alright, so I’m pretty new to PHP. I’ve done a few PHP tutorials so I decided to make a site by myself this time, only using documentation and examples etc. I wasn’t really sure what jQuery was, I didn’t even know I needed it for a validation.
After some trial and error with some code of a website. I finally got it displaying that a username is not available when you type something in. The problem is, it even says it for usernames which I know are in the Database. I’m currently at a loss on how to fix this.
- Project setup (for files related)
- register.php
- registration_form.php
- check_username.php
- confirm-registration.php
- connect.php
I’ll add the contents of the files below, and yes. I know, that I should be using mysqli, I’ll most likely get around to that soon. Any help with this would be greatly appreciated.
Register.php
[php]<?php
include ‘…/connect.php’;
if (isset($_SESSION['signed_in']) && $_SESSION['signed_in'] == true ) {
header('Location : http://localhost/blog');
} else {
echo include 'registration_form.php';
require 'check_username.php';
}
?>
[/php]
registration_form.php
[php]
Register - Create a new Account
[/php]
Check_username.php
[php]<?php
$username = mysql_real_escape_string($_POST[‘username’]);
//mysql query to select field username if it's equal to the username that we check '
$result = mysql_query('SELECT user_name from users where user_name = "'. $username .'"');
//if number of rows fields is bigger them 0 that means it's NOT available '
if(mysql_num_rows($result) > 0){
//and we send 0 to the ajax request
echo 0;
} else {
//else if it's not bigger then 0, then it's available '
//and we send 1 to the ajax request
echo 1;
}
?>
[/php]
Confirm-registration.php
[php]<?php
include ‘…/connect.php’;
$value = mysql_real_escape_string($_POST['username']);
$value2 = mysql_real_escape_string($_POST['create-password']);
$value3 = mysql_real_escape_string($_POST['create-email']);
$value5 = mysql_real_escape_string($_POST['confirm-password']);
if (($value == '') || ($value2 == '') || ($value3 == '')) {
die('<p>Registration Failed - Fields not working -: The fields where not correclty filled out, please try again.');
}
if (($value2 != $value5)) {
die('<p>The Passwords entered do not match, please <a href="register.php">enter them again.</a>');
}
$sql = "INSERT INTO users(user_name, user_pass, user_email, user_date, user_group) VALUES('" . strip_tags($value) . "', '" . sha1($value2) . "', '" . strip_tags($value3) . "', NOW(), 2)";
$result = mysql_query($sql);
if (!$result) {
die('Registration failed - Database Error - : ' . mysql_error());
}
echo '<p><b>Registration Successful :</b> your account has been successfully created, click <a href="../index.php">here</a> to return to the homepage</p>';
include 'available.php';
?>
[/php]
Connect.php
[php]<?php
define(‘DB_NAME’, ‘blog’);
define(‘DB_USER’, ‘root’);
define(‘DB_PASS’, ‘’);
define(‘DB_HOST’, ‘localhost’);
$connect = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die(mysql_error());
$select_db = mysql_select_db(DB_NAME, $connect) or die(mysql_error());
?>
[/php]