Jump to content

Register Script Not Working D:


Ne.OnZ

Recommended Posts

Hello, I have been in those code dilemma for a while. I have no idea why it does not work. Here is the code:

 

<html>
<body>
<div align="center">

<?php require("connect.php"); ?>

<?php
$username = $_POST['user'];
$password = sha1($_POST['pass']);
$password2 = $_POST['pass2'];
$email = $_POST['email'];

function error()
{
   if(!$query)
   {
      echo "Query Failed: " . mysql_errno() . '<br />' . mysql_error();
    }
}

function check() 
{ 
      mysql_select_db("divnxn5_web");
      $result = mysql_query("SELECT `username` FROM `users` WHERE `username` = '$username'");
      $error = mysql_query($result) OR die(mysql_error());
   
      while($row = mysql_fetch_array($result))
      {
         echo "The username " . $username . 'is already taken!';
       }
}

if(!$con)
{
  echo "<br /><br />Registering is disabled right now, please check back later.";
}
else
{
  if(!$_POST['register'])
     {
       echo "<br /><br />Use the following form to register a new account with us.
   <form action='register.php' method='post'><br /><br />
   <font size='2'>Username:</font><br />
   <input type='text' id='user' name='user' size='20' style='background-color: #FFFFFF; font-size: 8pt; border: 1 solid #003399' /><br />
   <font size='2'>Password:</font><br />
   <input type='password' id='pass' name='pass' size='20' style='background-color: #FFFFFF; font-size: 8pt; border: 1 solid #003399' /><br />
   <font size='2'>Confirm Password:</font><br />
   <input type='password' id='pass2' name='pass2' size='20' style='background-color: #FFFFFF; font-size: 8pt; border: 1 solid #003399' /><br />
   <font size='2'>E-mail Address:</font><br />
   <input type='text' id='email' name='email' size='20' style='background-color: #FFFFFF; font-size: 8pt; border: 1 solid #003399' /><br /><br />
   <input type='submit' name='register' id='register' value='Register' style='background-color: #FFFFFF; color: #000000; font-size: 8pt; border: 1 solid #003399' />
   <input type='reset' value='Clear' style='background-color: #FFFFFF; color: #000000; font-size: 8pt; border: 1 solid #003399' />
   </form>";
     }
  else
     {
         if(!$username)
           echo "The username you entered encountered a problem.<br /><br />";
           check();

        if(!$password || !$password2)
           echo "The password field cannot be left empty!<br /><br />";

        if(!$email)
           echo "The email you entered encountered a problem.<br /><br />";

        if(!$password == $password2)
           echo "The passwords you entered do not match.<br /><br />";

        else
        {
            mysql_select_db("divnxn5_web");
            mysql_query("INSERT INTO users(username, password, email) VALUES('$username', '$password', '$email')");
            error();
            mysql_close($con);
            echo "Thank you for registering with us " . $username . '! Enjoy your stay!';
       }
   }
}
?>

</div>
</body>
</html>

 

The Error I get is: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #4' at line 1

 

What I'm trying to do is check if the username is taken after they fill it out. Another problem is, it doesn't even check for other problems, the password being empty/not matching or email being empty. I'm using php 4.4.7 if that helps any.

 

Would greatly appreciate it if anyone helps me out.

 

Thank You,

Ne.OnZ  :)

Link to comment
Share on other sites

  • Replies 51
  • Created
  • Last Reply

Top Posters In This Topic

To give you a heads up, as a helper I usually ignore people who feel the need to spam themselves to the top less than a day after posting.

 

I'm going to leave you with a hint here

Check when you're trying to do with $result. Try echo'ing it. Are you sure it's something you want to query to the database?

Link to comment
Share on other sites

One error here:

 

<?php

//it should be:
$password = sha1($_POST['pass']);
$password2 = sha1($_POST['pass2']);

if($password != $password2)
     echo "The passwords you entered do not match.<br /><br />";

?>

Link to comment
Share on other sites

These lines are incorrect:

