Abdulkarim Dorman
Abdulkarim Dorman

Reputation: 11

how to prevent same record to be inserted twice in mysql using php

The following is my code that I have written for not inserting same data I would like if the record exist in mysql then it should show me error message that the record already exist the else part should insert record to database but it not working can any one help me plz the help would be highly appreciated

function addcontact()
    {
        if(isset($_POST['addContact']))
        {
            $officeName = strip_tags($_POST['office_name']);
            $contactName = strip_tags($_POST['contactName']);
            $contactNo = strip_tags($_POST['contactNo']);
            $digitalNo = strip_tags($_POST['digitalNo']);
        $mobileNo = strip_tags($_POST['mobileNo']);

        $check="SELECT * FROM contacts WHERE office_name = '$officeName'";
        if(mysql_num_rows($check) != 0)
         {
            echo "Already in Exists<br/>";
        }else
                     {          
                        $sql = mysql_query("INSERT INTO contacts (office_name, contact_no, 
                          digital_no, mobile_no) VALUES 
                         ('$contactName','$contactNo','$digitalNo','$mobileNo')") or die(mysql_error());
        if($sql)
        {
            header("Location: index.php?admin&done");  exit;
        }
        else
        {
            header("Location: index.php?admin&failed");  exit;  
        }
    }
}
}

Upvotes: 0

Views: 3346

Answers (4)

vishnu reddy
vishnu reddy

Reputation: 123

you can handle it from database side. write a stored procedure such a way that first check weather the record is in database or not if exist then ignore it and get back the text "Record already exist", if not exist then insert it to table. use conditional statements in mysql.

Upvotes: 0

Gabriel C. Troia
Gabriel C. Troia

Reputation: 3340

Add the ON Duplicate KEY Update. This way you don't need to check if the record already exists, which means you don't need an extra select query just to check. If it exists, nothing happens.

INSERT INTO contacts (office_name, contact_no, digital_no, mobile_no) 
VALUES ('$contactName','$contactNo','$digitalNo','$mobileNo')
ON DUPLICATE KEY UPDATE office_name = office_name

And set the office_name to be the primary key or a unique index.

Upvotes: 1

himanshu bhardiya
himanshu bhardiya

Reputation: 81

you did mistake here.

$check="SELECT * FROM contacts WHERE office_name = '$officeName'";
    if(mysql_num_rows($check) != 0)
     {
        echo "Already in Exists<br/>";
    }

just add mysql_query like

$check=mysql_query("SELECT * FROM contacts WHERE office_name = '$officeName'");
    if(mysql_num_rows($check) != 0)
     {
        echo "Already in Exists<br/>";
    }

or you can also use like

    $name=$_POST['username'];

$q="select * from login where name='$name' ";
$rs=mysql_query($q);
if(mysql_fetch_row($rs)>0)
{
    echo "already exist";
}
else
{
    $msg="done";
}

Upvotes: 1

Suleman Ahmad
Suleman Ahmad

Reputation: 2113

There is missing one step, your first query is not executed, please try this:-

function addcontact()
{
    if(isset($_POST['addContact']))
    {
        $officeName = strip_tags($_POST['office_name']);
        $contactName = strip_tags($_POST['contactName']);
        $contactNo = strip_tags($_POST['contactNo']);
        $digitalNo = strip_tags($_POST['digitalNo']);
    $mobileNo = strip_tags($_POST['mobileNo']);

    $check= mysql_query("SELECT * FROM contacts WHERE office_name = '{$officeName}'");
    if(mysql_num_rows($check) != 0)
     {
        echo "Already in Exists<br/>";
    }else
                 {          
                    $sql = mysql_query("INSERT INTO contacts (office_name, contact_no, 
                      digital_no, mobile_no) VALUES 
                     ('$contactName','$contactNo','$digitalNo','$mobileNo')") or die(mysql_error());
    if($sql)
    {
        header("Location: index.php?admin&done");  exit;
    }
    else
    {
        header("Location: index.php?admin&failed");  exit;  
    }
}

} }

Upvotes: 0

Related Questions