PHP Regisration Form

I am creating a registration form with validation and I am stuck on this error. Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/www/familease.org.uk/register.php on line 25

The code I have, worked fine for me on my local host but when I transferred it to my websites server the error appeared. Help is hugely appreciated.

*Also because I know it’s coming I have tried using mysqli instead of mysql but it created more errors so I stuck with what I was most comfortable with.

Code:

<?php
require 'core.php';
$con=mysql_connect("localhost","*****","****","*****");

if (!loggedin()) {

if (isset($_POST['Firstname'])&&isset($_POST['Surname'])&&isset($_POST['Email'])&&isset($_POST['Username'])&&isset($_POST['Password'])&&isset($_POST['password_again'])) {
 $Firstname= $_POST['Firstname'];
 $Surname= $_POST['Surname'];
 $Email= $_POST['Email'];
 $Username= $_POST['Username'];
 $Password= $_POST['Password'];
 $password_hash = md5($Password);
 $password_again= $_POST['password_again'];
 
 if (!empty($Firstname)&&!empty($Surname)&&!empty($Email)&&!empty($Username)&&!empty($Password)&&!empty($password_again)){
    if ($Password!=$password_again) {
		echo 'Passwords do not match';
		
	} else {
	$query = "SELECT `Username` FROM `Parent` WHERE `Username`='$Username'";
	$query_run = mysql_query($query);
	echo $query_run;
	
	if (mysql_num_rows($query_run)==1) {
	  echo 'The username '.$Username.' already exists';
    } else {
	  $query = "INSERT INTO `Parents` VALUES('".mysql_real_escape_string($Firstname)."','".mysql_real_escape_string($Surname)."','".mysql_real_escape_string($Email)."','".mysql_real_escape_string($Username)."','".mysql_real_escape_string($password_hash)."','')";
	  if ($query_run = (mysql_query($query))) {
	  header('Location: register_success.php');
	  } else {
	  echo 'Sorry, we couldn\'t register you at this time. Try again later.';
	}
	}
	}
 } else {
 echo'All fields are required';
 }
 
}
 
 



?>

<form action="register.php" method="POST">
Firstname: <br> <input type="text" name="Firstname" ><br><br>
Surname: <br> <input type="text" name="Surname" ><br><br>
Email Address: <br> <input type="text" name="Email" ><br><br>
Username: <br> <input type="text" name="Username" ><br><br>
Password: <br> <input type="password" name="Password"><br><br>
Password Again: <br> <input type="password" name="password_again"><br><br>
<input type="submit" value="Register"></form>



<?php
} else if (loggedin()) {
  echo 'You\'re already registered and logged in.';
}

?>

Where to start??? :o

First mysql is DEPRECIATED, please consider using mysqli or PDO. There are plenty of tutorials out there on mysqli or PDO (I Recommend PDO) that are not that hard to understand. Heck, I think I created a tutorial that would at least get you heading in the right direction, see my signature’s link for it. Sometimes getting out of your comfort zone makes you a better coder, heck I tackle scripts that I’m not comfortable with. I do it for it is necessary and doing it right the first time will save in the long run, for doing it the other way will create a lot of unnecessary headaches. However, just stick with it and eventually a light-bulb will come on. :wink:

Second and lastly don’t use md5 as password hashing, instead consider using

[php]/ A nice password hashing library for PHP 5

// Find it here: https://github.com/ircmaxell/password_compat/blob/master/lib/password.php

// Read the Documentation for further help:

// NOTE: if you’re not using PHP 5, there are plenty of

// other good password hashing libraries out there —> JUST GOOGLE IT!

require ‘includes/password.inc.php’;[/php]

Don’t reinvent the wheel, use a trusted password library that some egghead(s) with vast knowledge has written, it will save you in the long run.

There I think I was nice about that (well I think I was). ;D

You can get a lot a help from visiting http://www.php.net/manual/en/function.date.php or to see a very simple registration/login tutorial just visit the link on my signature.

Change
[php]$query_run = mysql_query($query);[/php]

TO

[php]$query_run = mysql_query($query) or die ("Error in query: $query. ".mysql_error());[/php]

And you will get the answer to your problem. Post the mysql error here.

Sponsor our Newsletter | Privacy Policy | Terms of Service