frijole Posted May 18, 2008 Share Posted May 18, 2008 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. Quote Link to comment Share on other sites More sharing options...
LooieENG Posted May 18, 2008 Share Posted May 18, 2008 <?php //Database Settings $host = 'localhost'); $dbuser = '******'); $pass = '******'); $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.'); } ?> Quote Link to comment Share on other sites More sharing options...
frijole Posted May 18, 2008 Author Share Posted May 18, 2008 was anything changed here? or just reposted? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted May 18, 2008 Share Posted May 18, 2008 All of your mysql_query() statements report a 'Connection Error.' when the query fails - $result = mysql_query($query) or die('Connection Error.'); Change all those to something meaningful and add a mysql_error() statement to get mysql to tell you why the query failed. Quote Link to comment Share on other sites More sharing options...
frijole Posted May 18, 2008 Author Share Posted May 18, 2008 so I would replace die with a mysql_error()? Quote Link to comment Share on other sites More sharing options...
947740 Posted May 18, 2008 Share Posted May 18, 2008 frijole-replace or die('Connection Error.'); with or die('Connection Error'.mysql_error($conn)); LooieENG did make changes, but his variables were incorrect. $host = 'localhost'); $dbuser = '******'); $pass = '******'); $db = '******'); Those should not have parantheses. Quote Link to comment Share on other sites More sharing options...
frijole Posted May 18, 2008 Author Share Posted May 18, 2008 here is what I have now and it is acting as if it was successful, no error. It echoes "You have been successfully added" I don't get it. <?php //Database Settings $host = "localhost"; $dbUser = "thinksna"; $pass = "******"; $DB = "thinksna_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, $conn); if (!db) { // cannot connect to the database so quit the script die('Could not connect to database <br /> Please contact the site administrator.'); } //this script registers a new user. 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 mysql_error($conn); 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 { $result = mysql_query($insertUser) or mysql_error($conn); echo 'You have been added! sign-in and enjoy.'; } } } //show form and errors if applicable } showRegisterForm($error); ?> Quote Link to comment Share on other sites More sharing options...
LooieENG Posted May 18, 2008 Share Posted May 18, 2008 Woops, forgot to remove those when I edited Quote Link to comment Share on other sites More sharing options...
947740 Posted May 18, 2008 Share Posted May 18, 2008 Yeah, I noticed that. Change or mysql_error($conn); to or die("ERROR!".mysql_error($conn); ERROR! does not make a difference, obviously. Also, change this: echo 'You have been added! sign-in and enjoy.'; To this: if($result) { echo 'You have been added! sign-in and enjoy.'; } Quote Link to comment Share on other sites More sharing options...
frijole Posted May 18, 2008 Author Share Posted May 18, 2008 here is the error errorUnknown column '$userName' in 'field list' Quote Link to comment Share on other sites More sharing options...
frijole Posted May 18, 2008 Author Share Posted May 18, 2008 ok, thank you so much for the help. Now it is inserting the data but it is inserting $userName as the username instead of the value that it holds. Quote Link to comment Share on other sites More sharing options...
frijole Posted May 18, 2008 Author Share Posted May 18, 2008 it's fixed! thanks so much guys. You've really been a huge help. Quote Link to comment Share on other sites More sharing options...
947740 Posted May 18, 2008 Share Posted May 18, 2008 Glad to here it worked. No problem. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.