Php 如何创建jquery函数以防止数据库中出现重复条目

Php 如何创建jquery函数以防止数据库中出现重复条目,php,jquery,mysql,Php,Jquery,Mysql,我创建了一个表单,它获取数据并将其插入数据库。onlick of create按钮我想让函数检查数据库中是否已经存在具有employee_id的条目。如果存在,我想让它显示已经存在的数据。你还想插入吗?我不知道。有人能帮我吗。表格是 <form id="myForm" name="myForm" action='insert.php' method='post' > <input type='hidden' name='st' value=0> <t

我创建了一个表单,它获取数据并将其插入数据库。onlick of create按钮我想让函数检查数据库中是否已经存在具有employee_id的条目。如果存在,我想让它显示已经存在的数据。你还想插入吗?我不知道。有人能帮我吗。表格是

<form id="myForm" name="myForm" action='insert.php' method='post' >
    <input type='hidden' name='st' value=0>
    <table  style="text-align:center; width:100%">
        <tr>
            <td style="text-align:right"><label>Select SE/AE:</label></td>
            <td style="text-align:left">
                <?php include("configs.php");
                $sql = "SELECT DISTINCT seae FROM se_ae ";?>
                <select name="seae">
                    <option value="" selected></option>
                    <?php foreach ($dbo->query($sql) as $row) { ?>
                        <option value="<?php echo $row['seae']; ?>">
                        <?php echo $row['seae']; ?></option> 
                    <?php }?>
                </select>
            </td>
        </tr>
        <tr>
            <td style="text-align:right"><label>Select Brand:</label></td>
            <td style="text-align:left"> 
                <?php //include("configs.php"); 
                $sql = "SELECT DISTINCT `brand` FROM se_ae ";?>
                <select name="brand">
                    <option value="" selected> </option>
                    <?php foreach ($dbo->query($sql) as $row) { ?>
                        <option value="<?php echo $row['brand']; ?>">
                        <?php echo $row['brand']; ?></option> 
                    <?php }?>
                </select>
            </td>
        </tr>
        <tr>
            <td style="text-align:right"><label>Select Territory:</label></td>
            <td style="text-align:left">
                <?php //include("configs.php");
                $sql = "SELECT DISTINCT `territory` FROM se_ae ";?>
                <select name="territory"> 
                    <option value="" selected></option>
                    <?php foreach ($dbo->query($sql) as $row) { ?>
                        <option value="<?php echo $row['territory']; ?>">
                        <?php echo $row['territory']; ?></option> 
                    <?php }?>
                </select>
            </td>
        </tr>
        <tr>
            <td style="text-align:right"><label for="name">Employee Name:</label></td>
            <td style="text-align:left">
                <input type="text" id="name"  name="name"/>
            </td>
        </tr>
        <tr>
            <td style="text-align:right"><label for="number">Employee ID:</label></td>
            <td style="text-align:left">
                <input type="text" id="number" name="number"  />
            </td>
        </tr>
        <tr>
            <td style="text-align:right"><label for="email"> Email:</label></td>
            <td style="text-align:left">
                <input type="text" id="email"   name="email"  />
            </td>
        </tr>
        <tr>
            <td style="text-align:right"><label for="contact"> Contact:</label></td>
            <td style="text-align:left">
                <input type="text" id="contact"  name="contact"  />
            </td>
        </tr>
        <tr>
            <td style="text-align:right"><label for="exist"> Exist:</label></td>
            <td style="text-align:left">
                <input type="radio" id="exist"  name="exist" value="Active"/>Active 
                <input type="radio" id="exist"  name="exist" value="NA"/>NA
            </td>
        </tr>
        <tr>
            <td style="text-align:right" class='swMntTopMenu'>
                <input style="background-color:rgb(255,213,32)"  name="Reset" type="reset" value="Reset">
            </td>
            <td style="text-align:left" class='swMntTopMenu'>
                <input style="background-color:rgb(255,213,32)"  name="submit" type="submit" value="Create">
            </td>
        </tr>
    </table>
</form>

选择SE/AE:
选择品牌:
选择地区:
员工姓名:
员工ID:
电邮:
联系人:
存在:
活跃的
NA

您可以使用Jquery验证

$(function () {
$("#resturantRegistration").validate({
    rules: {
            number: { 
            required: true,
                    remote:"user/checkEmployee"
        }
    },
    messages: {
        number: { 
            required: "Please enter Employee id",
            remote: $.validator.format("Employee id already in use")
        }
    }
});
});
PHP函数

function checkEmployee(){
            $emailid = $_GET['number'];
            if(!empty($emailid)){
                $emailRes = $this->users->is_email_available($emailid);
                if ($emailRes == ''){                        
                    echo 'false';
                } else  {            
                    echo 'true';
                }
            }
        }
用于检查数据库的模型函数

/**
     * Check if email available for registering
     *
     * @param   string
     * @return  bool
     */
    function is_email_available($email)
    {
        $this->db->select('1', FALSE);
        $this->db->where('LOWER(email)=', strtolower($email));
        $this->db->or_where('LOWER(new_email)=', strtolower($email));

        $query = $this->db->get($this->table_name);
        return $query->num_rows() == 0;
    }

以上代码遵循codeigniter MVC框架,您也可以在核心PHP中自定义此代码

它可以使用jquery ajax.post..@user3113490获得,如果您不介意的话,可以解释一下我在这篇jquery ajax帖子中真的是个傻瓜。。就像一个调解人。。。。首先是获取输入值并将其传递到一个php文件中,该文件将执行验证。。。ajax是传递值的人。。。就像我说的,就像一个调解人。你可以从这条线索中得到想法。我将此用作电子邮件ID,您可以将其用作员工ID