# Check for record; if it exists, get the row id, if not, write a new record

**URL:** <https://forum.kirupa.com/t/check-for-record-if-it-exists-get-the-row-id-if-not-write-a-new-record/315032>\
**Category:** Uncategorized\
**Created:** [October 15, 2010, 3:41pm UTC](https://forum.kirupa.com/t/check-for-record-if-it-exists-get-the-row-id-if-not-write-a-new-record/315032 "2010-10-15T15:41:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![DavetriesPHP](https://avatars.discourse-cdn.com/v4/letter/d/839c29/32.png) [@DavetriesPHP](https://forum.kirupa.com/u/DavetriesPHP)\
**Post date:** [October 15, 2010, 3:41pm UTC](https://forum.kirupa.com/t/check-for-record-if-it-exists-get-the-row-id-if-not-write-a-new-record/315032/1 "2010-10-15T15:41:38Z")

</div>

Hello,  
This is a google maps mashup. I want the user to be able to add a tag to the marker he is creating. I’m using PHP to write to 3 different MySQL tables : markers, tags, and a 3rd table, markers\_tags. Of course, the 3rd table is just a 2-column table that saves the ids of the rows in the first two tables. I base this idea on Brian Haveri’s excellent tutorial [http://www.kirupa.com/developer/php/relational\_db\_design4.htm](http://www.kirupa.com/developer/php/relational_db_design4.htm)  
Just substitute my tags table for his categories table.

So I have the following code, and it works, IF I don’t care about having duplicated tags :

// Insert new row into markers table  
$query = sprintf(“INSERT INTO markers " .  
" (id, codepostale, nom, marche\_lundi, marche\_mardi, marche\_mercredi, marche\_jeudi, marche\_vendredi, marche\_samedi, marche\_dimanche, lat, lng, remarques, liensource ) " .  
" VALUES (NULL, ‘%s’,’%s’, ‘%s’, ‘%s’, ‘%s’, ‘%s’, ‘%s’, ‘%s’, ‘%s’, ‘%s’, ‘%s’, ‘%s’, ‘%s’);”,  
mysql\_real\_escape\_string($codepostale),  
mysql\_real\_escape\_string($nom),  
mysql\_real\_escape\_string($marche\_lundi),  
mysql\_real\_escape\_string($marche\_mardi),  
mysql\_real\_escape\_string($marche\_mercredi),  
mysql\_real\_escape\_string($marche\_jeudi),  
mysql\_real\_escape\_string($marche\_vendredi),  
mysql\_real\_escape\_string($marche\_samedi),  
mysql\_real\_escape\_string($marche\_dimanche),  
mysql\_real\_escape\_string($lat),  
mysql\_real\_escape\_string($lng),  
mysql\_real\_escape\_string($remarques),  
mysql\_real\_escape\_string($liensource));

$resultmarkertable = mysql\_query($query);  
if (!$resultmarkertable) {  
die('Invalid query: ’ . mysql\_error());  
}  
$last\_insert\_marker\_id = mysql\_insert\_id();  
//above stores the id of the insert

// Insert new row into tags table  
//I think id below is not necessary since it’s auto increment?  
$query2 = sprintf(“INSERT INTO tags " .  
" (tag\_id, tag ) " .  
" VALUES (NULL, ‘%s’);”,

mysql\_real\_escape\_string($tag));  
$resulttagtable = mysql\_query($query2);  
if (!$resulttagtable) {  
die(‘Invalid query: ’ . mysql\_error());  
}  
$last\_insert\_tag\_id = mysql\_insert\_id();  
//above stores the id of the insert  
//now, below, lets insert into markers\_tags table  
$query3 = sprintf(“INSERT INTO markers\_tags " .  
" (marker\_id, tag\_id )” .  
" VALUES (’$last\_insert\_marker\_id’, ‘$last\_insert\_tag\_id’);",

mysql\_real\_escape\_string($tag));  
$resultmarkers\_tags = mysql\_query($query3);  
if (!$resultmarkers\_tags) {  
die('Invalid query: ’ . mysql\_error());  
}

?\>

However, I think an IF, ELSE statement in place of my simple insert tag query. I need to run a select on the tag table to see if the tag to be inserted already exists. If it does, we get the id and save THAT to the variable “$last\_insert\_tag\_id” and if that SELECT statement comes up with 0 values, I need to run the regular old insert statement above. Something like this

//check to see if tag already exists  
$tagcheck = mysql\_query(“SELECT ID FROM `tags` WHERE tag = ‘$tag’”)  
IF, ELSE, etc.  
But I can’t figure it out. Any ideas?
