简体   繁体   中英

How do I modify this MySQL INSERT Query for multiple rows?

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.

Using basic inset / ignore syntax you could do something like this.

A couple of inserts to put in the artist details and label details, then an INSERT based on a SELECT:-

<?php   
    $query = "INSERT IGNORE INTO artistTable (artistName, artistImag) VALUES('Beyonce', 'beyonce.jpg')";
    $insert_result = mysql_query($query);

    $query = "INSERT IGNORE INTO labelTable (labelName) VALUES('Columbia Records')";
    $insert_result = mysql_query($query);

    $query = "INSERT IGNORE INTO songTable (trackTitle, artistID, labelID)
            SELECT 'Crazy in Love', a.artistID, b.labelID
            FROM artistTable a
            INNER JOIN labelTable b
            ON a.artistName = 'Beyonce'
            AND a.artistImag = 'beyonce.jpg'
            AND b.labelName = 'Columbia Records'";
    $insert_result = mysql_query($query);
    $songID = mysql_insert_id();
    return $songID;
?>

As @LoganWayne says, you probably should use MySQLi .

<?php   

/* ESTABLISH CONNECTION */

$con=mysqli_connect("Host","Username","Password","Database"); /* REPLACE NECESSARY DATA */

if(mysqli_connect_errno()){

echo "Error".mysqli_connect_error();
}


/*              FOR artistsTable TABLE              */

$query = "SELECT id FROM artistsTable WHERE artistName='Beyonce'";
$select_result = mysqli_query($con,$query); /* EXECUTE QUERY */
if (mysqli_num_rows($select_result)==0) { /* IF QUERY'S RESULT IS 0 */

   $query = "INSERT INTO table SET artistName='Beyonce' AND artistImage='beyonce.jpg'";
   $insert_result = mysqli_query($con,$query); /* EXECUTE INSERT QUERY */

} /* END OF IF */

else {

   while($row = mysqli_fetch_array($select_result)){
   $artistID = mysqli_real_escape_string($con,$row['artistID']); /* ESCAPE STRING */
   } /* END OF WHILE LOOP */

} /* END OF ELSE */


/*              FOR recordLabelTable TABLE              */

$query = "SELECT id FROM recordLabelTable WHERE labelName='Columbia Records'";
$select_result = mysqli_query($con,$query); /* EXECUTE SELECT QUERY */

if (mysqli_num_rows($select_result)==0) { /* IF QUERY'S RESULT IS 0 */
   $query = "INSERT INTO table SET labelName='Columbia Records'";
   $insert_result = mysqli_query($con,$query); /* EXECUTE INSERT QUERY */
}

else {
   while($row = mysqli_fetch_array($select_result)){
   $labelID = mysqli_real_escape_string($con,$row['labelID']); /* ESCAPE STRING */
   }
}


/*              FOR songtable TABLE              */

$query = "SELECT id FROM songTable WHERE trackTitle='Crazy in Love' AND artistID='$artistID' AND labelID='$labelID'";

$select_result = mysqli_query($con,$query); /* EXECUTE SELECT QUERY */
if (mysqli_num_rows($select_result)==0) {

   $query = "INSERT INTO songTable SET trackTitle='Crazy in Love' AND artistID='$artistID' AND labelID='$labelID'";
   $insert_result = mysqli_query($con,$query); /* EXECUTE QUERY */

} /* END OF IF */

else {
   while($row = mysqli_fetch_array($select_result)){
   $songID = mysqli_real_escape_string($con,$row['songID']);
   } /* END OF WHILE LOOP */
}

?>

SUMMARY:

  • Used at least MySQLi instead of deprecated MySQL.
  • Replaced fetch_assoc() function with fetch_array() function.
  • Used mysqli_real_escape_string() function to prevent some of SQL injections .
  • Cleaned your code. You have misplaced apostrophes(') and double quotes(") hanging around.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM