Please help! Insert into database problem
Hi all, I am pretty new to PHP programming and and have a problem with inserting customer data into my MySQL database, I think its probably a really obvious problem, but I've been staring at the same code for hours and its driving me mad
This code fragment is supposed to insert all the customer data into my Customer database. Each time I try, it returns to the clients form saying "The client has been saved" but it hasn't.
I would greatly appreciate any help x
Here's the code:
PHP Code:
//Check to make sure the data has been entered before saving
function SaveClient(&$dbconnection)
{
//If there are empty fields: a message is displayed
if (empty($_POST['title']) || empty($_POST['firstname']) || empty($_POST['surname']) || empty($_POST['email']) || empty($_POST['addressline1']) || empty($_POST['town']) || empty($_POST['county']) || empty($_POST['postcode']) || empty($_POST['homephone']) || empty($_POST['dateofbirth']) || empty($_POST['username']))
return "<p>You must fill in all fields!<br />".AddClientForm(); //Display the form to add a client
$title = EscapeData($_POST['title'], $dbconnection);
$firstname = EscapeData($_POST['firstname'], $dbconnection);
$surname = EscapeData($_POST['surname'], $dbconnection);
$email = EscapeData($_POST['email'], $dbconnection);
$addressline1 = EscapeData($_POST['addressline1'], $dbconnection);
$town = EscapeData($_POST['town'], $dbconnection);
$county = EscapeData($_POST['county'], $dbconnection);
$postcode = EscapeData($_POST['postcode'], $dbconnection);
$homephone = EscapeData($_POST['homephone'], $dbconnection);
$dateofbirth = EscapeData($_POST['dateofbirth'], $dbconnection);
$username = EscapeData($_POST['username'], $dbconnection);
//Query to show if the user name already exists
$query = "SELECT username FROM Customer WHERE (username = '$username')";
$result = @mysql_query($query); //Run the query.
if ($result) //If it ran ok, display the records.
{
if (mysql_num_rows($result) > 0) //The client name already exists
return "<p>Unfortunately that user name already exists, please choose another</p><br />".AddClientForm(); //Display the form to register the client
mysql_free_result($result); //Free up the resources.
}
//Checks that there are no invalid characters in the title
if (!preg_match("/^[A-Za-z' -]{2,50}$/", stripslashes(trim($title))))
return "<p>Please, enter a valid title!</p><br />".AddClientForm(); //Display the form to add a new client
//Client forename checking
if (!preg_match("/^[^';=]{5,255}$/", $firstname)) //Security (for avoiding malicious entries)
return "<p>Please enter a valid forename!</p><br />".AddClientForm(); //Display the form to add a new client
//The passwords are checked to see if they are the same
if (strcmp($_POST['password1'], $_POST['password2']) != 0)
return "<p>Your password did not match the confirmed password, please try again</p><br />".AddClientForm(); //Display the form to add a client
//Checks that there are no invalid characters in the password
$password = EscapeData($_POST['password1'], $dbconnection);
if (!preg_match("/^\w{5,16}$/", $password))
return "<p>Please, enter a valid password!</p><br />".AddClientForm(); //Display the form to add a client
else {
$query = "INSERT INTO Customer (title, firstname, surname, email, addressline1, addressline2, town, county, postcode, homephone, dateofbirth, workphone, mobile, username) " .
"VALUES ('$title', '$firstname', '$surname', '$email', '$addressline1', '$addressline2', '$town', '$county', '$postcode', '$homephone','$dateofbirth', '$workphone', '$mobile', '$username', PASSWORD('$password'))";
@mysql_query($query);
return "<p>The client has been saved</p>".ClientsForm(); //Display the form with all clients
echo $firstname;
}
}
|