Hey I hope someone can help me here because I am pretty stumped on this one...

All I'm trying to do is to update my user_usr table with the submitted form data. It was working perfectly the other day so I must have changed something somewhere.

The code and the database query seem to execute as they should, but the form data isn't being appended properly to the database. Although a new record is inserted and a new user_id appears, the remaining fields of the database are left blank... Here is the 'register_server.php' file

<?php


//CONNECT TO MYSQL
mysql_connect("localhost", "root", "root") or die("Could not connect: ".mysql_error());

echo "<h2>Connected to MySQL</h2><br>";

//CONNECT TO TEST DATABASE
mysql_select_db("computabid_auctions") or die("Could not connect: ".mysql_error());

echo "<h2>Connected to Database</h2><br>";


	//IMPORT DATA FOR DATABASE UPDATE
	$firstname = $_POST['firstname'];

	$surname = $_POST['surname'];

	$address1 = $_POST['address1'];

	$address2 = $_POST['address2'];

	$city = $_POST['city'];

	$postcode = $_POST['postcode'];

	$county = $_POST['county'];

	$region = $_POST['region'];

	$telephone = $_POST['telephone'];

	$email = $_POST['email'];    

	$yearofbirth = $_POST['year'];
	$monthofbirth = $_POST['month'];
	$dateofbirth = $_POST['date'];
	$dob = $yearofbirth.'-'.$monthofbirth.'-'.$dateofbirth;

	$username = $_POST['username'];
	$password = md5($_POST['password2']);

//UPDATE THE DATABASE
$query = "INSERT INTO user_usr (firstname, surname, addr_1, addr_2, city_town, postcode, county, region, tel_no, email_addr, dob, username, passwd)

VALUES('$firstname', '$surname', '$address1', '$address2', '$city', '$postcode', '$county', '$region', '$telephone', '$email', '$dob', '$username', '$password')";

mysql_query($query) or die(mysql_error());

mysql_close();



echo "<h2>You have successfully Registered</h2><br>";

?>

Before this page executed, I am validating the form input, so it could be a problem with that validation page (below) rather than this page shown above. However, I'm not getting any error messages, so I really am confused as to what's going wrong! Here is the main logic of the form validation page (register.php)...

<?php
//THIS FUNCTION VALIDATES USER INPUT
function VerifyForm(&$values, &$errors)
	{
   if (strlen($values['firstname']) == 0)
      $errors['firstname'] = 'Please Enter a name';
      
   if (strlen($values['surname']) == 0)
   	$errors['surname'] = 'Plese Enter a surname';
   	
   if (strlen($values['address1']) == 0)
   	$errors['address1'] = 'Please enter the first line of your address';	
   	
   if (strlen($values['address2']) == 0)
   	$errors['address2'] = 'Please enter you street name';	
   	
   if (strlen($values['city']) == 0)
   	$errors['city'] = 'Please enter your town/city';		
   	
   if (strlen($values['postcode']) == 0)
   	$errors['postcode'] = 'Please enter your postcode';
   elseif (strlen($values['postcode']) < 6)
      $errors['postcode'] = 'Specified Postcode is too short';	
	elseif (!preg_match("/^([Gg][Ii][Rr]0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))){0,1}[0-9][A-Za-z]{2})$/",$values['postcode']))   	
		$errors['postcode'] = 'Invalid postcode entered';   	
   	
   if (strlen($values['county']) == 0)
   	$errors['county'] = 'Please enter your county';
   	
	if (strlen($values['region']) == 0)
   	$errors['region'] = 'Please select a region';   	
   	
   if (strlen($values['telephone']) == 0)
   	$errors['telephone'] = 'Please enter a contact telephone number';
   elseif (strlen($values['telephone']) < 11)
   	$errors['telephone'] = 'telephone number too short';
   	$values['telephone'] = ereg_replace('[^0-9]', '', $_POST['telephone']);
	if (!filter_var($values['telephone'], FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => "/^0[1-9][0-9]{8,9}$/"))))   
   	$errors['telephone'] = 'Invalid number';
   	
   if (strlen($values['email']) == 0)
   	$errors['email'] = 'Please enter a valid e-mail address';	
   elseif(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $values['email']))
    	$errors['email'] = 'please enter a real email';

// ........................ more validation.......................  
      	 
   if (strlen($values['password2']) == 0)
   	$errors['password2'] = 'Please re-enter your password';								
   
   if(strcmp($values['password'], $values['password2'])!= 0)
		$errors['password2'] = 'Passwords do not match';   
   	     
   return (count($errors) == 0);
}

