**RESOLVED** Problem successfully submitting form

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
mikes1471
Forum Commoner
Posts: 88
Joined: Sat Jan 24, 2009 3:29 pm

**RESOLVED** Problem successfully submitting form

Post by mikes1471 »

Hi Guys

I have a registration code I'm working on at the moment and have got to a point where I dont understand how to resolve the error (not the most experienced with PHP) and I was wondering if someone could help me see the error of my ways?

My error is Fatal error: Call to undefined function escape_data() in /var/www/vhosts/(website).com/httpdocs/register.php on line 56

The code is as follows:

Code: Select all

<?php # register.php
include_once "functions.php";
 
connect();
 
$valid_email = FALSE;
$valid_pswd  = FALSE;
$scroll_message = 'Registration Page.';
$error_message  = '';
$fname     = ''; $lname    = ''; $email  = ''; $cemail   = '';
$sexuality = ''; $bday     = 0 ; $bmonth = ''; $byear    = 0 ;
$street    = ''; $postcode = ''; $city   = ''; $username = '';
$pswd1     = ''; $pswd2    = ''; $sex    = ''; $country  = '';
 
 
function validate_username($usrnme)
{
  $sql_select = "SELECT count(username) as usr_cnt
                   FROM clients
                  WHERE username = '$usrnme';";
 
  $result      = @mysql_query ($sql_select);
  $row         = mysql_fetch_array($result, MYSQL_ASSOC);
  $maxnum      = $row['usr_cnt'];
 
  if ($maxnum == 0)
    $ans = 'notused';
  else
    $ans = 'used';
 
  mysql_free_result ($result);
 
  return $ans;
}
 
