Jump to content

trouble with user registration


matthew798

Recommended Posts

This entire script is for managing user registration.... It is SUPPOSED to return the following when somebody successfully registers:

 

"User registration successful!

 

Your userid and password have been emailed to , the email address you just provided in your registration form. To log in, click here to return to the login page, and enter your new personal userid and password."

 

instead, it displays nothing, and does not enter ANY of the info into the mySQL database...

 

<?php // signup.php 
include 'common.php'; 
include 'db.php';

if (!isset($_POST['submitok'])): 
   // Display the user signup form 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>New User Registration</title>
</head>
<body>
   <?php 
else: 
   // Process signup submission 
   dbConnect('sessions');
   
      if ($_POST['username']=='' or $_POST['password']=='' 
     or $_POST['email']=='') { 
       error('One or more required fields were left blank.\\n'. 
             'Please fill them in and try again.'); 
   }
   
      // Check for existing user with the new id 
   $sql = "SELECT COUNT(*) FROM user WHERE userid = '$_POST[username]'"; 
   $result = mysql_query($sql); 
   if (!$result) { 
       error('A database error occurred in processing your '. 
             'submission.\\nIf this error persists, please '. 
             'contact matthew.goulart@gmail.com.'); 
   } 
   if (@mysql_result($result,0,0)>0) { 
       error('A user already exists with your chosen userid.\\n'. 
             'Please try another.'); 
   }
   
      $sql = "INSERT INTO user SET 
             username = '$_POST[username]', 
             password = '$_POST[password], 
             email = '$_POST[email]', 
             country = '$_POST[country]', 
             province = '$_POST[province]'"; 
   if (!mysql_query($sql)) 
       error('A database error occurred in processing your '. 
             'submission.\\nIf this error persists, please '. 
             'contact matthew.goulart@gmail.com.');



// Email the new password to the person. 
   $message = "G'Day! 

Your personal account for the Project Web Site 
has been created! To log in, proceed to the 
following address: 

   http://www.example.com/ 

Your personal login ID and password are as 
follows: 

   userid: $_POST[username] 
   password: $_POST[password]

You aren't stuck with this password! Your can 
change it at any time after you have logged in. 

If you have any problems, feel free to contact me at 
<you@example.com>. 

-Your Name 
Your Site Webmaster 
"; 

   mail($_POST['newemail'],"Your Password for Your Website", 
        $message, "From:Your Name <you@example.com>");
?>


   <p><strong>User registration successful!</strong></p> 
   <p>Your userid and password have been emailed to 
      <strong><?php $_POST[email] ?></strong>, the email address 
      you just provided in your registration form. To log in, 
      click <a href="index.php">here</a> to return to the login 
      page, and enter your new personal userid and password.</p> 
   <?php 
endif; 
?>
</body>
</html>

 

here are the two included files...

 

common.php

 

<?php // common.php 

function error($msg) { 
   ?> 
   <html> 
   <head> 
   <script language="JavaScript [10]"> 
   <!-- 
       alert("<?php $msg ?>"); 
       history.back(); 
   //--> 
   </script> 
   </head> 
   <body> 
   </body> 
   </html> 
   <?php 
   exit; 
} 
?>

 

db.php

 


<?php // db.php 

$dbhost = "localhost"; 
$dbuser = "****"; 
$dbpass = "****"; 

function dbConnect($db="") { 
   global $dbhost, $dbuser, $dbpass; 
    
   $dbcnx = @mysql_connect($dbhost, $dbuser, $dbpass) 
       or die("The site database appears to be down."); 

   if ($db!="" and !@mysql_select_db($db)) 
       die("The site database is unavailable."); 
    
   return $dbcnx; 
} 
?>

 

P.S. I starred out the user and pass...

 

If anyone could tell me why it's not returning anything or writing anything to the databse... I know for a fact that the database info is correct and that the table does infact exist.

 

Thanks in advance!!!

 

Link to comment
Share on other sites

Not sure if that is causing the problem, but this:

 

$sql = "SELECT COUNT(*) FROM user WHERE userid = '$_POST[username]'";

 

should be:

<?php
$sql = "SELECT COUNT(*) FROM user WHERE userid = '{$_POST['username']}'";
//or
$sql = "SELECT COUNT(*) FROM user WHERE userid = '" . $_POST['username'] . "'";
?>

 

And that issue is everywhere in the queries. Not mentioning that you must clean your input by escaping special characters with mysql_real_escape_string().

 

PS. I didnt know that an if() else could be written as if(): else:

Link to comment
Share on other sites

Not sure if that is causing the problem, but this:

 

$sql = "SELECT COUNT(*) FROM user WHERE userid = '$_POST[username]'";

 

should be:

<?php
$sql = "SELECT COUNT(*) FROM user WHERE userid = '{$_POST['username']}'";
//or
$sql = "SELECT COUNT(*) FROM user WHERE userid = '" . $_POST['username'] . "'";
?>

 

And that issue is everywhere in the queries. Not mentioning that you must clean your input by escaping special characters with mysql_real_escape_string().

 

PS. I didnt know that an if() else could be written as if(): else:

 

Never knew it either o_o It seems ugly.. I'd use semicolons just for switch/case/break statements.

Link to comment
Share on other sites

first thing I personally see wrong is that you are using a colon instead of brackets with your if...else statement.  Colons are used for executing just one expression but you have multiple expressions (multiple lines of code to be executed) so they need to be wrapped in brackets

 

