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 8887001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:41:54+00:00 2026-06-14T21:41:54+00:00

I’m having trouble inserting form data into my database. I can connect to the

  • 0

I’m having trouble inserting form data into my database. I can connect to the database as it does not throw up an error but no information is being inserted. Below is my code, any help would be appreciated.

<?php  
// string checking
function isValid($str) {
  if(!preg_match('/[^A-Za-z0-9.-]/', $str)) {
  return true;
  } else {
    return false;
  }
}


// Check for form submission
if(isset($_POST['submit'])){

// Get the POST data
$agree = $_POST['agree'];
$firstname = $_POST['firstname'];
$surname = $_POST['surname'];
$business = $_POST['business'];
$state = $_POST['state'];
$email = $_POST['email'];

// If the T&C box has been ticked
if($agree){

  // Validate the POST data
  $validationError = '';

    // Name Validation
    if($firstname == ''){
      $validationError .= "Please enter your first name.\n";
    } else {
      if(is_numeric($firstname)){
        $validationError .= "The first name you have supplied cannot contain numbers.\n";
      }
      if(strlen($firstname) > 50) {
        $validationError .= "The first name you have supplied must be less than 50 characters.\n";
      }
      if(strlen($firstname) < 3) {
        $validationError .= "The first name you have supplied is too short.\n";
      }
      if(isValid($firstname) == false ){
        $validationError .= "The first name cannot contain special characters.\n";
  }
    }

if($surname == ""){
      $validationError .= "Please enter your Surname.\n";
    } else {
      if(is_numeric($surname)){
        $validationError .= "The Surname you have supplied cannot contain numbers.\n";
      }
      if(strlen($surname) > 50) {
        $validationError .= "The Surname you have supplied must be less than 50 characters.\n";
      }
      if(strlen($surname) < 3) {
        $validationError .= "The Surname you have supplied is too short.\n";
      }
      if(isValid($surname) == false ){
        $validationError .= "The Surname cannot contain special characters.\n";
  }
    }


if($state == ''){
      $validationError .= "Please select your state.\n";
}

    // Email validation

    // Function to validate email addresses, taken from here: http://www.linuxjournal.com/article/9585
    function check_email_address($email) {

      // Set up regular expression strings to evaluate the value of email variable against
      $regex1 = '/^[_a-z0-9-][^()<>@,;:\\"[] ]*@([a-z0-9-]+.)+[a-z]{2,4}$/i';

      // Run the preg_match function on regex 1
      if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
           return false;
      } else {
          return true;      
      } 
    }

    if($email != ''){
      if(!check_email_address($email)) {
        $validationError .= "The email address does not appear to be correct, please try again.\n";
      }
    } else {
      $validationError .= "Please enter your email address.\n";
    }

  //Start the mySQL connection

  if($validationError == ''){
    $link = mysql_connect('localhost', '--removed--', '--removed--');

    if (!$link) {
      $validationError .= "There was an error connecting to the database. Please contact us so that we can fix the problem.\n";
    } else {

      // Select the db
      mysql_select_db("keyinv_seminar", $link);

      // Check for an existing entry under that email address
      $checkQuery = 'SELECT * FROM seminar WHERE email="' . mysql_real_escape_string($email) . '"';
      $result = mysql_query($checkQuery, $link);

      if (mysql_num_rows($result) != 0) {
        $validationError .= "There is already an entry in the competition, using that email address.\n";
      } else {
        // There is no existing entry, update the db
        $insertQuery = "INSERT INTO seminar (firstname, surname, business, state, email) VALUES ('" . mysql_real_escape_string($firstname) . "', '" . mysql_real_escape_string($surname) . "', '" . mysql_real_escape_string($business) . "', " . mysql_real_escape_string($state) . ", '" . mysql_real_escape_string($email) . "')";

        $result = mysql_query($insertQuery, $link);

      }

      // Close the connection
      if($link){
        mysql_close($link);

        if($validationError == ''){
          header('Location: thankyou.php');
        }
      }
    }
  }
} else {
  $validationError = "You must accept the Terms and Conditions \nin order to enter this contest.";
}
}
?>
  • 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-06-14T21:41:55+00:00Added an answer on June 14, 2026 at 9:41 pm

    Check out your INSERT query..

    $insertQuery = "INSERT INTO seminar (firstname, surname, business, state, email) VALUES ('" 
        . mysql_real_escape_string($firstname) . "', '" 
        . mysql_real_escape_string($surname) . "', '" 
        . mysql_real_escape_string($business) . "', " 
        . mysql_real_escape_string($state) . ", '" 
        . mysql_real_escape_string($email) . "')";
    

    You’ve missed out some single quotes around the $state value. Change it to this:

    $insertQuery = "INSERT INTO seminar (firstname, surname, business, state, email) VALUES ('"  
        . mysql_real_escape_string($firstname) . "', '" 
        . mysql_real_escape_string($surname) . "', '" 
        . mysql_real_escape_string($business) . "', '" 
        . mysql_real_escape_string($state) . "', '" 
        . mysql_real_escape_string($email) . "')";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
Does anyone know how can I replace this 2 symbol below from the string
I want to construct a data frame in an Rcpp function, but when I
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,

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.