2013-08-17 52 views
1

我寫了這個php代碼來提交表單中的所有條目到數據庫中,但我遇到了一個奇怪的問題。插入查詢有時會起作用,但其他時間則不起作用,並顯示「查詢數據庫時出錯」。我已經檢查過所有HTML字段的ID,一百次,它們絕對沒問題。數據庫中的所有列也都很好。請幫我解決這個錯誤。有時插入查詢有效,但有時它不會

<?php 
require_once('connectvars.php'); 
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die('Error connecting to the MySQL server.'); 
$name = mysqli_real_escape_string($dbc, trim($_POST['name'])); 
$fathername = mysqli_real_escape_string($dbc, trim($_POST['fathername'])); 
$dob = mysqli_real_escape_string($dbc, trim($_POST['dob'])); 
$year = $_POST['year']; 
$email = mysqli_real_escape_string($dbc, trim($_POST['email'])); 
$pass = mysqli_real_escape_string($dbc, trim($_POST['pass'])); 
$confirmpass = mysqli_real_escape_string($dbc, trim($_POST['confirmpass'])); 
$address = mysqli_real_escape_string($dbc, trim($_POST['address'])); 
$mobile = mysqli_real_escape_string($dbc, trim($_POST['mobile'])); 
$rollno = mysqli_real_escape_string($dbc, trim($_POST['rollno'])); 
$coordinator = $_POST['coordinator']; 



if(isset($_POST['submit'])) { 


    $output_form = false; 

    //To check if all the form entries have been filled or not 
    if(!empty($name) && !empty($fathername) && !empty($dob) && !empty($email) && !empty($pass) && !empty($confirmpass) && !empty($address) && !empty($mobile) && !empty($rollno)){ 

     //Code to validate the form entries 
     $domain = preg_replace('/^[a-zA-Z0-9][a-zA-Z0-9\._\-&!?=#]*@/', '', $email); 

     //function to check if the domain name is valid and it exists in the Domain Name System(if the web server is on Windows) 
     function win_checkdnsrr($domain, $recType='') { 
      if(!empty($domain)) { 
       if($recType == '') $recType="MX"; 
       exec("nslookup -type=$recType $domain",$output); 
       foreach($output as $line) { 
        if (preg_match("/^$domain/", $line)) return true; 
       } 
      } 
     } 

     if(preg_match('/^[a-zA-Z0-9][a-zA-Z0-9\._\-&!?=#]*@/', $email) && win_checkdnsrr($domain) && preg_match('/^\d{10}$/', $mobile) && preg_match('/(DTU\/)? ?\w{4}\/\w{2,3}\/\d{3,4}$/', $rollno) && preg_match('/^.{6,20}$/', $pass) && ($pass == $confirmpass)) { 


      $query = "SELECT * FROM reg_table WHERE email = '$email'"; 

      $data = mysqli_query($dbc, $query); 
      if(mysqli_num_rows($data) == 0) { 

       //The user is registering for the first time, so insert into database 
       $query = "INSERT INTO reg_table(`name`, `fathername`, `dob`, `collegeyear`, `email`, `pass`, `address`, `mobile`, `rollno`, `coordinator`) VALUES ('$name','$fathername', '$dob', $year, '$email', SHA('$pass'), '$address', $mobile, '$rollno', '$coordinator')"; 

       $test = mysqli_affected_rows($dbc); 
       echo $test; 

       $result = mysqli_query($dbc, $query) or die('Error querying database'); 


       mysqli_close($dbc); 
       $output_form = false; 

       echo '<p class="success">Data submitted</p>'; 
      } 

      else { 
       //A user with same email-id exists 
       echo '<p class="error">A person with this email-id is already registered</p>'; 
       $output_form = true; 
      } 
     } 
     else { 
      //the data entered is invalid 
      echo '<p class="error">Please enter valid data</p>'; 
      $output_form = true; 
     } 
    } 
    else { 
     echo '<p class="error">Please fill all the form fields</p>'; 
     $output_form = true; 
    } 
} 
else { 
    $output_form = true; 
} 

if($output_form) { 

?>

+0

你的代碼正在哭泣,被重構。 – DevZer0

+0

改爲使用[prepared statements](http://php.net/manual/en/mysqli.prepare.php)。 – hjpotter92

+0

@ hjpotter92有什麼好處? –

回答

0

我曾與MySQL插入有時同樣的問題。沒有錯誤。

我正在做批量插入做一個批處理過程中放了一個暫停作品。

usleep(250000); // take a 1/4 second break 
相關問題