My script is working as it should but when it gets to the point where it is supposed to query the DB, I a connection error. Here is the script:
<?php
//this script registers a new user.
require_once 'dbConnect.php';
require_once 'functions.php';
$error = '';
if ($_POST['register'] == 1) { //if the form has been submitted
$userName = $_POST['user'];
$pass = $_POST['pass'];
$rePass = $_POST['rePass'];
$query = 'SELECT * FROM users WHERE user_id="$userName"';
$insertUser = 'INSERT INTO users VALUES ($userName, $pass)';
if (empty($userName) || empty($pass) || empty($rePass)) { //are any of the fields empty
$error = "All fields must be filled out.";}
elseif ($pass != $rePass) { //all fileds are filled out, are the values valid?
$error = "The passwords do not match.";}
elseif (strlen($pass) < 4 || strlen($pass) > 12) { //check if username is too long or too short
$error = "You password must be between 4 and 12 characters long.";}
elseif (strlen($userName) < 4 || strlen($userName) > 12) { //check if username is too long or too short
$error = "You username must be between 4 and 12 characters long.";}
elseif (preg_replace('/[\w ]/', '', $userName)) { //check for invalid characters in the username
$error = "Your username may only contain letters, numbers, or underscores";}
else { //check if username already exists
$result = mysql_query($query) or die('Connection Error.');
if (mysql_num_rows($result) > 0) {
$error = "That username is already taken.";
}
else { //add user to the database
if (strlen($error) > 0) {
showRegisterForm($error);
} else {
mysql_query($insertUser) or die('Connection Error.');
echo 'You have been added! sign-in and enjoy.';
}
}
} //show form and errors if applicable
}
showRegisterForm($error);
?>
and here is my dbConnect.php which deals with the DB, minus the username etc.
<?php
//Database Settings
define("HOST", "localhost");
define("DBUSER", "******");
define("PASS", "******");
define("DB", "******");
//MySQL Connection
$conn = mysql_connect(HOST, DBUSER, PASS);
if (!conn)
{
// the connection failed so quit the script
die('Could not connect !<br />Please contact the site administrator.');
}
$db = mysql_select_db(DB);
if (!db)
{
// cannot connect to the database so quit the script
die('Could not connect to database <br /> Please contact the site administrator.');
}
?>
I would appreciate any ideas.