Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6374623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:30:40+00:00 2026-05-25T01:30:40+00:00

I have created a form (enquiry form) in HTML that posts to the following

  • 0

I have created a form (enquiry form) in HTML that posts to the following code:

<?php           
if(isset($_POST['submit']))

{
$name = mysql_real_escape_string((string)$_POST['name']);
$surname = mysql_real_escape_string((string)$_POST['surname']);
$email = mysql_real_escape_string((string)$_POST['email']);
$phone = mysql_real_escape_string((string)$_POST['phone']);
$country = mysql_real_escape_string((string)$_POST['country']);
$message = mysql_real_escape_string((string)$_POST['message']);

$sql = "INSERT INTO contact
       (name, surname, email, phone, country, message)
       VALUES('$name', '$surname', '$email', '$phone', '$country', '$message')";

mysql_select_db($db);
$retval = mysql_query( $sql, $conn )or die(mysql_error());

echo 'Thank you '.$name.' '.$surname.'. Your enquiry has been forwarded to our team. <br><br>Please check you email inbox for further information.<br><br>Return to homepage:<br><br><button class="search" onclick="/">Return to homepage</button>';

mysql_close($conn);
}

?>

I am wondering, how I can display errors and stop the form posting when invalid or zero data is entered?

Whilst learning how to create forms on the web, I also heard about SQL injections. Am I protected?

Help much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:30:41+00:00Added an answer on May 25, 2026 at 1:30 am

    I am wondering how I can display errors and stop the form posting when invalid or zero data is entered.

    You have to do the validation after if(isset($_POST['submit'])). For example you could check is name is non empty:

    if (empty($_POST['name'])) {
        $errors[] = 'name must not be empty';
    }
    

    For more complicated validations such as validating that email is valid, you should take a look at the filter extension:

    $email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
    if (!$email) // invalid email
    

    And after all your validations:

    if (!count($errors)) {
        // do the insert here
    }
    

    You could use a while block to break as soon as you detect an error:

    while (isset($_POST['submit'])) {
    
        if (empty($_POST['name'])) {
            $errors = 'name must not be empty';
            break;
        }
    
        // do the insert here
    
        break;
    }
    

    Whilst learning how to create forms on the web, I also heard about sql injections, am I protected?

    Yes, as long as you escape anything that you embed in a SQL query (just like you are doing), you are protected.

    You should try using prepared statements, this is safer and easier to use.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created test form here: http://www.sitecdn.net/clients/repcow/form_min.html Code for checkboxes looks like: <div class=optIn>
I have created a form below and some javascript code in php, the it
I have the following form in cakephp: <div class=quickcontactDisplay> <?php echo $form->create('Enquiry',array('action'=>'add','class'=>'quickcontact')); echo $form->hidden('visitor_id',
I have created a form that has a name and email address input. Name:
I have created a form that has a Submit button and it works by
I have created an form validation using ajax/php. Each text box is validated using
I have created a form that is used for both adding and editing a
I have created a non-form c# program that uses the NotifyIcon class. The text
I have created a macro for excel which will pop up form that contains
I have created a form that must pass validation via JQuery to see if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.