Jump to content


Photo

Help with this code!!


  • Please log in to reply
8 replies to this topic

#1 techiefreak05

techiefreak05
  • Members
  • PipPipPip
  • Advanced Member
  • 494 posts
  • LocationER, MN

Posted 14 August 2006 - 03:50 PM

Hi, I have a registration page on my site and I wanted to expand it. i want to add a First Name field.. I added the field in the database and table, I jsut dont know how to add the contents into the database...

<? 
/* Include Files *********************/
session_start();
include("login.php");
include("func.php");
/*************************************/
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- DW6 -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>ZycoWorld</title>
<link rel="stylesheet" href="files/2col_rightNav.css" type="text/css">
</head>
<body> 
<div id="masthead"> 
  <h1 id="siteName"><img src=files/zyLogo1.jpg width="500" height="100"></h1> 
  <div id="globalNav"> 
<?php DisplayLinks(); ?>
  </div> 
</div> 
  <span class="relatedLinks">

  <?php
if($logged_in){
headerLinks();
}
?>

</span>
<!-- end masthead --> 
<div id="content"> 
  <h2 id="pageName">Regsiter</h2> 
  <div class="feature"> 
    <h3>Get a ZycoWorld Account today!</h3>
    <p>
<! - REGISTER CODE HERE - >
<?
/**
 * 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);
}
/**
 * Returns true if the email address is already
 * in use by another user, false otherwise.
 */

function emailUsed($email){
   global $conn;
   if(!get_magic_quotes_gpc()){
      $email = addslashes($email);
   }
   $q = "select email from users where email = '$email'";
   $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.
 */
$usertold = addslashes($usertold);

function addNewUser($username, $password, $email, $usertold){
   global $conn;
   $q = "INSERT INTO users VALUES ('$username', '$password', '$email', '$usertold')";
   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']){
?>
<meta http-equiv="refresh" content="5;url=main.php">
<body bgcolor=#FOE68C>
<h1>Registered!</h1>
<p>Thank you <b><? echo $uname; ?></b>, your information has been added to the database, you will be redirected to HOME in about 1 second.</p>

<?
   }
   else{
?>
<meta http-equiv="refresh" content="1;url=register.php">
<body bgcolor=#FOE68C>
<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><p><A HREF=register.php>Try Again</a> You will be redirected to REGISTER in about 1 second.

<?
   }
   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>
<body>

<? 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'] || !$_POST['email'] || !$_POST['passcon']){
      die('You didn\'t fill in a required field.');
   }

if($_POST['pass'] <> $_POST['passcon']){
die('Error, your password didn\'t match.');
}

   /* 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.");
   }

/* Check if email is already in use */
   if(emailUsed($_POST['email'])){
      $email = $_POST['email'];
      die("Sorry, the email address (<i>{$email}</i>) provided is already in use");
   }

   /* Add the new account to the database */
   $md5pass = md5($_POST['pass']); 
   $_SESSION['reguname'] = $_POST['user'];
$_SESSION['regresult'] = addNewUser($_POST['user'], $md5pass, $_POST['email'], $_POST['usertold']);
   $_SESSION['registered'] = true;
$ourFileName = "$_SESSION[reguname].html";
$html = file_get_contents("");
$file = fopen($ourFileName, "w+");
fwrite($file, $html);
fclose($file);
   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.
 */
?>
<form action="<? echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post">
<table align="center" border="0" cellspacing="0" cellpadding="3">
<tr><td>First Name:</td><td><input type="text" name="fname" maxlength="10"></td></tr>
<tr><td>Username:*</td><td><input type="text" name="user" maxlength="30"></td></tr>
<tr><td>Password:*</td><td><input type="password" name="pass" maxlength="30"></td></tr>
<tr><td>Re-type Password:*</td><td><input type="password" name="passcon" maxlength="30"></td></tr>
<tr><td>Email:*</td><td><input type="text" name="email" maxlength="50"></td></tr>
<tr><td><hr width=25%></td></tr>
<tr><td>Did a User tell you about this site? If so, please enter their username:</td><td><input type="text" name="usertold" maxlength="30"></td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="subjoin" value="Join!"></td></tr>
</table>
</form>
<div align=center>
*= Required field.
</div>
</body>
</html>


