Jump to content

Help with this code!!


techiefreak05

Recommended Posts

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...

[code]<?
/* 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>
[/code]

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 to comment
Share on other sites

Try:

[code]
<?
/* 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>
[/code]
Link to comment
Share on other sites

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

[code]
[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]
[/code]
Link to comment
Share on other sites

The query you have in your [b]addNewUser()[/b] 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]
[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]
[/code]

I also added a [b]die()[/b] 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 [b]die()[/b] so users will get correctly directed to the "Registration Failed" page.
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.