如何为多行修改此MySQL INSERT查询?

时间:2014-06-09 08:46:52

标签: php mysql

我正在尝试为艺术家,歌曲和唱片标签插入记录,同时检查数据库中是否已存在数据。

以下代码来自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;
    ?>

我需要修改它以检查是否已存在三个唯一值(来自3个单独的表),如果不存在,则插入它们。这是我的尝试:

<?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;


    ?>

我假设必须有一种更有效的方法来做到这一点。任何想法都会非常感激。

2 个答案:

答案 0 :(得分:2)

使用基本的插入/忽略语法,你可以做这样的事情。

要插入艺术家详细信息和标签详细信息的几个插入内容,然后是基于SELECT的INSERT: -

<?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;
?>

正如@LoganWayne所说,你可能应该使用MySQLi。

答案 1 :(得分:1)

<?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 */
}

?>

内容:

  • 至少使用 MySQLi 而不是弃用 MySQL。
  • fetch_array()函数替换 fetch_assoc()函数。
  • 使用 mysqli_real_escape_string()函数来阻止某些 SQL注入
  • 清理您的代码。你放错了撇号(&#39;)和双引号(&#34;)。