1

I have a music database and I'm trying to check whether the user entered a duplicate album. When both the album title and artist name are the same, it gives an error and does not insert the data as expected. It also works for when it's a different artist but the same album name. But when it's a new album by an artist already in the database, PHP executes both the if and else blocks.

function getDB(){
    try{
        $db = new PDO('mysql:host=localhost;dbname=test;charset=utf8mb4', '', '');
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        return $db;
    }catch(Exception $e){
        echo $e->getMessage();
    }

}

function duplicateAlbum(){
    $db = getDB();
    $stmt = $db->prepare("select * from artist join album on artist.id = album.artist_id where name = ? and title = ?");
    $stmt->execute(array($_POST['artist'],$_POST['title']));
    echo $stmt->rowCount() != 0;
    return $stmt->rowCount() != 0;
}

function echoResults(){
    $db = getDB();
    $albums = $db->prepare("select * from album where title = ?");
    $albums->execute(array($_POST['title']));
    $artists = $db->prepare("select * from artist where name = ?");
    $artists->execute(array($_POST['artist']));
    $results = array("albums" => $albums->fetchAll(PDO::FETCH_ASSOC), "artists" => $artists->fetchAll(PDO::FETCH_ASSOC));
    echo json_encode($results);
}

function addAlbum($artist, $title, $genre, $released){
    $db = getDB();
    $stmt = $db->prepare("select id from artist where name = ?");
    $stmt->execute(array($artist));
    $artistresult = $stmt->fetchAll(PDO::FETCH_ASSOC)[0]['id'];
    $stmt = $db->prepare("insert into album values (?,?,?,?)");
    $stmt->execute(array($title, $genre, $released,$artistresult));
}


if(!duplicateAlbum()){
      addAlbum($_POST['artist'],$_POST['title'],$_POST['genre'],$_POST['released']);
      echoResults();
     }
else echo "Duplicate album";
7
  • 1
    How many times does duplicateAlbum() gets called? Looks like it does twice. Commented Jul 20, 2017 at 23:54
  • What is your output? Commented Jul 20, 2017 at 23:56
  • echoResults just does a select * for both tables and sends them in JSON format to Javascript. However, in this case I get a JSON error on the Javascript side because it's trying to parse the "Duplicate album" string. Yet, it still adds the album to the database. Commented Jul 21, 2017 at 0:01
  • @AaronFeigenbaum what is echoResultS(); Commented Jul 21, 2017 at 0:29
  • Updated my post. Commented Jul 21, 2017 at 0:32

2 Answers 2

5

Is imposible to hit both (if/else) in the same run, if both things are happening is because you are running your script twice, or something is calling it again. I recommend you to analize your code execution flow

Sign up to request clarification or add additional context in comments.

5 Comments

It actually works if I have a return statement after echoResults but I don't know why it doesn't work without it.
Hmm, is there more code after your if/else statement ?
Yes, I have a catch block in case of a connection error and another else block (which connects to an if at the top checking that $_POST isn't empty.
Maybe you should post all your code. Sounds like you are jumping to conclusions. Consider using 'error_log' to see how many times your code is really executing.
Updated my post.
1

This sometimes happens when you have some exception inside if block. I also faced the same issue. It was happening because of some error in the method calls that I had in my if block. While debugging I can see the control going in both the block.

It might be possible that duplicateAlbum() or echoResults() is having some issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.