//Check if the form has been submitted
if (isset($_POST['submit']))
{
  //create error message array.
  $error = '';
 
  
 
  //initialize working variables
  $fname     = escape_data($_POST['firstname']);
  $lname     = escape_data($_POST['lastname']);
  $email     = escape_data($_POST['email']);
  $cemail    = escape_data($_POST['cemail']);
  $sex       = escape_data($_POST['sex']);
  $sexuality = escape_data($_POST['sexuality']);
  $bday      = escape_data($_POST['bday']);
  $bmonth    = escape_data($_POST['bmonth']);
  $byear     = escape_data($_POST['byear']);
  $street    = escape_data($_POST['street']);
  $postcode  = escape_data($_POST['postcode']);
  $city      = escape_data($_POST['city']);
  $country   = escape_data($_POST['country']);
  $username  = escape_data($_POST['username']);
  $pswd1     = escape_data($_POST['pswd_one']);
  $pswd2     = escape_data($_POST['pswd_two']);
 
 
  // Check first name.
  if (empty($fname))
    $error = 'You must to enter your First Name';
 
  // Check last name.
  if (empty($lname))
    $error = 'You forgot to enter your Last Name.';
 
  //Check for valid email address
  if (!empty($email))
  {
    if ($email != $cemail)
      $error = 'Emails do not match!';
  }
  else
    $error = 'You forgot to enter your email address!';
 
  // Check sex.
  if (empty($sex))
    $error = 'You must enter your Sex!';
 
  // Check sexuality.
  if (empty($sexuality))
    $error = 'You must enter your Sexuality!';
  else
  {
    if ($sexuality == 'Straight')
    {
      $sexuality = 'st';
    }
    else if ($sexuality == 'Open Minded')
    {
      $sexuality = 'om';
    }
    else if ($sexuality == 'Bisexual')
    {
      $sexuality = 'bi';
    }
    else if ($sexuality == 'Gay_Lesbian')
    {
      $sexuality = 'gl';
    }
    else if ($sexuality == 'Other')
    {
      $sexuality = 'ot';
    }
  }
 
  // Check street.
  if (empty($street))
    $error = 'You must enter your House or Street Number!';
 
  // Check city.
  if (empty($city))
    $error = 'You must enter your City!';
 
  // Check postal code.
  if (empty($postcode))
    $error = 'You must enter your Postal Code!';
 
  // Check country.
  if (empty($country))
    $error = 'You must enter your home Country!';
 
  // Check username.
  if (empty($username))
    $error = 'You must enter a Username!';
  else
  {
    $username_available = validate_username($username);
 
    if ($username_available == 'used')
      $error = 'Username ' . $username. ' not available!, Please re-enter';
  }
 
  //Check for password and match against the confirmed password
  if (!empty($pswd1))
  {
    if ($pswd1 != $pswd2)
      $error = 'Passwords do not match.';
  }
  else
  {
    $error = 'You forgot to enter your password.';
  }
 
  //check error array
  if (empty($error))
  {
    $scroll_message = "Successfully Registered";
 
    $sql_insert= "INSERT INTO clients VALUES (
                     null, '$fname',
                    '$lname','$username',
                    PASSWORD('$pswd1')
                  );";
 
    $result = @mysql_query ($sql_insert); // Run the query.
 
    if (mysql_affected_rows() != 1)
    {
      echo '<p>Problems with client table data insert: ' . mysql_error() . '<br /><br />Query: ' . $sql_insert . '</p>';
      mysql_close();  // Close data connection
      exit();
    }
 
    $sql_select  = "SELECT MAX(client_num) AS maxvalue FROM clients;";
    $result      = @mysql_query ($sql_select);
    $row         = mysql_fetch_array($result, MYSQL_ASSOC);
    $maxnum      = $row['maxvalue'];
 
    $sql_insert = "INSERT INTO client_details VALUES (
                      $maxnum, '$sex',
                     '$sexuality', $bday,
                     '$bmonth', $byear,
                     '$street', '$city',
                     '$country', '$postcode',
                     '$email'
                   );";
 
    $result = @mysql_query($sql_insert);
 
    if (mysql_affected_rows() != 1)
    {
      echo '<p>Problem with client_details  table insert: ' . mysql_error() . '<br /><br />Query: ' . $sql_insert . '</p>';
      mysql_close();  // Close data connection
      exit();
    }
    else
    {
      $body ="Thank-you for registering with the PicFrisky!  \n
              Your id is           '{$_POST['']}'. \n
              Your registered password is '{$_POST['password']}'.\n
              Your registered email is    '{$_POST['email']}'.\n
              Keep this email handy.\n\n
              Sincerely,\n
              Mike Hockinson, Site Administrator";
      $success = 'You are now registered.  An email has been sent to your email account confirming the information.';
      mail($e, 'Thank for registering!', $body , 'From: <insert your site email here',$success);
      $error = '';  // flush error messages.
    }
  }
  else
  {
    $scroll_message = "There are problems with your registration!";
    $error_message = $error;
  }
 
  mysql_close();  // Close data connection
}
 
?>
 
  <form name="register" method="POST" action="register.php">
  <table width="442" border="0">
    <tr>
      <td width="197"><b><font color="#333333">First Name</font></b></td>
      <td width="235"><input type="text" name="firstname" size="25" maxlength="25" value="<?php echo ($fname); ?>" /></td>
    </tr>
    <tr>
      <td><b><font color="#333333">Last Name</font></b></td>
      <td><input type="text" name="lastname" size="25" maxlength="35" value="<?php echo ($lname); ?>" /></td>
    </tr>
    <tr>
      <td><b><font color="#333333">Your Email Address</font></b></td>
      <td><input type="text" name="email" size="25" maxlength="60" value="<?php echo ($email); ?>" /></td>
    </tr>
    <tr>
      <td><b><font color="#333333">Confirm Your Email Address</font></b></td>
      <td><input type="text" name="cemail" size="25" maxlength="60" value="<?php echo ($cemail); ?>" /></td>
    </tr>
    <tr>
      <td><b><font color="#333333">Sex</font></b></td>
      <td>Male
        <input type="radio" name="sex" value="male" checked  <?php echo ($sex == 'male')   ? 'checked' : ''; ?> />
