<?php
// Connect to the MySQL
$link = mysql_connect('localhost', 'spowpow1_Sean', '<password>');
if (!$link)
{
die('Not connected : ' . mysql_error());
}
// select test as the current db
$db_selected = mysql_select_db('spowpow1_test', $link);
if (!$db_selected)
{
die ("Database not selected : " . mysql_error());
}
/**
* This function can be used to check the sanity of variables
*
* @access private
*
* @param string $type The type of variable can be bool, float, numeric, string, array, or object
* @param string $string The variable name you would like to check
* @param string $length The maximum length of the variable
*
* return bool
*/
if (mysql_num_rows(mysql_query("SELECT * FROM users2 WHERE userName = '" . $_POST['userName'] . "'",$link)) == 1) {
echo("Username Unavailable");
exit();
}
else{
function sanityCheck($string, $type, $length){
// assign the type
$type = 'is_'.$type;
if(!$type($string))
{
return FALSE;
}
// now we see if there is anything in the string
elseif(empty($string))
{
return FALSE;
}
// then we check how long the string is
elseif(strlen($string) > $length)
{
return FALSE;
}
else
{
// if all is well, we return TRUE
return TRUE;
}
}
/**
* This function if the $_POST vars are set
*
* @access private
*
* return bool
*/
function checkSet(){
return isset($_POST['userName'], $_POST['userAddress'], $_POST['userCity'], $_POST['userZip'], $_POST['userEmail']);
}
/**
* This function checks if an email address in a valid format
*
* @access private
*
* @param string $email The email address to check
*
* return bool
*/
function checkEmail($email){
return preg_match('/^\S+@[\w\d.-]{2,}\.[\w]{2,6}$/iU', $email) ? TRUE : FALSE;
}
if(checkSet() != FALSE)
{
if(empty($_POST['userName'])==FALSE && sanityCheck($_POST['userName'], 'string', 25) != FALSE)
{
$userName = $_POST['userName'];
}
else
{
echo("Username is not set");
exit();
}
if(sanityCheck($_POST['userPass'], 'string', 100) != FALSE)
{
$userAddress = $_POST['userPass'];
}
else
{
echo("Please try a different password");
}
if(sanityCheck($_POST['userEmail'], 'string', 100) != FALSE && checkEmail($_POST['userEmail']) != FALSE)
{
$userEmail = $_POST['userEmail'];
}
else
{
echo("Invalid Email Address Supplied");
exit();
}
mysql_real_escape_string($userName);
mysql_real_escape_string($userPass);
mysql_real_escape_string($userEmail);
$query = "INSERT INTO users2 (userName, userPass, userEmail)
VALUES( '$userName', '$userPass', '$userEmail')";
if(!mysql_query($query))
{
echo 'Query failed '.mysql_error();
exit();
}
else
{
setcookie("userName",$userName,time()+86400);
echo("Account " . $_COOKIE['userName'] . "successfully created!");
}
}
else
{
echo("Please fill out ALL fields.");
}
}
?>
Sorry it's s long. If there was a certain section I knew was going wrong I could have trimmed it down, but I have no idea what's going wrong with it.
Every time I run it, it says "Please fill out ALL fields" instead of "Account <userName> successfully created!".
I'm going to go to bed because I've been up all night making and figuring this code out :p . It's 5AM right now and I need some sleep.
Any help would be EXTREMELY appreciated.
THANK YOU (I love this community 😃)!