Jump to content

Registration Script


mattd8752

Recommended Posts

I get the following error:
[quote]
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/mattd/public_html/f1/register.php on line 29
Sorry, that username is already taken.
[/quote]
From the following code:
[code]
<?php
include "dbconnect.php";
if(!isset($_POST['register']) || !isset($_POST['regkey']) || !isset($_POST['username']) || !isset($_POST['password']) || !isset($_POST['email'])){
?>
<html>

<head>
<title>Registration Form</title>
</head>

<body>

<form method="POST" action="register.php">
Username:<input type="text" name="username" size="20"><br>
Password:<input type="password" name="password" size="20"><br>
Email:<input type="text" name="email" size="20">
Registration Key:<input type="text" name="regkey" size="20"> (you need to get a registration key to sign up)<br>
<input type="submit" value="Submit" name="register">
</form>

</body>

</html>

<?php
}else{
//reg them
$result=mysql_query("select * from users where username='$username'");
if(mysql_num_rows($result) != "1"){
echo "Sorry, that username is already taken.";
}else{
echo "Username is available...<br>";
$pass = md5($_POST['password']);
mysql_query("INSERT INTO users (username, password, regkey) VALUES($name, $hp, $username ) ");
echo "Your account should be registered.  Try logging in!  <a href=\"login.php\">Login Page</a>";
}
}
?>
[/code]

Please help with this one if you can, I am not quite sure what is causing the error.  Also, that user name is available.  It should say your account has been registered with the error if it were to be working properly how I want it to as well (but the error has to go anyway :D ) anyways, if you know what the problem is please reply.

Thanks in advanced -- Matt
Link to comment
Share on other sites

Now I'm not exactly sure what causes the problem but I had this difficulty earlier today and this is what was suggested to me to change it to in order to make it more efficent

[code]
$found = mysql_result(mysql_query('SELECT count(0) FROM `users` WHERE `username` = "'.$username.'" LIMIT 1'), 0, 0);
if ( $found > 0 ){
echo ("Sorry, that username is already taken.");
die;
}
else{
echo "Username is available...<br>";
$pass = md5($_POST['password']);
mysql_query("INSERT INTO users (username, password, regkey) VALUES($name, $hp, $username ) ");
echo "Your account should be registered.  Try logging in!  <a href=\"login.php\">Login Page</a>";
}
}
?>
[/code]

Some variables might need to be changed to suit your needs
Link to comment
Share on other sites

You forgot to transfer your $_POST variable.

Try this:

[code]

<?php
include "dbconnect.php";
if(!isset($_POST['register']) || !isset($_POST['regkey']) || !isset($_POST['username']) || !isset($_POST['password']) || !isset($_POST['email'])){
?>
<html>

<head>
<title>Registration Form</title>
</head>

<body>

<form method="POST" action="register.php">
Username:<input type="text" name="username" size="20"><br>
Password:<input type="password" name="password" size="20"><br>
Email:<input type="text" name="email" size="20">
Registration Key:<input type="text" name="regkey" size="20"> (you need to get a registration key to sign up)<br>
<input type="submit" value="Submit" name="register">
</form>

</body>

</html>

<?php
}else{
//reg them

$username= $_POST['username'];

$result=mysql_query("select * from users where username='$username'");
if(mysql_num_rows($result) != "1"){
echo "Sorry, that username is already taken.";
}else{
echo "Username is available...<br>";
$pass = md5($_POST['password']);
mysql_query("INSERT INTO users (username, password, regkey) VALUES($name, $hp, $username ) ");
echo "Your account should be registered.  Try logging in!  <a href=\"login.php\">Login Page</a>";
}
}
?>

[/code]
Link to comment
Share on other sites

[quote author=smc link=topic=123423.msg510098#msg510098 date=1169423647]
Now I'm not exactly sure what causes the problem but I had this difficulty earlier today and this is what was suggested to me to change it to in order to make it more efficent

[code]
$found = mysql_result(mysql_query('SELECT count(0) FROM `users` WHERE `username` = "'.$username.'" LIMIT 1'), 0, 0);
if ( $found > 0 ){
echo ("Sorry, that username is already taken.");
die;
}
else{
echo "Username is available...<br>";
$pass = md5($_POST['password']);
mysql_query("INSERT INTO users (username, password, regkey) VALUES($name, $hp, $username ) ");
echo "Your account should be registered.  Try logging in!  <a href=\"login.php\">Login Page</a>";
}
}
?>
[/code]

Some variables might need to be changed to suit your needs
[/quote]

Now what is the use of count(0), what does it do?  I don't quite get that part.
Link to comment
Share on other sites

Ok, I've got my second version of the script here, and its got some MAJOR bugs.

I have no idea how I've gotten to these, but:

Register.php:
[code]
<?php
include "dbconnect.php";
if(!isset($_POST['register']) || !isset($_POST['regkey']) || !isset($_POST['username']) || !isset($_POST['password']) || !isset($_POST['email'])){
?>
<html>

<head>
<title>Registration Form</title>
</head>

<body>

<form method="POST" action="register.php">
Username:<input type="text" name="username" size="20"><br>
Password:<input type="password" name="password" size="20"><br>
Email:<input type="text" name="email" size="20"><br>
Registration Key:<input type="text" name="regkey" size="20"> (you need to get a registration key to sign up)<br>
<input type="submit" value="Submit" name="register">
</form>

</body>

</html>

<?php
}else{
//reg them
$username = $_POST['username'];
$key = $_POST['regkey'];
$pass = $_POST['password'];
$pass = md5($pass);
$email = $_POST['email'];
$found = mysql_result(mysql_query('SELECT count(0) FROM `users` WHERE `username` = "'.$username.'" LIMIT 1'), 0, 0);
if ( $found > 0 ){
echo ("Sorry, that username is already taken.");
die;
}else{
echo "Username is available...<br>";
$pass = md5($_POST['password']);
mysql_query("INSERT INTO users (username, password, regkey, email) VALUES($username, $pass, $key, $email) ");
echo "Your account should be registered.  Try logging in!  <a href=\"login.php\">Login Page</a>";
}
}
?>
[/code]

