FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Question about a php and mysql script.





Diablosblizz
Okay, this "may" be a long post. I am having php/mysql problems, but I just started to get them about 2 days ago. Heres my PHP Code:

Code:
<?
session_start();
include("database.php");

/**
 * Returns true if the username has been taken
 * by another user, false otherwise.
 */
function usernameTaken($username){
   global $conn;
   if(!get_magic_quotes_gpc()){
      $username = addslashes($username);
   }
   $q = "select username from users where username = '$username'";
   $result = mysql_query($q,$conn);
   return (mysql_numrows($result) > 0);
}

/**
 * Inserts the given (username, password) pair
 * into the database. Returns true on success,
 * false otherwise.
 */
function addNewUser($username, $password){
   global $conn;
   $q = "INSERT INTO users VALUES ('$username', '$password')";
   return mysql_query($q,$conn);
}

/**
 * Displays the appropriate message to the user
 * after the registration attempt. It displays a
 * success or failure status depending on a
 * session variable set during registration.
 */
function displayStatus(){
   $uname = $_SESSION['reguname'];
   if($_SESSION['regresult']){
?>
<h1>Registered!</h1>
<p>Thank you <b><? echo $uname; ?></b>, your information has been added to the database, you may now <a href="main.php" title="Login">log in</a>.</p>

<?
   }
   else{
?>
<h1>Registration Failed</h1>
<p>We're sorry, but an error has occurred and your registration for the username <b><? echo $uname; ?></b>, could not be completed.<br>
Please try again at a later time.</p>

<?
   }
   unset($_SESSION['reguname']);
   unset($_SESSION['registered']);
   unset($_SESSION['regresult']);
}

if(isset($_SESSION['registered'])){
/**
 * This is the page that will be displayed after the
 * registration has been attempted.
 */
?>

<html>
<title>Registration Page</title>

<? displayStatus(); ?>

</body>
</html>

<?
   return;
}

/**
 * Determines whether or not to show to sign-up form
 * based on whether the form has been submitted, if it
 * has, check the database for consistency and create
 * the new account.
 */
if(isset($_POST['subjoin'])){
   /* Make sure all fields were entered */
   if(!$_POST['user'] || !$_POST['pass']){
      die('You didn\'t fill in a required field.');
   }

   /* Spruce up username, check length */
   $_POST['user'] = trim($_POST['user']);
   if(strlen($_POST['user']) > 30){
      die("Sorry, the username is longer than 30 characters, please shorten it.");
   }

   /* Check if username is already in use */
   if(usernameTaken($_POST['user'])){
      $use = $_POST['user'];
      die("Sorry, the username: <strong>$use</strong> is already taken, please pick another one.");
   }

   /* Add the new account to the database */
   $md5pass = md5($_POST['pass']);
   $_SESSION['reguname'] = $_POST['user'];
   $_SESSION['regresult'] = addNewUser($_POST['user'], $md5pass);
   $_SESSION['registered'] = true;
   echo "<meta http-equiv=\"Refresh\" content=\"0;url=$HTTP_SERVER_VARS[PHP_SELF]\">";
   return;
}
else{
/**
 * This is the page with the sign-up form, the names
 * of the input fields are important and should not
 * be changed.
 */
?>
<head><title>Members Area - Register</title></head>
<html>
<body bgcolor="#4b4c4b">
<font face="Verdana, Arial, Helvetica, sans-serif" color="#FFFFFF" size="2">
<h1>Register</h1>
<form action="<? echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr><td><font face="Verdana, Arial, Helvetica, sans-serif" color="#FFFFFF" size="2">Username:</td><td><input type="text" name="user" maxlength="30"></td></tr>
<tr><td><font face="Verdana, Arial, Helvetica, sans-serif" color="#FFFFFF" size="2">Password:</td><td><input type="password" name="pass" maxlength="30"></td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="subjoin" value="Join!"></font></form><BR><BR></center>
<font face="Verdana, Arial, Helvetica, sans-serif" color="#FFFFFF" size="2">
</td></tr>
</table>
</form>
</body>
</html>

<?
}
?>


Its called register.php. When I register I "quickly" get this error:

Code:
Warning: mysql_rumrows(): suplied argument is not a valid MySQL result resource in /home/diablos/public_html/users/register.php on line 16


and then the page changes to "Sorry you could not be registered". I have never had problems with this code before, so was "mysql_rumrows()" disabled or something?

I really need some help!

Thanks!
chris20
that type of error, in my experience anyway usually occurs when your sql query is messed up, try echoing the mysql query to the screen, then going into phpmyadmin and typing the query that comes up, if phpmyadmin gives u an error, then that is the problem

also, mysql_numrows is outdated and obsolete, you might want to consider using mysql_num_rows


hope this helps
Diablosblizz
Thank you for helping me. Though I did not create this, I will contact the creator and see whats wrong. Thanks.
vignesh_natraj
you might have missed a double quote somwhere in the query statement
Related topics
Need a Registration form PHP/Mysql with email confirmation
A very good PHP MySQL Tutorial
Free PHP/MySQL login script
PHP, MySQL...
ASP +PHP+MySQL Tutorials
Php voting script needed (URGENT)
PHP/MYSQL Poll?
PHP contact script...
Link PHP/Mysql with Outlook Express
PHP/MySql Sending e-mail subcription confirmation to webmast
Simple PHP Login Script
php msql quiz script
Hot or Not PHP-Nuke script
What's the best PHP Wiki script?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.