midjam Posted July 6, 2011 Share Posted July 6, 2011 Hi guys, need some help with a registeration page i`m creating. I was following and all seems to work fine however, i`m getting the following error message: Notice: Trying to get property of non-object in C:\xampp\htdocs\fishfinder\submit-registration.php on line 41 Here is my code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php include("connections/connection.php"); $username = $_POST['username']; $email = $_POST['email']; $password = $_POST['password']; $passwordconfirm = $_POST['passwordconfirm']; // Prevent SQL Injections $username = mysql_real_escape_string(stripslashes($username)); $email = mysql_real_escape_string(stripslashes($email)); $password = mysql_real_escape_string(stripslashes($password)); $passwordconfirm = mysql_real_escape_string(stripslashes($passwordconfirm)); // Get all users from database. $sql = "SELECT * FROM user"; $resultCount = mysql_query($sql, $cn) or die(mysql_error($cn)); // Check how many users were returned from query above. $num_users = mysql_num_rows($resultCount); // Check each username to see if in use. $row_count = -1; while ($row_count < $num_users) { $data = mysql_fetch_object($resultCount); $row_count++; if ($data->username == $username) { echo '<p>The username "' . $username . '" is not available.</p>'; $row_count = $num_users; } else if ($row_count == $num_users) { echo '<p>The username "' . $username . '" has been selected.</p>'; if ($password != $passwordconfirm) { echo '<p>Passwords do not match.</p>'; echo '<p><strong>New user has not been created.</strong></p>'; } else { echo '<p>Passwords match.</p>'; $datejoined = time(); $sql = "INSERT INTO user (username, email, password, datejoined, userlevel, active) VALUES ('" . $username . "', '" . $email . "', '" . $password . "', '" . $datejoined . "', '1', '1')"; $result = mysql_query($sql, $cn) or die(mysql_error($cn)); echo "<p><strong>The username '" . $username . "' has been created. Please login <a href='login.php'>here</a>.</strong></p>"; } } } ?> </body> </html> Any help would be great, as i`m exhausted looking for a solution. Thanks Quote Link to comment Share on other sites More sharing options...
gristoi Posted July 6, 2011 Share Posted July 6, 2011 it is telling you that your query object is not an object. try this: <?php $sql = "SELECT * FROM user"; $resultCount = mysql_query($sql, $cn) or die(mysql_error($cn)); while ($row= mysql_fetch_object($resultCount)) { if ($row->username == $username) { // rest of code ?> Quote Link to comment Share on other sites More sharing options...
TeNDoLLA Posted July 6, 2011 Share Posted July 6, 2011 Most likely your sql query is failing and leading that the resultset is not proper result and the fetch_object function does not return an object. Quote Link to comment Share on other sites More sharing options...
gristoi Posted July 6, 2011 Share Posted July 6, 2011 Forgot to mention that, TeNDoLLA is right. Check that your query is actually returning anything. Quote Link to comment Share on other sites More sharing options...
midjam Posted July 6, 2011 Author Share Posted July 6, 2011 Thanks guys for your replies I`m still fairly new to PHP and do not know how to check, tried echo $sql; and it displayed the SELECT. The funny thing is, the form seems to work fine. Quote Link to comment Share on other sites More sharing options...
gristoi Posted July 6, 2011 Share Posted July 6, 2011 var_dump($resultCount); see what that gives you Quote Link to comment Share on other sites More sharing options...
midjam Posted July 6, 2011 Author Share Posted July 6, 2011 ok thanks, done that and got this: resource(5) of type (mysql result) Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted July 6, 2011 Share Posted July 6, 2011 Your original while() logic test is wrong and will cause the loop to be execuited once when there are zero rows and mysql_fetch_object($resultCount) will return a false value, not an object. Actually, in looking at your logic, you need to redo all of that. If you are trying to determine if a username is already in use or not, you don't query for all the rows in the table and loop through all of them. You use a WHERE clause in your query to try and find if the entered username exists. You then simply test what mysql_num_rows returns to decide if the user name exists or not using one if(){}else{} statement, no loop. Edit: Your code also needs to test if your form was submitted at all (prevents your code from executing when a search engine indexes your site or someone requests the URL of your form processing page.) Edit2: If some tutorial had that code as part of it, find a different tutorial. Quote Link to comment Share on other sites More sharing options...
midjam Posted July 6, 2011 Author Share Posted July 6, 2011 Thanks for your reply. Have always used dreamweaver to do this kinda thing but, wanted to try and code it myself to get a better understanding of how PHP & mysql works. Maybe I should just use the bloated DW code for now. 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.