/** * Inserts an artist in database. * * @return bool True on success or false on failure */ public function insert() { require_once $_SERVER['DOCUMENT_ROOT'] . '/server/lib/DatabaseConnection.php'; $connection = new DatabaseConnection(); $query = $connection->prepare('INSERT INTO `artist` (`name`, `mbid`, `summary`, `country`) VALUES (:name, :mbid, :summary, :country);'); $query->bindValue(':name', $this->name, PDO::PARAM_STR); $query->bindValue(':mbid', $this->mbid, PDO::PARAM_STR); $query->bindValue(':summary', $this->summary, PDO::PARAM_STR); $query->bindValue(':country', $this->country, PDO::PARAM_STR); if ($query->execute()) { $this->id = $connection->lastInsertId(); //returns insertion was successfully processed return true; } //returns insertion has encountered an error return false; }
/** * Inserts an album in database. * * @return bool True on success or false on failure */ private function insert() { require_once $_SERVER['DOCUMENT_ROOT'] . '/server/lib/DatabaseConnection.php'; $connection = new DatabaseConnection(); $query = $connection->prepare('INSERT INTO `album` (`name`, `mbid`, `artist`, `year`, `disk`, `country`, `mbidGroup`) VALUES ( :name, :mbid, :artist, :year, :disk, :country, :mbidGroup);'); $query->bindValue(':name', $this->name, PDO::PARAM_STR); $query->bindValue(':mbid', $this->mbid, PDO::PARAM_STR); $query->bindValue(':artist', $this->artist, PDO::PARAM_INT); $query->bindValue(':year', $this->year, PDO::PARAM_INT); $query->bindValue(':disk', $this->disk, PDO::PARAM_INT); $query->bindValue(':country', $this->country, PDO::PARAM_STR); $query->bindValue(':mbidGroup', $this->mbidGroup, PDO::PARAM_STR); if ($query->execute()) { $this->id = $connection->lastInsertId(); //returns insertion was successfully processed return true; } //returns insertion has encountered an error return false; }
/** * Inserts a track in database. * * @return bool True on success or false on failure */ public function insert() { require_once $_SERVER['DOCUMENT_ROOT'] . '/server/lib/DatabaseConnection.php'; $connection = new DatabaseConnection(); $query = $connection->prepare('INSERT INTO `track` (`file`, `album`, `year`, `artist`, `title`, `bitrate`, `rate`, `mode`, `size`, `time`, `track`, `mbid`, `updateTime`, `additionTime`, `composer`) VALUES (:file, :album, :year, :artist, :title, :bitrate, :rate, :mode, :size, :time, :track, :mbid, :update, :addition, :composer);'); $query->bindValue(':file', $this->file, PDO::PARAM_STR); $query->bindValue(':album', $this->album, PDO::PARAM_INT); $query->bindValue(':year', $this->year, PDO::PARAM_INT); $query->bindValue(':artist', $this->artist, PDO::PARAM_INT); $query->bindValue(':title', $this->title, PDO::PARAM_STR); $query->bindValue(':bitrate', $this->bitrate, PDO::PARAM_INT); $query->bindValue(':rate', $this->rate, PDO::PARAM_INT); $query->bindValue(':mode', $this->mode, PDO::PARAM_STR); $query->bindValue(':size', $this->size, PDO::PARAM_INT); $query->bindValue(':time', $this->time, PDO::PARAM_INT); $query->bindValue(':track', $this->track, PDO::PARAM_INT); $query->bindValue(':mbid', $this->mbid, PDO::PARAM_STR); $query->bindValue(':update', time(), PDO::PARAM_INT); $query->bindValue(':addition', time(), PDO::PARAM_INT); $query->bindValue(':composer', $this->composer, PDO::PARAM_STR); if ($query->execute()) { $this->id = $connection->lastInsertId(); //return true to indicate a successful track insertion return true; } //return false to indicate an error during track insertion return false; }
/** * Create user with provided informations. * * @param object $user User with his values attributes * @param string $error The returned error message * * @return bool True if the user is created */ public function create($user, &$error) { $error = ''; require_once $_SERVER['DOCUMENT_ROOT'] . '/server/lib/DatabaseConnection.php'; $connection = new DatabaseConnection(); $query = $connection->prepare('INSERT INTO `user` (`login`, `name`, `email`, `password`, `status`) VALUES (:login, :name, :email, :password, :status);'); $query->bindValue(':login', $this->login, PDO::PARAM_STR); $query->bindValue(':name', $this->name, PDO::PARAM_STR); $query->bindValue(':email', $this->email, PDO::PARAM_STR); $query->bindValue(':password', md5($this->password), PDO::PARAM_STR); $query->bindValue(':status', $this->status, PDO::PARAM_INT); if ($query->execute() && $query->rowCount() > 0) { $this->id = $connection->lastInsertId(); //return true to indicate a successful user creation return true; } $error = $query->errorInfo()[2]; //try to return intelligible error if ($query->errorInfo()[1] === 1062 || $query->errorInfo()[2] === 'UNIQUE constraint failed: user.login') { $error = 'login `' . $this->login . '` already exists'; } //return false to indicate an error occurred while creating user return false; }