Trying to set up an account registration system using mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
php_noob
Forum Newbie
Posts: 8
Joined: Fri Jun 26, 2009 10:59 pm

Trying to set up an account registration system using mysql

Post by php_noob »

Forumites,

I am trying to set up accounts for my website, using mysql. I have the registration page working; however, I cannot get the verification page (the page I'm using to assure that all of the fields meet my requirements) to work. So far this is all I have:

Code: Select all

 
<?php
include('config.php'); //config.php is the file where I connect to the database
 
//This verifies that the username is alfanumeric
 
$test=$_POST[username];
 
 
 
if (!eregi("([^A-Za-z0-9]"),$test)){
 
    //test for duplicate names
 
    $query="SELECT * FROM userbase WHERE username='$test'";
    $result=mysql_query($query);
    $num=mysql_num_rows($result);
 
    if ($num == 0){
 
}else{
header("Location:invalidname.html");
 
}
 
else{
header("Location:invalidname.html");
}
 
?>
 
 
I cannot get this work. I feed it incorrect information, yet it doesn't catch it. I have NO idea where the error is.
Last edited by Benjamin on Sat Jun 27, 2009 1:46 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Re: Trying to set up an account registration system using mysql

Post by a94060 »

this exists twice in the script

Code: Select all

# }else{
# header("Location:invalidname.html");

The problem may lie in the eregi statement, but i am not really sure about that. Also, the sql query should be

Code: Select all

$query="SELECT * FROM userbase WHERE username='.$test.'";
If you are currently debugging, an echo $query statement could help to see if that is the source of error


Also,thiss hould be in the PHP-Code forum
Last edited by Benjamin on Sat Jun 27, 2009 1:46 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Trying to set up an account registration system using mysql

Post by Benjamin »

:arrow: Moved to PHP - Code
php_noob
Forum Newbie
Posts: 8
Joined: Fri Jun 26, 2009 10:59 pm

Re: Trying to set up an account registration system using mysql

Post by php_noob »

Thanks for taking the time to rewrite the code! It still isn't working though. I can't get error codes with my hosting service, but I know the error isn't with the config.php file (I tested that and it works fine), and it isn't with the registration form (I echoed the username and it spat it out), so I think it's somewhere with the checking process.

username is the name I assigned the textbox on the registration page
php_noob
Forum Newbie
Posts: 8
Joined: Fri Jun 26, 2009 10:59 pm

Re: Trying to set up an account registration system using mysql

Post by php_noob »

Here's the deal: I have three scripts trying to do a job.

register.php - this is where the registration form is. It contains a username, password, and email field. The action of the registration form is to submit the data to registernext.php to be checked to assure a valid username.

The config.php file contains this:

Code: Select all

 
<?php
 
$host="mysql1.100ws.com";//hostname
$username="databaseusername";//username to database, real username edited out
$password="password";//password to database, real password edited out
$db_name="databasename";//database name, real database name edited out
 
//connect to database
mysql_connect("$host","$username","$password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select database");
 
 
?>
 
When I intentionally submit bad information (%## as a username or a username already in the database) it doesn't redirect my browser to the error page. To troubleshoot, when I have it echo back the username I submitted (with no other code) it works; however, when I include the full code (the one you rewrote) it doesn't echo the username back.
php_noob
Forum Newbie
Posts: 8
Joined: Fri Jun 26, 2009 10:59 pm

Re: Trying to set up an account registration system using mysql

Post by php_noob »

Okay, we're getting somewhere. When I fed it a username that already existed in my database ('clash') it outputted this:

The script was successful. The username was found in the database.

SELECT * FROM userbase WHERE username = 'clash'
The problem is that it is now sending EVERYTHING to invalidname.html, except for when it finds a username that is already in the database.
php_noob
Forum Newbie
Posts: 8
Joined: Fri Jun 26, 2009 10:59 pm

Re: Trying to set up an account registration system using mysql

Post by php_noob »

It's just a page that tells the user that they did something wrong (ie. provide an username using symbols or provide an existing username).

It's sending perfectly good names (alfanumeric and not preexisting) to invalidname.html. However, it stays on registernext.php outputting that message when I send it a name that already exists in the database.
php_noob
Forum Newbie
Posts: 8
Joined: Fri Jun 26, 2009 10:59 pm

Re: Trying to set up an account registration system using mysql

Post by php_noob »

I simply want to send it to the database (I know I didn't include that in the original script).
php_noob
Forum Newbie
Posts: 8
Joined: Fri Jun 26, 2009 10:59 pm

Re: Trying to set up an account registration system using mysql

Post by php_noob »

Alright, I'm REALLY confused. Now when I submit data:

1. if it is already in the database it will go to invalidname.html, AND it will input it into the database. I only want it to go to invalidname.html

2. if it is NOT in the database it outputs:
The script was successful. The username was found in the database.

SELECT * FROM userbase WHERE username = 'bingfi'
and it DOESN'T put it in the database. I want it to be put into the database.

Here's my code:

Code: Select all

 
<?php
error_reporting(E_ALL | E_STRICT); // Added
ini_set('display_errors', 'On'); // Added
include './config.php';
$success = false;
if (isset($_POST['username'])) {
    $test = preg_replace('/[^A-Za-z0-9]+/', '', $_POST['username']);
    if (strlen($test) > 0) {
        $query = "SELECT * FROM userbase WHERE username = '$test'";
        $result = mysql_query($query);
        $num = mysql_num_rows($result);
        if ($num == 0) {
            $success = true;
        } else {
            $username = $_POST['username'];
            $password = $_POST['pass'];
            $email = $_POST['email'];
 
            $query="INSERT INTO userbase (username,password,email) VALUES      ('$username','$password','$email')"; 
             mysql_query($query);
 
            
            
           }
    }
}
if (!$success) {
    if (!headers_sent()) {
        header('Location: invalidname.html');      
    }
    echo "<p>There must have been an error message.</p>\n"
       . "<pre>$query</pre>"; // Added
    exit;
}
echo "<p>The script was successful. The username was found in the database.</p>\n"
   . "<pre>$query</pre>"; // Added
?>
 
 
php_noob
Forum Newbie
Posts: 8
Joined: Fri Jun 26, 2009 10:59 pm

Re: Trying to set up an account registration system using mysql

Post by php_noob »

Thanks, with your help, I finally have everything set up and working perfectly. I really appreciate your help!
Post Reply