It looks like its working on that page, but nothing is added to the DB.  Also the following error:
[quote]
Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/mattd/public_html/f1/register.php on line 33
[/quote]
But wait, thats not it, I've got a problem on my next page (yeah... it sucks).

I have the login page allowing me to login as anyone (doesn't that rock... if only I wasn't trying to fix it).
[code]
<?
ob_start();
session_start();
session_destroy();
$message="";


$Login=$_POST['Login'];
if(isset($Login)){
$username=$_POST['username'];
$md5=md5($_POST['password']);

// Connect database
include "dbconnect.php";


// Check matching of username and password.
$result=mysql_query("select * from users where username='$username'");
if(mysql_num_rows($result)!='0'){ // If match.
session_register("username");
header("location:main.php");
exit;
}else{ // If not match.
$message="The username or password you have entered is incorrect.<br>";
}

}
?>

<html>
<head>
<title>Login</title>
</head>

<body>
<? echo $message; ?>
<form id="form1" name="form1" method="post" action="<? echo $PHP_SELF; ?>">
<table>
<tr>
<td>User : </td>
<td><input name="username" type="text" id="username" /></td>
</tr>
<tr>
<td>Password : </td>
<td><input name="password" type="password" id="password" /></td>
</tr>
</table>
<input name="Login" type="submit" id="Login" value="Login" />
</form>
</body>
</html>
[/code]

This code outputs the form first, then no errors if I make up a username and password, but if I use my username and password I get the exact same thing.  The problem is the database isn't being searched.  The problem might be in my dbconnect script, so here it goes (I hope Im not boring you to death)
[code]
<?php
$host="localhost"; // Host name.
$db_user="mattd_racing"; // MySQL username.
$db_password="HIDDEN"; // MySQL password.
$database="mattd_racingdb"; // Database name.
mysql_connect($host,$db_user,$db_password);
mysql_select_db($database);
?>
[/code]

There was no error from this reported.  I am hoping you can help me, if you have any suggestions please post them.  And thanks trochia, I didn't think of using the manual.
Link to comment
Share on other sites

First place to look..<lol>  Are you sure you are addressing all your tables and fields properly ? <snytax>...and do they exit?

  Have you been connected to this db before?

Here's a D-n-D ( down and dirty ) quick table viewer snip..

<?

/* D-n-D ( down and dirty ) quick table viewer  */

$sql = "show tables";
$result = connect($sql);
while ($rows = mysql_fetch_array($result))
{
  echo $rows[0]."<br>";

}


function connect($sql)
{
    $username  = "uname"; // User Defined
    $pwd        = "pwd"; // User Defined
    $host      = "localhost"; // User Defined possibly if remote
    $dbname    = "dbname"; // User Defined

    //echo "commencing connection to local db<br>";
   
    if (!($conn=mysql_connect($host, $username, $pwd)))  {
        printf("error connecting to DB");
        exit();
    }
    $db2=mysql_select_db($dbname,$conn);
   
    $result = mysql_query($sql);
    if ($result){
        return $result;
    }else{
      $error = mysql_error();
        echo "Can't complete query because $error";
        die();   
    }
}   
?>
Link to comment
Share on other sites

[quote author=trochia link=topic=123423.msg510156#msg510156 date=1169426321]
First place to look..<lol>  Are you sure you are addressing all your tables and fields properly ? <snytax>...and do they exist?

  Have you been connected to this db before?

Here's a D-n-D ( down and dirty ) quick table viewer snip..

<?

/* D-n-D ( down and dirty ) quick table viewer  */

$sql = "show tables";
$result = connect($sql);
while ($rows = mysql_fetch_array($result))
{
  echo $rows[0]."<br>";

}


function connect($sql)
{
    $username   = "uname"; // User Defined
    $pwd        = "pwd"; // User Defined
    $host       = "localhost"; // User Defined possibly if remote
    $dbname     = "dbname"; // User Defined

    //echo "commencing connection to local db<br>";
   
    if (!($conn=mysql_connect($host, $username, $pwd)))  {
        printf("error connecting to DB");
        exit();
    }
    $db2=mysql_select_db($dbname,$conn);
   
    $result = mysql_query($sql);
    if ($result){
        return $result;
    }else{
      $error = mysql_error();
        echo "Can't complete query because $error";
        die();   
    }
}   
?>
[/quote]
Link to comment
Share on other sites

That fixed about half of the problem, kinda makes me feel dumb, I forgot to link the user to the database.  The D n' D way works now.  But registration doesn't want to add them to the database.  I don't see why this isn't working.  If you can find the error please let me know.  Login doesn't let me go as anyone, it does let me login if I create the data in the database. It also can read if the username is taken, So my guess is I am having the problem with:
[code] mysql_query("INSERT INTO users (username, password, regkey, email) VALUES($username, $pass, $key, $email) ");[/code]
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.