Female
<input type="radio" name="sex" value="female" <?php echo ($sex == 'female') ? 'checked' : ''; ?> /></td>
    </tr>
    <tr>
      <td><b><font color="#333333">Sexuality</font></b></td>
      <td><select size="1" name="sexuality">
        <option <?php echo ($sexuality == 'Straight')    ? 'selected' : ''; ?>>Straight</option>
        <option <?php echo ($sexuality == 'Open Minded') ? 'selected' : ''; ?>>Open Minded</option>
        <option <?php echo ($sexuality == 'Bisexual')    ? 'selected' : ''; ?>>Bisexual</option>
        <option <?php echo ($sexuality == 'Gay_Lesbian') ? 'selected' : ''; ?>>Gay_Lesbian</option>
        <option <?php echo ($sexuality == 'Other')       ? 'selected' : ''; ?>>Other</option>
      </select></td>
    </tr>
    <tr>
      <td><b><font color="#333333">Date of Birth </font></b></td>
      <td><select size="1" name="bday">
        <option>Day</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
        <option value="9">9</option>
        <option value="10">10</option>
        <option value="11">11</option>
        <option value="12">12</option>
        <option value="13">13</option>
        <option value="14">14</option>
        <option value="15">15</option>
        <option value="16">16</option>
        <option value="17">17</option>
        <option value="18">18</option>
        <option value="19">19</option>
        <option value="20">20</option>
        <option value="21">21</option>
        <option value="22">22</option>
        <option value="23">23</option>
        <option value="24">24</option>
        <option value="25">25</option>
        <option value="26">26</option>
        <option value="27">27</option>
        <option value="28">28</option>
        <option value="29">29</option>
        <option value="30">30</option>
        <option value="31">31</option>
                        </select>
        <select size="1" name="bmonth">
          <option selected="selected">Month</option>
          <option value="January">January</option>
          <option value="February">February</option>
          <option value="March">March</option>
          <option value="April">April</option>
          <option value="May">May</option>
          <option value="June">June</option>
          <option value="July">July</option>
          <option value="August">August</option>
          <option value="September">September</option>
          <option value="October">October</option>
          <option value="November">November</option>
          <option value="December">December</option>
                        </select>
        <select size="1" name="byear">
          <option selected="selected">Year</option>
          <option value="1991">1991</option>
          <option value="1990">1990</option>
          <option value="1989">1989</option>
          <option value="1988">1988</option>
          <option value="1987">1987</option>
          <option value="1986">1986</option>
          <option value="1985">1985</option>
          <option value="1984">1984</option>
          <option value="1983">1983</option>
          <option value="1982">1982</option>
          <option value="1981">1981</option>
          <option value="1980">1980</option>
          <option value="1979">1979</option>
          <option value="1978">1978</option>
          <option value="1977">1977</option>
          <option value="1976">1976</option>
          <option value="1975">1975</option>
          <option value="1974">1974</option>
          <option value="1973">1973</option>
          <option value="1972">1972</option>
          <option value="1971">1971</option>
          <option value="1970">1970</option>
          <option value="1969">1969</option>
          <option value="1968">1968</option>
          <option value="1967">1967</option>
          <option value="1966">1966</option>
          <option value="1965">1965</option>
          <option value="1964">1964</option>
          <option value="1963">1963</option>
          <option value="1962">1962</option>
          <option value="1961">1961</option>
          <option value="1960">1960</option>
          <option value="1959">1959</option>
          <option value="1958">1958</option>
          <option value="1957">1957</option>
          <option value="1956">1956</option>
          <option value="1955">1955</option>
          <option value="1954">1954</option>
          <option value="1953">1953</option>
          <option value="1952">1952</option>
          <option value="1951">1951</option>
          <option value="1950">1950</option>
          <option value="1949">1949</option>
          <option value="1948">1948</option>
          <option value="1947">1947</option>
          <option value="1946">1946</option>
          <option value="1945">1945</option>
          <option value="1944">1944</option>
          <option value="1943">1943</option>
          <option value="1942">1942</option>
          <option value="1941">1941</option>
          <option value="1940">1940</option>
          <option value="1939">1939</option>
          <option value="1938">1938</option>
          <option value="1937">1937</option>
          <option value="1936">1936</option>
          <option value="1935">1935</option>
          <option value="1934">1934</option>
          <option value="1933">1933</option>
          <option value="1932">1932</option>
          <option value="1931">1931</option>
          <option value="1930">1930</option>
          <option value="1929">1929</option>
          <option value="1928">1928</option>
          <option value="1927">1927</option>
          <option value="1926">1926</option>
          <option value="1925">1925</option>
                        </select></td>
    </tr>
    <tr>
      <td><b><font color="#333333">House Name/Number</font></b></td>
      <td><input type="text" name="street" size="25" maxlength="30" value="<?php echo ($street); ?>" /></td>
    </tr>
    <tr>
      <td><b><font color="#333333">City</font></b></td>
      <td><input type="text" name="city" size="25" maxlength="30" value="<?php echo ($city); ?>" /></td>
    </tr>
    <tr>
      <td><b><font color="#333333">Postcode</font></b></td>
      <td><input type="text" name="postcode" size="25" maxlength="8" value="<?php echo ($postcode); ?>" /></td>
    </tr>
    <tr>
      <td><b><font color="#333333">Country</font></b></td>
      <td><input type="text" name="country" size="25" maxlength="8" value="<?php echo ($country); ?>" /></td>
    </tr>
    <tr>
      <td><b><font color="#333333">Desired Username</font></b></td>
      <td><input type="text" name="username" size="25" maxlength="20" value="<?php echo ($username); ?>" /></td>
    </tr>
    <tr>
      <td><b><font color="#333333">Password</font></b></td>
      <td><input type="password" maxlength="16" size="25" name="pswd_one" value="<?php echo ($pswd_one); ?>" /></td>
    </tr>
    <tr>
      <td><b><font color="#333333">Confirm Password</font></b></td>
      <td><input type="password" maxlength="16" size="25" name="pswd_two" value="<?php echo ($pswd_two); ?>" /></td>
    </tr>
    <tr>
      <td colspan="2" align="center"><input type="submit" name="submit" value="Register" /></td>
    </tr>
  </table>
 
  <table width="930">
      <tr>
        <td colspan="2"></td>
      </tr>
      
      <tr>
        <td colspan="2"></td>
      </tr>
      <tr>
        <td colspan="2"></td>
      </tr>
      <tr>
        <td colspan="2"></td>
      </tr>
      
      <tr>
        <td></td>
      </tr>
    </table>
  </form>
  </div>
 
  </body>
 
