Jump to content

Notice: Trying to get property of non-object


midjam

Recommended Posts

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 :)

Link to comment
Share on other sites

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





?>

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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. :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.