<?
}
?>
<! - END REG CODE - >
</p>
  </div> 
  <div class="story"> 
    <h3></h3> 
    <p></p> 
    <h3></h3>
	<p>
 </p> 
  </div> 
  <div class="story"> 
    <h3></h3> 
    <p>
</p> 
    <p> 
    </p> 
  </div> 
</div> 
<!--end content --> 

<?php
if($logged_in){
 navBar();
}else{
loginBar();
}
?>
<!--end navBar div --> 
<?php siteInfo(); ?>
<br> 
</body>
</html>

I tried smoem things but it didnt work .. I tried taking all the "$usertold" things and copying them and changing them to $fname .. but I kept getting either "unexpected T_VARIBLE" or T_STRING
Link shortener with advanced, detailed statistics:

http://tyny.us/

#2 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 14 August 2006 - 04:00 PM

Try:

<? 
/* Include Files *********************/
session_start();
include("login.php");
include("func.php");
/*************************************/
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- DW6 -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>ZycoWorld</title>
<link rel="stylesheet" href="files/2col_rightNav.css" type="text/css">
</head>
<body> 
<div id="masthead"> 
  <h1 id="siteName"><img src=files/zyLogo1.jpg width="500" height="100"></h1> 
  <div id="globalNav"> 
<?php DisplayLinks(); ?>
  </div> 
</div> 
  <span class="relatedLinks">

  <?php
if($logged_in){
headerLinks();
}
?>

</span>
<!-- end masthead --> 
<div id="content"> 
  <h2 id="pageName">Regsiter</h2> 
  <div class="feature"> 
    <h3>Get a ZycoWorld Account today!</h3>
    <p>
<! - REGISTER CODE HERE - >
<?
/**
 * 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);
}
/**
 * Returns true if the email address is already
 * in use by another user, false otherwise.
 */

function emailUsed($email){
   global $conn;
   if(!get_magic_quotes_gpc()){
      $email = addslashes($email);
   }
   $q = "select email from users where email = '$email'";
   $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.
 */
$usertold = addslashes($usertold);

function addNewUser($username, $password, $email, $usertold, $fname){
   global $conn;
   $q = "INSERT INTO users VALUES ('$username', '$password', '$email', '$usertold', '$fname')";
   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']){
?>
<meta http-equiv="refresh" content="5;url=main.php">
<body bgcolor=#FOE68C>
<h1>Registered!</h1>
<p>Thank you <b><? echo $uname; ?></b>, your information has been added to the database, you will be redirected to HOME in about 1 second.</p>

<?
   }
   else{
?>
<meta http-equiv="refresh" content="1;url=register.php">
<body bgcolor=#FOE68C>
<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><p><A HREF=register.php>Try Again</a> You will be redirected to REGISTER in about 1 second.

<?
   }
   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>
<body>

<? 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'] || !$_POST['email'] || !$_POST['passcon']){
      die('You didn\'t fill in a required field.');
   }

if($_POST['pass'] <> $_POST['passcon']){
die('Error, your password didn\'t match.');
}

   /* 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.");
   }

/* Check if email is already in use */
   if(emailUsed($_POST['email'])){
      $email = $_POST['email'];
      die("Sorry, the email address (<i>{$email}</i>) provided is already in use");
   }

   /* Add the new account to the database */
   $md5pass = md5($_POST['pass']); 
   $_SESSION['reguname'] = $_POST['user'];
$_SESSION['regresult'] = addNewUser($_POST['user'], $md5pass, $_POST['email'], $_POST['usertold'], $_POST['fname']);
   $_SESSION['registered'] = true;
$ourFileName = "$_SESSION[reguname].html";
$html = file_get_contents("");
$file = fopen($ourFileName, "w+");
fwrite($file, $html);
fclose($file);
   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.
 */