<?php
      $result = mysql_query("SELECT `username` FROM `users` WHERE `username` = '$username'");
      $error = mysql_query($result) OR die(mysql_error());
?>

The should be something like:

<?php
      $query = "SELECT `username` FROM `users` WHERE `username` = '$username'";
      $result = mysql_query($query) or die("Problem with the query: $query<br>" . mysql_error());
?>

 

Ken

Link to comment
Share on other sites

In the sticky, it says I can bump after several hours of no response.

 

I tried it the way you guys told me. I tried a username that was already in the database and it did not catch it. It added the same username again. And the other error checking still don't appear.

Link to comment
Share on other sites

<?php
$num_rows= mysql_num_rows($username);

if($num_rows<=0) { die('no username in 
database');}

elseif($num_rows>1) { die(' you cannot enter more than one username'); }

?>

I get the error: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/divnxn5/public_html/register.php on line 23. Followed by No username in database. I checked through phpmyadmin, and there are 2 usernames that I manually entered.

Link to comment
Share on other sites

Sorry, this is what I get. As you see, same thing as the error mentioned above.

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/divnxn5/public_html/register.php on line 23
No Username in database

 

The only mysql_num_rows() I have is the one in the code you gave me.

Link to comment
Share on other sites

Yes. :o

 

<?php 
require("connect.php");

$username = $_POST['user'];
$password = sha1($_POST['pass']);
$password2 = sha1($_POST['pass2']);
$email = $_POST['email'];

function error()
{
   if(!$query)
   {
      echo "Query Failed: " . mysql_errno() . '<br />' . mysql_error();
    }
}

function exist()
{
       $num_rows = mysql_num_rows($result);

       if($num_rows<=0)
       {
          die('No Username in database');
        }
       elseif($num_rows>1)
       {
        die('Cannot enter more then one username!');
       }
}

function check() 
{ 
      mysql_select_db("divnx5_web");
      $result = "SELECT `username` FROM `users` WHERE `username` = '$username'";
      $error = mysql_query($result) OR die(mysql_error());
   
      while($row = mysql_fetch_array($result))
      {
         echo "The username " . $username . 'is already taken!';
       }
}

if(!$con)
{
  echo "<br /><br />Registering is disabled right now, please check back later.";
}
else
{
  if(!$_POST['register'])
     {
       echo "<br /><br />Use the following form to register a new account with us.
   <form action='register.php' method='post'><br /><br />
   <font size='2'>Username:</font><br />
   <input type='text' id='user' name='user' size='20' style='background-color: #FFFFFF; font-size: 8pt; border: 1 solid #003399' /><br />
   <font size='2'>Password:</font><br />
   <input type='password' id='pass' name='pass' size='20' style='background-color: #FFFFFF; font-size: 8pt; border: 1 solid #003399' /><br />
   <font size='2'>Confirm Password:</font><br />
   <input type='password' id='pass2' name='pass2' size='20' style='background-color: #FFFFFF; font-size: 8pt; border: 1 solid #003399' /><br />
   <font size='2'>E-mail Address:</font><br />
   <input type='text' id='email' name='email' size='20' style='background-color: #FFFFFF; font-size: 8pt; border: 1 solid #003399' /><br /><br />
   <input type='submit' name='register' id='register' value='Register' style='background-color: #FFFFFF; color: #000000; font-size: 8pt; border: 1 solid #003399' />
   <input type='reset' value='Clear' style='background-color: #FFFFFF; color: #000000; font-size: 8pt; border: 1 solid #003399' />
   </form>";
     }
  else
     {
         if(!$username)
           echo "The username you entered encountered a problem.<br /><br />";
           exist();
           check();

        if(!$password || !$password2)
           echo "The password field cannot be left empty!<br /><br />";

        if(!$email)
           echo "The email you entered encountered a problem.<br /><br />";

        if($password != $password2)
           echo "The passwords you entered do not match.<br /><br />";

        else
        {
            mysql_select_db("divnx5_web");
            mysql_query("INSERT INTO users(username, password, email) VALUES('$username', '$password', '$email')");
            error();
            mysql_close($con);
            echo "Thank you for registering with us " . $username . '! Enjoy your stay!';
       }
   }
}
?>

Link to comment
Share on other sites

try this...

 

<?php 
require("connect.php");

$username = $_POST['user'];
$password = sha1($_POST['pass']);
$password2 = sha1($_POST['pass2']);
$email = $_POST['email'];

function error()
{
   if(!$query)
   {
      echo "Query Failed: " . mysql_errno() . '<br />' . mysql_error();
    }
}

function exist()
{
       global $result;
       $num_rows = mysql_num_rows($result);

       if($num_rows<=0)
       {
          die('No Username in database');
        }
       elseif($num_rows>1)
       {
        die('Cannot enter more then one username!');
       }
}

function check() 
{
      global $result;
      mysql_select_db("divnx5_web");
      $result = "SELECT `username` FROM `users` WHERE `username` = '$username'";
      $error = mysql_query($result) OR die(mysql_error());
   
      while($row = mysql_fetch_array($result))
      {
         echo "The username " . $username . 'is already taken!';
       }
}

if(!$con)
{
  echo "<br /><br />Registering is disabled right now, please check back later.";
}
else
{
  if(!$_POST['register'])
     {
       echo "<br /><br />Use the following form to register a new account with us.
   <form action='register.php' method='post'><br /><br />
   <font size='2'>Username:</font><br />
   <input type='text' id='user' name='user' size='20' style='background-color: #FFFFFF; font-size: 8pt; border: 1 solid #003399' /><br />
   <font size='2'>Password:</font><br />
   <input type='password' id='pass' name='pass' size='20' style='background-color: #FFFFFF; font-size: 8pt; border: 1 solid #003399' /><br />
   <font size='2'>Confirm Password:</font><br />
   <input type='password' id='pass2' name='pass2' size='20' style='background-color: #FFFFFF; font-size: 8pt; border: 1 solid #003399' /><br />
   <font size='2'>E-mail Address:</font><br />
   <input type='text' id='email' name='email' size='20' style='background-color: #FFFFFF; font-size: 8pt; border: 1 solid #003399' /><br /><br />
   <input type='submit' name='register' id='register' value='Register' style='background-color: #FFFFFF; color: #000000; font-size: 8pt; border: 1 solid #003399' />
   <input type='reset' value='Clear' style='background-color: #FFFFFF; color: #000000; font-size: 8pt; border: 1 solid #003399' />
   </form>";
     }
  else
     {
         if(!$username)
           echo "The username you entered encountered a problem.<br /><br />";
           exist();
           check();

        if(!$password || !$password2)
           echo "The password field cannot be left empty!<br /><br />";

        if(!$email)
           echo "The email you entered encountered a problem.<br /><br />";

        if($password != $password2)
           echo "The passwords you entered do not match.<br /><br />";

        else
        {
            mysql_select_db("divnx5_web");
            mysql_query("INSERT INTO users(username, password, email) VALUES('$username', '$password', '$email')");
            error();
            mysql_close($con);
            echo "Thank you for registering with us " . $username . '! Enjoy your stay!';
       }
   }
}
?>

Link to comment
Share on other sites

You're having problems with variable scope. You can use the solution benjaminbeazy has proposed or pass the variables into the functions, which is my preferred solution. Something like this:

<?php 
require("connect.php");
$db = mysql_select_db("divnx5_web");

$username = $_POST['user'];
$password = sha1($_POST['pass']);
$password2 = sha1($_POST['pass2']);
$email = $_POST['email'];

function error($result, $query)
{
   if(!$result)
   {
      echo "Query Failed: $query<br />" . mysql_errno() . '<br />' . mysql_error();
    }
}

function exist($result)
{
       $num_rows = mysql_num_rows($result);

       if($num_rows<=0)
       {
          die('No Username in database');
        }
       elseif($num_rows>1)
       {
        die('Cannot enter more then one username!');
       }
}

function check($username) 
{ 
      $query = "SELECT `username` FROM `users` WHERE `username` = '$username'";
	$result = mysql_query($query) or die("Problem with the query:$query<br />" . mysql_error());
	if (mysql_num_rows($result) > 0)
         echo "The username " . $username . 'is already taken!';
}

if(!$con)
{
  echo "<br /><br />Registering is disabled right now, please check back later.";
}
else
{
  if(!$_POST['register'])
     {
       echo "<br /><br />Use the following form to register a new account with us.
   <form action='register.php' method='post'><br /><br />
   <font size='2'>Username:</font><br />
   <input type='text' id='user' name='user' size='20' style='background-color: #FFFFFF; font-size: 8pt; border: 1 solid #003399' /><br />
   <font size='2'>Password:</font><br />
   <input type='password' id='pass' name='pass' size='20' style='background-color: #FFFFFF; font-size: 8pt; border: 1 solid #003399' /><br />
   <font size='2'>Confirm Password:</font><br />
   <input type='password' id='pass2' name='pass2' size='20' style='background-color: #FFFFFF; font-size: 8pt; border: 1 solid #003399' /><br />
   <font size='2'>E-mail Address:</font><br />
   <input type='text' id='email' name='email' size='20' style='background-color: #FFFFFF; font-size: 8pt; border: 1 solid #003399' /><br /><br />
   <input type='submit' name='register' id='register' value='Register' style='background-color: #FFFFFF; color: #000000; font-size: 8pt; border: 1 solid #003399' />
   <input type='reset' value='Clear' style='background-color: #FFFFFF; color: #000000; font-size: 8pt; border: 1 solid #003399' />
   </form>";
     }
  else
     {
         if(!$username)
           echo "The username you entered encountered a problem.<br /><br />";
           exist();
           check($username);

        if(!$password || !$password2)
           echo "The password field cannot be left empty!<br /><br />";

        if(!$email)
           echo "The email you entered encountered a problem.<br /><br />";

        if($password != $password2)
           echo "The passwords you entered do not match.<br /><br />";

        else
        {
	  		$query = "INSERT INTO users(username, password, email) VALUES('$username', '$password', '$email')";
            $result = mysql_query($query);
            error($result, $query);
            mysql_close($con);
            echo "Thank you for registering with us " . $username . '! Enjoy your stay!';
       }
   }
}
?>

 

I tried to fix it as best I could, but your logic doesn't make any sense in many places.

 

Ken

Link to comment
Share on other sites

Kenrbnsn, your way worked, sort of. It finally says username is taken, but if I leave other fields blank or enter random stuff, then enter a username that isn't taken. It adds the username to the database, even though the passwords or email fields might have problems.

 

I don't see any problems with my logic on that part.  ???

 

Thanks

Link to comment
Share on other sites

<?php

if(!$password || !$password2||!isset($password)||!isset($password2)||empty($password)||empty($password2))
           echo "The password field cannot be left empty!<br /><br />";


if(!isset($username)||empty($username)) {


echo "The Username was not filled in!<br /><br />";
}


?>

Link to comment
Share on other sites

<?php

if(!$password || !$password2||!isset($password)||!isset($password2)||empty($password)||empty($password2))
           echo "The password field cannot be left empty!<br /><br />";


if(!isset($username)||empty($username)) {


echo "The Username was not filled in!<br /><br />";
}


?>

 

That helped out a lot, but it still has some problems. I was messing around with the form so test all conditions.

 

I entered a username that was taken, both passwords correctly and an empty email field.

 

It came back saying, username is taken, email field was empty, but it ended up adding the username to the db again, even though it was taken and the email field was empty.  ???

 

Edit: Also if I only enter a username, and leave all other fields blank, it ends up adding the username to the db with the other fields empty. x.X

Link to comment
Share on other sites

change your  logic man change :

 

<?php


} else {//insert }?>

 

 

to:

<?php
elseif(isset($username)||isset($password)||isset($password2)||!empty($username)||!empty($password)||!empty($password2)
||!empty($email)||isset($email)) { //insert

}?>

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.