<?php

   include "pdo_connection.php";

   if($_REQUEST["stodo"] == 1) {
      $sql = "UPDATE _9a_playlist SET Songs = ?, Date = NOW() WHERE User_ID = ? AND ID = ?";
      $values = array($_REQUEST["songs"], $_REQUEST["user"], $_REQUEST["id"]);

      $stmt = $pdo->prepare($sql);
      $stmt->execute($values);

      if($stmt->rowCount() == 0) echo "ERROR# - No songs saved!";
      else echo "OK";
   }

   if($_REQUEST["stodo"] == 0) {
      $sql = "SELECT Songs FROM _9a_playlist WHERE User_ID = ? AND ID = ?";
      $values = array($_REQUEST["user"], $_REQUEST["PlayListID"]);

      $stmt = $pdo->prepare($sql);
      $stmt->execute($values);

      if($stmt->rowCount() == 0) echo "No playlist songs1.";
      else {
         $row = $stmt->fetch();
         if(!$row || $row == "") echo "No playlist songs2.";
         else {
            $songs = explode("|", $row["Songs"]);
            if(!$songs || count($songs) == 0) echo "No playlist songs3.";
            else {
               $output = "";
               foreach ($songs as $song) {
                  $sql = "SELECT _9a_song.ID, _9a_song.Artist_ID, _9a_song.Title, _9a_arist.Name"
                        ."  FROM _9a_song JOIN _9a_arist ON _9a_song.Artist_ID = _9a_arist.ID"
                        ." WHERE _9a_song.ID = ?";

                  $stmt = $pdo->prepare($sql);
                  $stmt->execute(array($song));

                  if($stmt->rowCount() == 0) echo "No playlist songs4.";
                  else {
                     $row = $stmt->fetch();
                     $output = $output.$row["ID"]."|".$row["Name"]."|".$row["Title"]."|".$row["Artist_ID"]."#";
                  }
               }
               echo trim($output, "#");
            }
         }
      }
   }

?>