doubian6241 2012-11-22 10:00
浏览 59
已采纳

PHP脚本不将表单数据插入数据库

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.
";
    } else {
      if(is_numeric($firstname)){
        $validationError .= "The first name you have supplied cannot contain numbers.
";
      }
      if(strlen($firstname) > 50) {
        $validationError .= "The first name you have supplied must be less than 50 characters.
";
      }
      if(strlen($firstname) < 3) {
        $validationError .= "The first name you have supplied is too short.
";
      }
      if(isValid($firstname) == false ){
        $validationError .= "The first name cannot contain special characters.
";
  }
    }

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


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

    // 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.
";
      }
    } else {
      $validationError .= "Please enter your email address.
";
    }

  //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.
";
    } 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.
";
      } 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 
in order to enter this contest.";
}
}
?>
  • 写回答

1条回答 默认 最新

  • dousi5501 2012-11-22 10:03
    关注

    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) . "')";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?