wavedude Posted February 1, 2009 Share Posted February 1, 2009 I'm having trouble getting my php page to connect to the database. What could be wrong? Heres the code: <?php session_start(); //lets start the session so we could use them! // session_start() must always be used if you want to use session variables on the page. require_once("data.php"); //include.. if(empty($_SESSION['username'])){ //check if user is not logged in $act = htmlspecialchars(mysql_real_escape_string($_GET['act'])); // security hoo-haa if($act == 'reg'){ // if we've attempted to start the registration process... // lets set the variables $username = htmlspecialchars(mysql_real_escape_string($_POST['username'])); // username... $username = strtolower($username); // lets make it lower cased so username like fRIiKs would be transformed to friiks $password = htmlspecialchars(mysql_real_escape_string($_POST['password'])); // password(s) $passwordCheck = htmlspecialchars(mysql_real_escape_string($_POST['passwordCheck'])); $email = htmlspecialchars(mysql_real_escape_string($_POST['email'])); // E-mail $checkUsername = mysql_num_rows(mysql_query("SELECT `id` FROM `users` WHERE `username`='".$username."' LIMIT 1")); // check if username not taken $emailCheck = mysql_num_rows(mysql_query("SELECT `id` FROM `users` WHERE `email`='".$email."' LIMIT 1")); // check if E-mail not taken if($checkUsername < 1 && $emailCheck < 1 && !empty($password) && !empty($username) && $password == $passwordCheck && strlen($username) < 18 && strlen($password) < 32){ // if everything's fine lets continue with the registration process $hash = substr(md5(mt_rand(0000000,999999)."somethingForNothingRawr!"),0,25); // create random, 25 character long hash $password = sha1($password.$hash); // create password $lastId = mysql_fetch_array(mysql_query("SELECT `userId` FROM `users` ORDER BY `id` DESC LIMIT 1")); $myId = $lastId['userId']+1; // Get the last user id in the database and create the current users id if(mysql_query("INSERT INTO `users` (`id`, `userId`, `userLevel`, `username`, `password`, `email`, `registered`, `avatar`, `randString`) VALUES ('NULL','".$myId."','1','".$username."','".$password."','".$email."','".time()."','','".$hash."')")){ // If the user data is inserted into the DB return success! header("Location:register.php?act=success"); }else{ // if not, return error... header("Location:register.php?act=err7"); } }else{ // if something's wrong above let's check out what IS wrong and return an error! if($checkUsername > 0){ header("Location:register.php?act=err0"); } if($emailCheck > 0){ header("Location:register.php?act=err1"); } if(empty($password)){ header("Location:register.php?act=err2"); } if(empty($username)){ header("Location:register.php?act=err3"); } if($password != $passwordCheck){ header("Location:register.php?act=err4"); } if(strlen($username) > 18){ header("Location:register.php?act=err5"); } if(strlen($password) > 32){ header("Location:register.php?act=err6"); } } } $errors = array( "0" => "Username already taken!", "1" => "This E-mail is already used!", "2" => "Password field must be filled!", "3" => "Username field must be filled!", "4" => "Passwords you entered didn't match!", "5" => "Username is longer than 18 characters", "6" => "Password is longer than 32 characters", "7" => "There was an error while inserting data into database!<br />Please try again later!" ); // lets create error reports... if($act == "success"){ ?> <span style="color:green;font-weight:bold">Registration was successful, you may log in <a href="index.php">here</a></span> <?php }else if(ereg('err',$act) > 0){ // if $act variable contains "err" then lets get the number of error and output the error! ?> <span style="color:red;font-weight:bold"><?php echo $errors[substr($act,3)]; ?></span> <?php } ?> <form action="register.php?act=reg" method="post" > <table> <tr><td>Username:</td><td><input type='text' name='username' maxlength='18' /></td></tr> <tr><td>Password:</td><td><input type='password' name='password' maxlength='32' /></td></tr> <tr><td>Password Again:</td><td><input type='password' name='passwordCheck' maxlength='32' /></td></tr> <tr><td>E-Mail:</td><td><input type='text' name='email' /></td></tr> <tr><td><input type='submit' value='Register' /></td></tr> </table> </form> <?php }else{ // if user's logged in return the user to index header("Location:index.php"); } ?> That would be the registering page. It connects to a page called data.php to get the MySQL info. Please help, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/143332-cant-connect-to-mysql/ Share on other sites More sharing options...
skiingguru1611 Posted February 1, 2009 Share Posted February 1, 2009 Could you please show us the "data.php" code? Quote Link to comment https://forums.phpfreaks.com/topic/143332-cant-connect-to-mysql/#findComment-751748 Share on other sites More sharing options...
Philip Posted February 1, 2009 Share Posted February 1, 2009 Separate your mysql_fetch's and queries, and run or die(mysql_error()); on each of the queries: Example, your check username: $checkUsername = mysql_num_rows(mysql_query("SELECT `id` FROM `users` WHERE `username`='".$username."' LIMIT 1")); Change to: $checkUsernameQ = mysql_query("SELECT `id` FROM `users` WHERE `username`='".$username."' LIMIT 1") or die(mysql_error()); $checkUsername = mysql_num_rows($checkUsernameQ); Quote Link to comment https://forums.phpfreaks.com/topic/143332-cant-connect-to-mysql/#findComment-751750 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.