April 11, 201115 yr Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/bkwebdes/public_html/php/register.php on line 19 Error Sorry, your registration failed. Please go back and try again. =============================================== does anybody no whats wrong with this? <?php include "base.php"; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>User Management System (Tom Cameron for NetTuts)</title> <link rel="stylesheet" href="style.css" type="text/css" /> </head> <body> <div id="main"> <?php if(!empty($_POST['username']) && !empty($_POST['password'])) { $username = mysql_real_escape_string($_POST['username']); $password = md5(mysql_real_escape_string($_POST['password'])); $email = mysql_real_escape_string($_POST['email']); $checkusername = mysql_query("SELECT * FROM users WHERE Username = '".$username."'"); if(mysql_num_rows($checkusername) == 1) { echo "<h1>Error</h1>"; echo "<p>Sorry, that username is taken. Please go back and try again.</p>"; } else { $registerquery = mysql_query("INSERT INTO users (Username, Password, EmailAddress) VALUES('".$username."', '".$password."', '".$email."')"); if($registerquery) { echo "<h1>Success</h1>"; echo "<p>Your account was successfully created. Please <a href=\"index.php\">click here to login</a>.</p>"; } else { echo "<h1>Error</h1>"; echo "<p>Sorry, your registration failed. Please go back and try again.</p>"; } } } else { ?> <h1>Register</h1> <p>Please enter your details below to register.</p> <form method="post" action="register.php" name="registerform" id="registerform"> <fieldset> <label for="username">Username:</label><input type="text" name="username" id="username" /><br /> <label for="password">Password:</label><input type="password" name="password" id="password" /><br /> <label for="email">Email Address:</label><input type="text" name="email" id="email" /><br /> <input type="submit" name="register" id="register" value="Register" /> </fieldset> </form> <?php } ?> </div> </body> </html> www.bkwebdesignandhosting.co.uk/php Edited April 11, 201115 yr by kingy da killa
April 11, 201115 yr Can you try replacing this line if(mysql_num_rows($checkusername) == 1) with this line if (mysql_num_rows($checkusername) > 0) Pat
April 11, 201115 yr Author thanks for trying it did not work though, i think it may be something to do with my sql database... going to have a playaround now
April 11, 201115 yr thanks for trying it did not work though, i think it may be something to do with my sql database... going to have a playaround now Hi, Are you sure that you have msyql connection? Or if you have, try to put var_dump(mysql_error()); after $checkusername = mysql_query("SELECT * FROM users WHERE Username = '".$username."'"); That will tell if something is wrong with the query.
April 11, 201115 yr Author Ive got a connection defo, but this comes up: Parse error: syntax error, unexpected $end in /home/bkwebdes/public_html/php/register.php on line 67 when i add that line in to register.php or index.php
April 11, 201115 yr Ive got a connection defo, but this comes up: Parse error: syntax error, unexpected $end in /home/bkwebdes/public_html/php/register.php on line 67 when i add that line in to register.php or index.php sounds like you have not closed all your curly braces can you try closing a final curly brace in your last php statement <?php } } ?>
April 11, 201115 yr Author woo got it working i typed my sql qwuery in wrong: CREATE TABLE `users` ( `UserID` INT(25) NOT NULL AUTO_INCREMENT PRIMARY KEY , `Username` VARCHAR(65) NOT NULL , `Password` VARCHAR(32) NOT NULL , `EmailAddress` VARCHAR(255) NOT NULL ); all good now
April 11, 201115 yr Author once i log in, it does not take me to a members page or anything.. <?php } elseif(!empty($_POST['username']) && !empty($_POST['password'])) { $username = mysql_real_escape_string($_POST['username']); $password = md5(mysql_real_escape_string($_POST['password'])); $checklogin = mysql_query("SELECT * FROM users WHERE Username = '".$username."' AND Password = '".$password."'"); if(mysql_num_rows($checklogin) == 1) { $row = mysql_fetch_array($checklogin); $email = $row['EmailAddress']; $_SESSION['Username'] = $username; $_SESSION['EmailAddress'] = $email; $_SESSION['LoggedIn'] = 1; echo "<h1>Success</h1>"; echo "<p>We are now redirecting you to the member area.</p>"; echo "<meta http-equiv='refresh' content='=2;index.php' />"; } else { echo "<h1>Error</h1>"; echo "<p>Sorry, your account could not be found. Please <a href=\"index.php\">click here to try again</a>.</p>"; } i checked the databse and new registerations are on there, anybody got a clue?
April 11, 201115 yr can you replace this echo "<h1>Success</h1>"; echo "<p>We are now redirecting you to the member area.</p>"; echo "<meta http-equiv='refresh' content='=2;index.php' />"; with this header('Location: yourpage.php');
April 11, 201115 yr Author can you replace this echo "<h1>Success</h1>"; echo "<p>We are now redirecting you to the member area.</p>"; echo "<meta http-equiv='refresh' content='=2;index.php' />"; with this header('Location: yourpage.php'); it lets me change that with no errors, but still doesent do antyhign when i log in but refresh the index page >.<
April 11, 201115 yr Meta redirects should be in the <head> ... </head> tags to work Personally I would use header('Location: yourpage.php'); as Pat has suggested, although make sure after that you put die(); on a new line, so that it terminates there and redirects (this is to stop any other code executing once the header has been added, otherwise you could end up with unexpected result. Also, make sure there are NO echo's or output before it Edited April 11, 201115 yr by Jay Gilford
April 11, 201115 yr Author <?php include "base.php"; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>User Management System (Tom Cameron for NetTuts)</title> <link rel="stylesheet" href="style.css" type="text/css" /> </head> <body> <div id="main"> <?php if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username'])) { ?> <h1>Member Area</h1> <pThanks for logging in! You are <b><?=$_SESSION['Username']?></b> and your email address is <b><?=$_SESSION['EmailAddress']?></b>.</p> <?php } elseif(!empty($_POST['username']) && !empty($_POST['password'])) { $username = mysql_real_escape_string($_POST['username']); $password = md5(mysql_real_escape_string($_POST['password'])); $checklogin = mysql_query("SELECT * FROM users WHERE Username = '".$username."' AND Password = '".$password."'"); if(mysql_num_rows($checklogin) == 1) { $row = mysql_fetch_array($checklogin); $email = $row['EmailAddress']; $_SESSION['Username'] = $username; $_SESSION['EmailAddress'] = $email; $_SESSION['LoggedIn'] = 1; echo "<h1>Success</h1>"; header('Location: yourpage.php');die(); } else { echo "<h1>Error</h1>"; echo "<p>Sorry, your account could not be found. Please <a href=\"index.php\">click here to try again</a>.</p>"; } } else { ?> <h1>Member Login</h1> <p> Thanks for visiting! Please either login below, or <a href="register.php">click here to register</a>.</p> <form method="post" action="index.php" name="loginform" id="loginform"> <fieldset> <label for="username">Username:</label><input type="text name="username" id="username" /><br /> <label for="password">Password:</label><input type="password" name="password" id="password" /><br /> <input type="submit" name="login" id="login" value="Login" /> </fieldset></form> <?php } ?> </div> </body> </html> still the script is just refreshing the index page
April 11, 201115 yr Your form action is sending you to the index page <form method="post" action="index.php" name="loginform" id="loginform"> Split the php and the html into two files the php part save as login.php or similar and in the form action change it to login. you will also have to remove this from the php part at the end else { And remove this from the html part <?php } ?> Edited April 11, 201115 yr by pat24
April 11, 201115 yr Author Your form action is sending you to the index page <form method="post" action="index.php" name="loginform" id="loginform"> Split the php and the html into two files the php part save as login.php or similar and in the form action change it to login. you will also have to remove this from the php part at the end else { And remove this from the html part <?php } ?> <label for="username">Username:</label><input type="text name="username" id="username" /><br /> i forgot another " woo it works now www.bkwebdesignandhosting.co.uk/php if you want to try
Create an account or sign in to comment