I am attempting to insert records for Artists, Songs and Record Labels, whilst checking that the data does not already exist in the Database.
The following code is from Mike Fenwick.
    <?php
    $query = "SELECT id FROM table WHERE unique1=value1 AND unique2=value2…";
    $select_result = mysql_query($query);
    if (!mysql_num_rows($select_result)) {
            $query = "INSERT INTO table SET unique1=value1 AND unique2=value2…";
            $insert_result = mysql_query($query);
            $id = mysql_insert_id();
    }
    else {
            $row = mysql_fetch_assoc($select_result);
            $id = $row['id'];
    }
    return $id;
    ?>
I need to modify this to check if three unique values exist already (from 3 separate tables), and if not, insert them. Here is my attempt:
<?php   
            $query = "SELECT id FROM artistsTable WHERE artistName='Beyonce'";
            $select_result = mysql_query($query);
            if (!mysql_num_rows($select_result)) {
                    $query = "INSERT INTO table SET artistName='Beyonce' AND artistImage='beyonce.jpg'";
                    $insert_result = mysql_query($query);
                    $artistID = mysql_insert_id();
            }
            else {
                    $row = mysql_fetch_assoc($select_result);
                    $artistID = $row['artistID'];
            }
            return $artistID;
            $query = "SELECT id FROM recordLabelTable WHERE labelName='Columbia Records'";
                $select_result = mysql_query($query);
                if (!mysql_num_rows($select_result)) {
                    $query = "INSERT INTO table SET labelName='Columbia Records'";
                    $insert_result = mysql_query($query);
                    $labelID = mysql_insert_id();
            }
            else {
                    $row = mysql_fetch_assoc($select_result);
                    $labelID = $row['labelID'];
            }
            return $labelID;
            $query = "SELECT id FROM songTable WHERE trackTitle='Crazy in Love' AND artistID=".$artistID." AND labelID=".$labelID."";
                $select_result = mysql_query($query);
                if (!mysql_num_rows($select_result)) {
                    $query = "INSERT INTO songTable SET trackTitle='Crazy in Love' AND artistID=".$artistID." AND labelID=".$labelID."";
                    $insert_result = mysql_query($query);
                    $songID = mysql_insert_id();
            }
            else {
                    $row = mysql_fetch_assoc($select_result);
                    $songID = $row['songID'];
            }
            return $songID;
    ?>
I'm assuming that there must be a more efficient way to do this. Any ideas would be much appreciated.
 
     
    