php - 如何创建 jquery 函数来防止向数据库重复输入

标签 php jquery mysql

我创建了一个表单,它获取数据并将其插入数据库。单击创建按钮我希望该函数检查数据库是否已存在具有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>

最佳答案

您可以使用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 中自定义它

关于php - 如何创建 jquery 函数来防止向数据库重复输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31242849/

相关文章:

jquery - grails,哪个 jquery 事件处理程序绑定(bind)到由 remoteFunction 或 remoteLink 进行的元素更新

mysql - 为什么 LEFT OUTER JOIN 无法工作?

javascript - 重复显示数据

javascript - 使用jquery获取childern值

javascript - Sublime Text 3 函数助手

javascript - 暂停和播放事件会更改 jquery 中的 CSS

mysql - 启用 innodb_dedicated_server 对性能有好处吗?

mysql - 选择并删除具有特定 ID 的重复记录

php - 是否可以使用 PHP CLI 获取 *.sh 文件并在 PHP 脚本中访问导出的环境变量?

php - 导致对象实例化的 Foreach 循环