php - 插入查询有时有效,但有时无效

标签 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) {

?>

最佳答案

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

我正在做批量插入,做一个批处理,暂停了它的工作。

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

关于php - 插入查询有时有效,但有时无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18285375/

相关文章:

php - 如何计算php中文件夹中的文件数?

php - 使用 Laravel 5.3 模型绑定(bind)更新多条记录

php - 在 PHP 中针对数组选择的表单

php - 如何一次在多个字段中搜索多个单词?

mysql - 任意字段分组/求和

mysql - 插入后触发sql

javascript - 如何在jquery中插入php脚本?

php - 将时间戳插入 MySQL?

php - 比较 SQL 和 PHP 中的 float

mysql - 如何在 MySQL 中将元素推送到 JSON