I will do my best to articulate the problem.
I am checking for duplicates on an Insert statement by placing the query into an array and using a While loop to check each row in the database table. I have the logic working except it's still giving me the error when only one of the conditions is met in any row. What I'm looking for is how to produce the error only when both are met for any single row.
Example: An artist can have multiple songs, but I don't want them to have duplicate songs. Meanwhile multiple artists should be able to have songs with the same title. However, I'm getting the error even if one of the conditions is true in any row, preventing me from accomplishing this.
So, how do I check both conditions to be met for a single row and not across multiple rows.
Thanks!
// Insert user input Music into the Database
// Check for duplicates
public function insertMusic() {
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
or die('Error connecting to DB');
$song_title = mysqli_real_escape_string($dbc, trim($this->song_title));
$song_length = mysqli_real_escape_string($dbc, trim($this->song_length));
$song_album = mysqli_real_escape_string($dbc, trim($this->song_album));
$song_artist = mysqli_real_escape_string($dbc, trim($this->song_artist));
$album_art = mysqli_real_escape_string($dbc, trim($this->album_art));
// Check database for duplicates
$duplicate_query = "Select * from Music";
$duplicate_results = mysqli_query($dbc, $duplicate_query)
or die('Error querying DB');
$isDuplicate = true;
while ($row = mysqli_fetch_array($duplicate_results)) {
// Something with these 2 conditions are the issue??
if ( ($song_title != $row['SongTitle']) && ($song_artist != $row['SongArtist']) ) {
$isDuplicate = false;
} else {
$isDuplicate = true;
echo '<p class="error">Song is already in the Database!</p>';
break;
}
}
if ($isDuplicate === false) {
$query = "INSERT INTO Music (id, dateAdded, SongTitle, SongLength, SongAlbum, SongArtist, AlbumArt)" .
"VALUES (0, NOW(), '$song_title', '$song_length', '$song_album', '$song_artist', '$album_art')";
// echo $query;
$result = mysqli_query($dbc, $query)
or die('Error querying DB 1');
mysqli_close($dbc);
echo '<p class="success">Success!</p>';
}
}
Aucun commentaire:
Enregistrer un commentaire