</html>
Last edited by mikes1471 on Sun Feb 01, 2009 1:00 pm, edited 1 time in total.
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Problem successfully submitting form

Post by papa »

Do you define the function in the functions.php file ?
mikes1471
Forum Commoner
Posts: 88
Joined: Sat Jan 24, 2009 3:29 pm

Re: Problem successfully submitting form

Post by mikes1471 »

Yes, the functions.php document looks like this:

Code: Select all

<?php
 
 
 
function protect($string){
    $string = mysql_real_escape_string($string);
    $string = strip_tags($string);
    $string = addslashes($string);
    
    return $string;
}
 
function connect(){
    $con = mysql_connect("**********.com", "(username)", "(password)") or die(mysql_error());
    $db = mysql_select_db("users", $con);
}
 
 
?>
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Problem successfully submitting form

Post by papa »

But escape_data() is never defined...

So this:

Code: Select all

//initialize working variables
$fname     = escape_data($_POST['firstname']);
...
 
Should probably be:

Code: Select all

//initialize working variables
$fname     = protect($_POST['firstname']);
...
 
mikes1471
Forum Commoner
Posts: 88
Joined: Sat Jan 24, 2009 3:29 pm

Re: Problem successfully submitting form

Post by mikes1471 »

Oh brilliant thanks
Post Reply