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