Location: PHPKode > projects > Kwalbum > kwalbum/ajax/addFavorite.php
<?php
/**
 * Add an item to the user's favorites.
 *
 * If it is not in favorites already then add it.
 * No checking is done for permissions other than being logged in.
 * If a user tries to add an item they do not have permission to view, it will
 * still be added.  It will just not show up when viewing.
 *
 * @version 2.1.1
 * @package kwalbum
 * @since 2.0 Sep 9, 2008
 */

$dir = pathinfo(__FILE__, PATHINFO_DIRNAME).'/../include/';
require_once $dir.'DBConnection.php';
$DB = new DBConnection();
require_once $dir.'verifyLogin.php';
$errorMsg = '<p class="error">Favorite Not Saved!</p>';

if (!$id = (int)$P['id'])
{
	if (TEST_MODE)
		echo '<p class="error">Invalid ID</p>';
	else
		echo $errorMsg;
	exit;
}

if (USER_ID)
{
	$userId = USER_ID;
	$query = "SELECT FavoriteItemIdFk FROM ".FAVORITE_TABLE.
		" WHERE FavoriteUserIdFk=$userId AND FavoriteItemIdFk=$id";
	if (0 < $DB->Query($query)->num_rows)
		echo 'This is already in your favorites!';
	else
	{
		$insertData = array (
			'FavoriteItemIdFk' => $id,
			'FavoriteUserIdFk' => $userId
		);
		if (false !== $DB->Insert(FAVORITE_TABLE, $insertData))
			echo 'Favorite Added';
		else
			if (TEST_MODE)
				echo $DEBUG_INFO;
			else
				echo $errorMsg;
	}
}
else
	if (TEST_MODE)
		echo '<p class="error">Not Logged In</p>';
	else
		echo $errorMsg;

Return current item: Kwalbum