if (condition) {

 // code here

} else {

 // code here

}

Link to comment
Share on other sites

You need endif; too, GuiltyGear.  You can also use it for while, foreach, for, etc.  Look up the syntax in the PHP manual.  I think it's ugly though.

 

I see. Personally I'm pretty fine with curly braces, as that [if; else; endif;] thing reminds me of Visual Basic  >:(

Link to comment
Share on other sites

well i have tried all of you suggestions to no avail... Except for the one about curly brackets instead of semi-colons... I couldn't find any in the script... (???)

 

It would also be important to note that this script is entirely taken from http://www.sitepoint.com/print/users-php-sessions-mysql

 

I changed it around a little bit to suit my needs but nothing major and i havn't changed the syntax of anything...

 

here is my code so far, although still not working...

 

***NEWUS.PHP***

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
?>

<?php // signup.php 

include 'common.php'; 
include 'db.php';

if (!isset($_POST['submitok'])): 
   // Display the user signup form 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>New User Registration</title>
</head>
<body>
   <?php 
else: 
   // Process signup submission 
   dbConnect('users');
   
      if ($_POST['username']=='' or $_POST['password']=='' 
     or $_POST['email']=='') { 
       error('One or more required fields were left blank.\\n'. 
             'Please fill them in and try again.'); 
   }
   
      // Check for existing user with the new id 
   $sql = "SELECT COUNT(*) FROM users WHERE username = '{$_POST['username']}'"; 
   $result = mysql_query($sql); 
   if (!$result) { 
       error('A database error occurred in processing your '. 
             'submission.\\nIf this error persists, please '. 
             'contact matthew.goulart@gmail.com.');
		 die("The site database is unavailable."); 
   } 
   if (@mysql_result($result,0,0)>0) { 
       error('A user already exists with your chosen userid.\\n'. 
             'Please try another.');
		 die("The site database is unavailable."); 
   }
   
      $sql = "INSERT INTO user SET 
             username = '$_POST[username]', 
             password = '$_POST[password], 
             email = '$_POST[email]', 
             country = '$_POST[country]', 
             province = '$_POST[province]'"; 
		 die("The site database is unavailable.");
   if (!mysql_query($sql)) 
       error('A database error occurred in processing your '. 
             'submission.\\nIf this error persists, please '. 
             'contact matthew.goulart@gmail.com.');
		 die("The site database is unavailable."); 



// Email the new password to the person. 
   $message = "G'Day! 

Your personal account for the Project Web Site 
has been created! To log in, proceed to the 
following address: 

   http://www.example.com/ 

Your personal login ID and password are as 
follows: 

   userid: $_POST[username] 
   password: $_POST[password]

You aren't stuck with this password! Your can 
change it at any time after you have logged in. 

If you have any problems, feel free to contact me at 
<you@example.com>. 

-Your Name 
Your Site Webmaster 
"; 

   mail($_POST['newemail'],"Your Password for Your Website", 
        $message, "From:Your Name <you@example.com>");
?>


   <p><strong>User registration successful!</strong></p> 
   <p>Your userid and password have been emailed to 
      <strong><?php $_POST[email] ?></strong>, the email address 
      you just provided in your registration form. To log in, 
      click <a href="index.php">here</a> to return to the login 
      page, and enter your new personal userid and password.</p> 
   <?php 
endif; 
?>
</body>
</html>

***COMMON.PHP***

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
?>

<?php // common.php 


function error($msg) { 
   ?> 
   <html> 
   <head> 
   <script language="JavaScript [10]"> 
   <!-- 
       alert("<?php $msg ?>"); 
       history.back(); 
   //--> 
   </script> 
   </head> 
   <body> 
   </body> 
   </html> 
   <?php 
   exit; 
} 
?>

***DB.PHP***

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
?>

<?php // db.php 


$dbhost = "localhost"; 
$dbuser = "admin"; 
$dbpass = "1admin"; 

function dbConnect($db="users") { 
   global $dbhost, $dbuser, $dbpass; 
    
   $dbcnx = mysql_connect($dbhost, $dbuser, $dbpass) 
       or die("The site database appears to be down."); 

   if ($db!="users" and !mysql_select_db($db)) 
       die("The site database is unavailable."); 
    
   return $dbcnx; 
} 
?>

 

P.S. i spearated the three files with stars and big letters so my post wouldnt be so cluttered...

 

oh yeah, and those error reporting lines i added aren't doing anything... (also important to note, i started PHP yesterday. I know a good bit of C++ and heard it was similar so i decided to give it a whirl. Also, i'm learning it so i can offer it as a course in my local business i run :)!!! )

Link to comment
Share on other sites

if (!isset($_POST['submitok'])): 

   // Display the user signup form

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>New User Registration</title>

</head>

<body>

   <?php

else:

   // Process signup submission

   dbConnect('users');

.

.

.

Link to comment
Share on other sites

There's no such thing as endif in php. The brackets are what signify the beginning and end of the code blocks to be executed.  In my first post in this thread I pointed out that he is using a colon instead of brackets.  Well colons can be used if you are only going to be executing 1 line of code, but he's not, so he needs to wrap the code blocks in brackets (see my first post).  He said he didn't see any colons, so I pointed them out.  I don't know whether or not this will completely fix his problem but I do know that that's at least one thing wrong with his code.  I didn't look any further into it than that.

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.