插入查询有时会起作用,但有时却不起作用

时间:2013-08-17 05:14:27

标签: php mysql insert

我写了这个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) {

&GT;

1 个答案:

答案 0 :(得分:0)

我在插入有时的mysql时遇到了同样的问题。没有错误。

我正在进行大量插入,进行批量处理,暂停工作。

usleep(250000); // take a 1/4 second break