//Create a new ad in the MySQL table, and upload the given image. $image is a reference to the image in $_FILES
function NewAd($owner_id, $title, $image, $category, $price, $info, $location)
{
$this->sql->query("SELECT * FROM ads WHERE title = '%s'", $title);
//Die if there's another ad with the same name
if($this->sql->num_rows() != 0)
{
echo "<script>alert('Sorry. Another ad exists with this title. Please choose another title.');</script>";
return FALSE;
}
//If $image is an array (i.e. not a link to an offsite hosted image)
if(is_array($image))
{
$filext = explode(".",$image['name']);
$filext = $filext[(count($filext)-1)];
echo $image['name'] . '<br>';
move_uploaded_file($image['tmp_name'], AD_IMAGES.addslashes($title).'.'.$filext);
$image_url = SERVER_ROOT.AD_IMAGES.addslashes($title).'.'.$filext;
}
elseif(empty($image))
$image_url = 'http://www.racingrides.com/images/noimage.gif';
//If $image is the URL to an image hosted elsewhere.
else
$image_url = $image;
//Query to add this information into the db
$this->sql->query("INSERT INTO ads SET owner = '%s', title = '%s', image = '%s', category = '%s', price = '%s', info = '%s', date = '%s', location = '%s'", $owner_id, $title, $image_url, $category, $price, $info, time(), $location);
return TRUE;
}