/    Sign up×
Community /Pin to ProfileBookmark

insert avoiding duplicate data

Hi

I have a website conected to a db. I add data automatically via php INSERT. I get data from csv and xml, an dthen INSERT into db
My problem, is that I duplicate data, so my db grows a lot.
How can I check if the data already exists? I was trying this code, but it does not work.


*****************

$query = “SELECT * FROM val WHERE lake_id=’$lai’ AND parameter_id=’$ipa’ and valu=’$vpa’ AND date=’$fe'”;
$results = mysql_query(“SELECT COUNT(*
) FROM val lake_id=’$lai’ AND parameter_id=’$ipa’ and valu=’$vpa’ AND date=’$fe'”);
$name = mysql_result($results,0);
if ($name > 0) {
echo “existe”;

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJun 01.2011 — Usually the best way is to set up a "unique" index on the columns that you do not want duplicated. Then when you want to do a data insert, either use MySQL's "REPLACE INTO" command (instead of an "INSERT"), or since that is a non-standard SQL command, instead use the "INSERT INTO . . . ON DUPLICATE KEY" syntax.
×

Success!

Help @vladyman spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...