0

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; } } } } 
4
  • 3
    "but it not working" is not an acceptable problem description Commented Dec 16, 2013 at 7:08
  • Please don't use mysql_*; it is deprecated. Also, your code is wide open to SQL injection. Commented Dec 16, 2013 at 7:09
  • 2
    add a primary key to avoid duplicate entry Commented Dec 16, 2013 at 7:10
  • you have not executed first query '$check="SELECT * FROM contacts WHERE office_name = '$officeName'";', only checking num rows!! Commented Dec 16, 2013 at 7:15

4 Answers 4

1

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"; } 
Sign up to request clarification or add additional context in comments.

6 Comments

any one tell me why it is negative rated?
because this way you are doing an extra query(SELECT) and another to actually INSERT.
i showed mistake that @Abdulkarim Dorman do. he missed mysql_query, so i show that and posted. m i do any thing wrong?
i just realize user that he did mistake there. nothing else. thanks @GabrielC.Troia
dear (himanshu bhardiya) let me know how to redirect when I submit the same record to : index.php?addcontacts page I have written the following code in process.php page:
|
1

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.

Comments

0

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; } } 

} }

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.