function DisplayForm($values, $errors)
{
?>

<html>

// ................................ blah blah blah ---------------------------------------

<table id="regtable"><tr><td class="error" colspan="3">
<?php
	if (count($errors) > 0)
        echo "Ooops! There were some errors in your form...";       
?>
</td></tr>


<form action="" method="POST">


	<tr><td width="30%">First Name: *</td>
	<td width="20%"><input type="text" name="firstname" maxlength="50" value"<?= htmlentities($values['firstname']) ?>"/></td> 
	<td width="50%" class="error"><?= $errors['firstname'] ?></td></tr><br>
		
// ......................... More input fields here ......................................

</html>

<?php
}
// PROCESS THE FORM DATA
function ProcessForm($values)
{    
    header("Location: http://localhost/register_server.php");
}

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $formValues = $_POST;
    $formErrors = array();
    
    if (!VerifyForm($formValues, $formErrors))
        DisplayForm($formValues, $formErrors);
    else
        ProcessForm($formValues);
}
else
    DisplayForm(null, null);
?>

Thanks for looking

Okay, I've figured out that it's to do with the form action attribute. When I change the form action to point to the register_server.php page, the data is added to the database correctly.

The problem I now have is that the VerifyForm function doesn't get executed, because the form data gets send straight to the register_server.php page.

Is there a way I can validate the input first, if no errors are found, then send the data to the register_server.php page?? I hope this makes sense,

Muchos Gracias

No actual problem is that, you are using header for redirection, then POST data will be lost, so your record insertion will be empty...

Correct. I am now using:

<form action="<?= $_SERVER['PHP_SELF'] ?>" method="POST">

So the validation takes place on the same page. I think I need a method of directing the user and the entered data to the register_server.php page. This diverts them:

header("Location: http://localhost/register_server.php");

But it doesn't send the form data with it. Any ideas of how to do that?
Thanks for your help

You can use javascript validations instead of PHP validations, there you can check the form data before submitting the form.

Otherwise If you need the same PHP validations, If validations are passed then you need to store your entire form data into a session array

It is so simple, just add this loop at the end of your verifyform function

foreach($values as $keys=>$val)
{
    unset($_SESSION[$keys]);
    $_SESSION[$keys] = $val;
}

 return count($errors);

Thanks for your help but I am new to php and still struggling to get this working properly. I presume I need to start the session on both pages? The loop that you kindly suggested puts the form values into the $val array but I'm still unable to access the values from this array on the next page? :( Here's what I've been trying...

register.php

<?php session_start();

function VerifyForm(&$values, &$errors)
{
   if (strlen($values['firstname']) == 0)
      $errors['firstname'] = 'Please Enter a name';
      
   if (strlen($values['surname']) == 0)
   	$errors['surname'] = 'Plese Enter a surname';

//--------------validation----------------------

  foreach($values as $keys=>$val)
  {
	  unset($_SESSION[$keys]);
     $_SESSION[$keys] = $val;
  }
  
  return (count($errors) == 0);
}
?>

register_server.php

<?php session_start(); 
//WHAT GOES HERE??  :S
$getfirstname = $_SESSION[$val]['firstname'];

?>

Thanks

Thanks for your help but I am new to php and still struggling to get this working properly. I presume I need to start the session on both pages? The loop that you kindly suggested puts the form values into the $val array but I'm still unable to access the values from this array on the next page? :( Here's what I've been trying...

register.php

<?php session_start();

function VerifyForm(&$values, &$errors)
{
   if (strlen($values['firstname']) == 0)
      $errors['firstname'] = 'Please Enter a name';
      
   if (strlen($values['surname']) == 0)
   	$errors['surname'] = 'Plese Enter a surname';

//--------------validation----------------------

  foreach($values as $keys=>$val)
  {
	  unset($_SESSION[$keys]);
     $_SESSION[$keys] = $val;
  }
  
  return (count($errors) == 0);
}
?>

register_server.php

<?php session_start(); 
//WHAT GOES HERE??  :S
$getfirstname = $_SESSION[$val]['firstname'];

?>

Thanks

Just display the session array in your register_server.php page

echo "<pre>";
print_r($_SESSION);
echo "</pre>";

If you get an empty session array then the data which you are assigning in your previous page(register.php) is not working, check there whether the post values are coming to till that place or not.

Solved it!! It's actually really simple but I just couldn't get my head round how to extract the values on the other side!

register.php

<?php session_start();
$_SESSION['formdata'] = $values;
?>

register_server.php

<?php session_start();

//I USED THIS TO CHECK THE VALUES EXISTED
foreach($_SESSION['formdata'] as $keys=>$value)
   {
	  echo 'The value of $_SESSION['."'".$keys."'".'] is '."'".$value."'".' <br />';
   }

//THEN EXTRACTED THEM FROM THE ARRAY
        $firstname = $_SESSION['formdata']['firstname'];
	$surname = $_SESSION['formdata']['surname'];

	$address1 = $_SESSION['formdata']['address1'];
?>

Thankyou for your patience!
nonshatter x

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.