Jump to content

Looking for a better way to insert values into database


perky416
 Share

Recommended Posts

Hi everyone.

 

I have a registration form on my website that returns errors if the form isnt filled out correctly.

An example of the code that returns the errors is:

 

 
// if first name is blank return error
if (!$first_name)
{
	echo "Please enter your first name!<br />";
}

// if first name contains invalid charcters return error
if (preg_match('/[^a-zA-Z\s]/', $first_name))
{
	echo "Your first name contains invalid charcters!<br />";
}

// set first name maximum length
if ($first_name && !preg_match('/[^a-zA-Z0-9\s]/', $first_name))
{
	if (strlen($first_name) >25)
	{
		echo "The maximum length for first name is 25 characters!<br />";
	}
}

 

Iv wrote it like this so that i can get multiple error messages appearing at the same time.

 

When it comes to writing the form data to the database im using the following:

 

if ($first_name && !preg_match('/[^a-zA-Z\s]/', $first_name) && strlen($first_name) <26 && $last_name && !preg_match('/[^a-zA-Z\s]/', $last_name) && strlen($last_name) <26 && $dob_day != "Day" && $dob_month != "Month" && $dob_year != "Year" && $gender != "Select" && $email && filter_var($email, FILTER_VALIDATE_EMAIL) && $confirm_email && $email == $confirm_email && $email_count == 0 && $town && !preg_match('/[^a-zA-Z0-9\s]/', $town) && strlen($town) < 26 && $location != "Select" && $postcode && !preg_match('/[^a-zA-Z0-9\s]/', $postcode) && $username && !preg_match('/[^a-zA-Z\s]/', $username) && strlen($username) >2 && strlen($username) <25 && $username_count == 0 && $password && strlen($password) >5 && strlen($password) <26 && $confirm_password && $password == $confirm_password)
{
// code to write to database here		
}

 

I was wondering if there was a better way to confirm that all the form data is ok, and if so write the data to the database? Im new to php and would love to learn a way to make this easier instead of typing out loads of lines of code.

 

Thanks to anyone that can help me.

Link to comment
Share on other sites

Store your field validation errors in an array, then if the array is empty(), insert the record into the DB. Alternately, you could set a 'flag' variable if a field doesn't validate that would not allow the insert if it's present.

Link to comment
Share on other sites

Thanks for info both of you, however when iv tried to implement it using the example, when the form fields are empty the only error message im getting is "Array".

 

<?php
$first_name = $_POST['first_name'];
$submit = $_POST['submit'];

if ($submit)
{

$error = array();
if (!$first_name)
{
    $error[] = "Please enter your first name!";
}
    
if(empty($error))
{
    // code to write to database here
}
else
{
    echo explode('<br />', $error);
}

}
?>
<html>
<form action="test.php" method="POST">
<input type="text" name="first_name" />
<input type="submit" name="submit" />
</form>
</html>

 

Have i done it correctly? Iv never used an array before.

 

Thanks again

Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

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