?>
<form action="<? echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post">
<table align="center" border="0" cellspacing="0" cellpadding="3">
<tr><td>First Name:</td><td><input type="text" name="fname" maxlength="10"></td></tr>
<tr><td>Username:*</td><td><input type="text" name="user" maxlength="30"></td></tr>
<tr><td>Password:*</td><td><input type="password" name="pass" maxlength="30"></td></tr>
<tr><td>Re-type Password:*</td><td><input type="password" name="passcon" maxlength="30"></td></tr>
<tr><td>Email:*</td><td><input type="text" name="email" maxlength="50"></td></tr>
<tr><td><hr width=25%></td></tr>
<tr><td>Did a User tell you about this site? If so, please enter their username:</td><td><input type="text" name="usertold" maxlength="30"></td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="subjoin" value="Join!"></td></tr>
</table>
</form>
<div align=center>
*= Required field.
</div>
</body>
</html>


<?
}
?>
<! - END REG CODE - >
</p>
  </div> 
  <div class="story"> 
    <h3></h3> 
    <p></p> 
    <h3></h3>
	<p>
 </p> 
  </div> 
  <div class="story"> 
    <h3></h3> 
    <p>
</p> 
    <p> 
    </p> 
  </div> 
</div> 
<!--end content --> 

<?php
if($logged_in){
 navBar();
}else{
loginBar();
}
?>
<!--end navBar div --> 
<?php siteInfo(); ?>
<br> 
</body>
</html>


#3 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 14 August 2006 - 04:01 PM

Also you did make the "fname" field in the "users" table right?

#4 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 14 August 2006 - 04:03 PM

A summary of hostfreak's code changes, as techiefreak05 posted a lot of code to look through... ;)

[code=php:0]

<?php
// Code above function
//...

function addNewUser($username, $password, $email, $usertold, $fname){
   global $conn;
   $q = "INSERT INTO users VALUES ('$username', '$password', '$email', '$usertold', '$fname')";
   return mysql_query($q,$conn);
}

// Code below function
// ...

// Call to the addNewUser() function
$_SESSION['regresult'] = addNewUser($_POST['user'], $md5pass, $_POST['email'], $_POST['usertold'], $_POST['fname']);

// The rest of your code
// ...
?>

[/code]


#5 techiefreak05

techiefreak05
  • Members
  • PipPipPip
  • Advanced Member
  • 494 posts
  • LocationER, MN

Posted 14 August 2006 - 04:06 PM

Yes, the fname field is in the users table .. its the very first column
Link shortener with advanced, detailed statistics:

http://tyny.us/

#6 techiefreak05

techiefreak05
  • Members
  • PipPipPip
  • Advanced Member
  • 494 posts
  • LocationER, MN

Posted 14 August 2006 - 04:07 PM

hostfreak:
The code you posted doesnt work .. i get the "Registration Failed" page.
Link shortener with advanced, detailed statistics:

http://tyny.us/

#7 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 14 August 2006 - 04:57 PM

The query you have in your addNewUser() function is field order specific, meaning you have to order the values you insert in the order they appear in the database. Try changing your query to be like this:

[code=php:0]
<?php

// Code before function
// ....

function addNewUser($username, $password, $email, $usertold, $fname){
   global $conn;
   $q = "INSERT INTO users SET username = '$username', password = '$password', email = '$email', usertold = '$usertold', fname = '$fname'";
   return mysql_query($q,$conn) or die("Error adding user: ".mysql_error());
}

// Code after function
// ...

?>
[/code]

I also added a die() after your query. Try adding this code above and post any errors you get. Once we've figured out your problem we can remove the die() so users will get correctly directed to the "Registration Failed" page.

#8 techiefreak05

techiefreak05
  • Members
  • PipPipPip
  • Advanced Member
  • 494 posts
  • LocationER, MN

Posted 14 August 2006 - 05:00 PM

That worked!! Thanks everyone!
Link shortener with advanced, detailed statistics:

http://tyny.us/

#9 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 14 August 2006 - 05:01 PM

Glad we could help... ;)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users