简体   繁体   English

我想验证我的数据库“名称和组”中的两列是否存在显示已存在,如果不存在则插入到数据库中

[英]I want to validate two columns from my database “name and group” if it exists display already exists and if it doesn't exist insert into the database

this the html with ajax and it displays successful and inserts into the database but how can I display error message if the name is already exist after the validation这是带有ajax的html,它显示成功并插入到数据库中,但是如果验证后名称已经存在,我如何显示错误消息

<form action="" id="manage-project">
                <label for="" class="control-label">Project Title</label>
                <input type="text" class="form-control form-control-sm" name="name"  value="<?php echo isset($name) ? $name : '' ?>">
            </div><div class="form-group">
      <label for="" class="control-label">Project Group </label>
      <input type="text" class="form-control form-control-sm" name="pname" required value="<?php echo isset($pname) ? $pname : '' ?>">

    </div>
<div class="card-footer border-top border-info">
        <div class="d-flex w-100 justify-content-center align-items-center">
            <button class="btn btn-flat  bg-gradient-primary mx-2" form="manage-project">Save</button>
            <button class="btn btn-flat bg-gradient-secondary mx-2" type="button" onclick="location.href='index.php?page=project_list'">Cancel</button>
        </div>
    </div>
$('#manage-project').submit(function(e){
    e.preventDefault()
    start_load()
    $.ajax({
        url:'ajax.php?action=save_project',
        data: new FormData($(this)[0]),   

        cache: false,
        contentType: false,
        processData: false,
        method: 'POST',
        type: 'POST',
        success:function(resp){
            if(resp == 1){
                alert_toast('Data successfully saved',"success");
                setTimeout(function(){
                    location.href = 'index.php?page=project_list'
                },2000)
            }
        }
    })

This is the insertion query and I guess I need select query to validate the name.这是插入查询,我想我需要选择查询来验证名称。 help me out here帮我看看

function save_project(){
    extract($_POST);
    $data = "";
    foreach($_POST as $k => $v){
        if(!in_array($k, array('id','user_ids')) && !is_numeric($k)){
            if($k == 'description')
                $v = htmlentities(str_replace("'","&#x2019;",$v));
            if(empty($data)){
                $data .= " $k='$v' ";
            }else{
                $data .= ", $k='$v' ";
            }
        }
    }
    if(isset($user_ids)){
        $data .= ", user_ids='".implode(',',$user_ids)."' ";
    }
    if(empty($id)){
        $save = $this->db->query("INSERT INTO project_list set $data");
    }else{
        $save = $this->db->query("UPDATE project_list set $data where id = $id");
    }
    if($save){
        return 1;
    }
}
$check = $this->db->query("select name from project_list where and pname='$pname' or name='$name'")->num_rows;
    if($check > 0){
        return 2;
        exit;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果记录不存在则插入,如果存在则更新 - INSERT if record doesn't exist, UPDATE if exists Nodejs:注册用户时验证数据库中是否已经存在email - Nodejs: validate if email already exists in the database when registering user 如何检查名称是否已存在于 Firebase 实时数据库(Firebase 和 JS)中 - How to check if name already exists in Firebase Realtime Database (Firebase & JS) 如何检查 IndexedDB 数据库是否已存在? - How do I check if an IndexedDB database already exists? 如果文档已经存在,不想覆盖它 - Don't want to overwrite the document if it already exists 如果用户已存在于数据库中,我想通过 php get 方法发送错误消息,然后在没有 GET 变量的情况下重置 url - i want to send error message through php get method if user already exists in database but then reset the url without GET variable 我的帖子没有插入我的数据库中 - my post doesn't insert in my database 确认 MongoDB 数据库中是否已存在 email - confirming if an email already exists in a MongoDB database 使用 Ajax 查看电子邮件是否已存在于数据库中 - Using Ajax to see if email already exists in database 如果URL中数据库ID中的变量不存在,Node.js如何将URL重定向到错误 - Node.js how to redirect URL to error if variable from database id in URL doesn't exists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM