1

I want to insert new record in database if not already present. I know I can do it if I make that column unique but cant do this as there are several redundant records already present . So i wish any new record I insert should only be inserted if not already present.

Sample table for reference

id name

1   a

2   b

3   c
sam
  • 688
  • 1
  • 12
  • 35

3 Answers3

1

Before inserting do a select query:

Select id from tablename where name = 'a' limit 1;

Then check, if the result has rows. If it does not have rows execute the insert statement.

Redlab
  • 3,110
  • 19
  • 17
0

INSERT IGNORE INTO... You can find more info on already accepted answer on another question.

Community
  • 1
  • 1
Bojan Radaković
  • 430
  • 3
  • 12
0

I will use pseudo-code to explain . Use a select query with a WHERE that gonna select only value/values that is/are equal to the value/values of the input type . You will have to store the values in variables , one variable for the input type and one variable to store the value for the select query.

Before that don't forget to store variables because if you do a search it will read the value but not gonna store it and the second reason is it's gonna help you for the if and else if .

Also i will recommend you to see POST method and REQUEST method and logic operators ( ex: && , || , etc ... )

You should use a if and a else if .

The if could be: if variable1 = variable 2

echo "Data already exist" ;

end of the if

the else if could be :

Insert query end of the else

You should echo the value that you get from the select query just to be sure for your test.

A.V
  • 141
  • 1
  • 12