Classified script help

I have a classified script that doesn’t post all the information it needs to. It posts all but the ‘information’. Not sure why, the title, price, location, picture, all post fine, just the information doesn’t.

I looked in my database, and it doesn’t post the information in there either - so my guess is it doesn’t get to the database to be pull to the ad itself.

Here’s the code:

[php]<?php
class CAd
{
//Constructor for CAd. Gives this class access to MySQL.
function CAd(&$sql)
{
$this->sql = $sql;
}

//Load all of the ad information from the db into class member variables
function LoadAd($id)
{
	//Query for the info
	$this->sql->query("SELECT * FROM ads where ID = '%s'", $id);
	$ad = $this->sql->getrowset();
	
	//"Extract" it into member variables
	class_extract($ad[0], $this);
	
	//Throw the ad's category's name into $this->category_name
	$this->sql->query("SELECT name FROM categories WHERE ID = '%s'", $this->category);
	$this->category_name = $this->sql->getresult();
}

function LoadOwner()
{
	//Throw the ad's owner's name into $this->owner_name
	$this->sql->query("SELECT * FROM users WHERE ID = '%s'", $this->owner);
	$this->owner_data = $this->sql->getrow();
}

//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))
	{
		move_uploaded_file($image['tmp_name'], AD_IMAGES.addslashes($title));
		$image_url = SERVER_ROOT.AD_IMAGES.addslashes($title);
	}
	//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;
}

//Delete an ad from the MySQL db, and delete the image that accompanies it.
function DeleteAd()
{
	//If image is hosted on this server...
	if(!!strstr($this->image, SERVER_ROOT.AD_IMAGES.addslashes($this->title)))
		unlink(AD_IMAGES.addslashes($this->title)); //Delete it...
	
	//Delete the ad from MySQL
	$this->sql->query("DELETE FROM ads WHERE ID = '%s'", $this->ID);
	
	return TRUE;
}

//Change an ad's title, price, and info in MySQL
function EditAd($title, $price, $info, $category)
{
	$this->sql->query("UPDATE ads SET title = '%s', price = '%s', info = '%s', category = '%s' WHERE ID = '%s'", $title, $price, $info, $category, $this->ID);
	
	return TRUE;
}

//Return the HTML code to output the ad on the category page. (Small sized)
function OutputAd()
{
	$out = '<table width="98%" align="center" border="0">';
	$out .= '<tr><td bgcolor="#AFD5FC"><b>Category:</b> <a href="index.php?cat='.$this->category.'">'.$this->category_name.'</a> - <b>Post Date:</b> '.date(DATE_FORMAT, $this->date).'</td></tr>';
	$out .= '<tr><td bgcolor="#D6D6D6">';
	$out .= '<table><tr valign="center"><td width="10%" bgcolor="#D6D6D6"><img width="75" height="56" src="'.$this->image.'"></td><td width="80%"><a href="ad_details.php?ad='.$this->ID.'">'.strtoupper($this->title).'</a><p><b>AD #:</b>'.$this->ID.'</td><td width="10%"><font color="red">$'.number_format($this->price, 2).'</font><br>'.$this->location.'</td></tr></table></td></tr></table>';
	
	return $out;
}

//Return the HTML code to output the ad on the ad page. (Full sized)
function OutputFullAd()
{
	//Increase ad views by 1. (For "Hottest Ads")
	$this->sql->query("UPDATE ads SET views = views+1 WHERE ID = '%s'", $this->ID);
	
	$out = '<table width="100%"><tr><td class="#AFDFC">';
	$out .= '<b>Ad #</b>'.$this->ID.' - <b>Posted Date: </b>'.date(DATE_FORMAT, $this->date);
	$out .= '</td></tr><tr><td class="#D6D6D6">';
	$out .= '<center><img src="'.$this->image.'" height=300 width=420><br><font color="red"><b>$'.number_format($this->price, 2).'</b></font><p><font size="+1">'.strtoupper($this->title).'</font><p></center>'.$this->info.'<p>';
	if($this->phone != '')
		$out .= '<b>Contact Info: </b>'.$this->phone.'<br>';
	$out .= '<b>Location: </b>'.$this->location.'<br>';
	$out .= '<b>Posted By: </b>'.$this->owner_data['name'].'<br>';
	$out .= '<a href="contact.php?i='.$this->ID.'">E-mail Seller</a><br>';
	$out .= '</td></tr></table>';
	
	return $out;
}

//Return the HTML code for creating a new ad
function OutputNewAd()
{
	$out = '<br><br><br><table width="98%" align="center"><tr><td bgcolor="#D6D6D6"><h3><center>Post a New Ad</center></h3></td></tr><form action="'.PHP_SELF.'" method=POST enctype="multipart/form-data">';
	
		
	$out .= '<tr><td bgcolor="#AFD5FC" align="center"><b>Category</b>: &nbsp;&nbsp;&nbsp;&nbsp;<select name=category>';
	
	//Grab an array of bottom level categories [0] => ID; [1] => name;
	$cat = new CCategory($this->sql);
	$cats = $cat->OutputCategoryArray();

	//Add each one to a <select>
	foreach($cats as $category)
		$out .= '<option value="'.$category[0].'">'.$category[1].'</option>';
	$out .= '</select></td></tr><tr><td bgcolor="#AFD5FC"></td></tr>';
	$out .= '<tr><td bgcolor="#D6D6D6" align="center"><b>Title</b>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=text name=title></td></tr><tr><td bgcolor="#D6D6D6"></td></tr>';
	$out .= '<tr><td bgcolor="#AFD5FC" align="center"><b>Price</b>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=text name=price></td></tr><tr><td bgcolor="#AFD5FC"></td></tr>';
	$out .= '<tr><td bgcolor="#D6D6D6" align="center"><b>Location</b>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=text name=location></td></tr>';
	$out .= '<tr><td bgcolor="#AFD5FC" align="center"><b>Information</b>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><textarea name=info cols=50 rows=10></textarea></td></tr><tr><td bgcolor="#AFD5FC"></td></tr>';
	$out .= '<tr><td bgcolor="#D6D6D6" align="center"><b>Image Upload</b>: <input type=hidden name="MAX_FILE_SIZE" value="10000000"><input type=file name="image_f"></td></tr><tr><td bgcolor="#D6D6D6"></td></tr>';
	$out .= '<tr><td bgcolor="#AFD5FC" align="center"><input type=submit name=NewAd value="Create Ad"></form></td></tr></table>';

	return $out;
}

function OutputEditAd()
{
	$out = '<form action="'.$_SERVER['REQUEST_URI'].'" method=POST>';
	$out .= '<b>Title</b>: <input type=text name=title value="'.$this->title.'"><br>';
	$out .= '<b>Category</b>: <select name=category>';
	
	//Grab an array of bottom level categories [0] => ID; [1] => name;
	$cat = new CCategory($this->sql);
	$cats = $cat->OutputCategoryArray();
	
	//Add each one to a <select>, making it selected if it's the current category
	foreach($cats as $category)
		$out .= '<option value="'.$category[0].'"'.(($this->category == $category[0]) ? ' selected' : '').'>'.$category[1].'</option>';
		$out .= '<option value="'.$category[0].'"'.(($this->category == $category[test]) ? ' selected' : '').'>'.$category[2].'</option>';
		$out .= '<option value="'.$category[0].'"'.(($this->category == $category[1212]) ? ' selected' : '').'>'.$category[3].'</option>';
	$out .= '</select><br>';
	$out .= '<b>Price</b>: <input type=text name=price value="'.$this->price.'"><br>';
	$out .= '<b>Location</b>: <input type=text name=location value="'.$this->location.'"><br>';
	$out .= '<b>Information</b>: <br><textarea name=info cols=50 rows=10>'.$this->info.'</textarea>';
	$out .= '<p><a href="ad_details.php?ad='.$this->ID.'">View Ad</a><hr>';
	$out .= '<input type=submit name=EditAd value="Edit Ad"> <input type="submit" name="DeleteAd" value="Delete Ad"></form>';

	return $out;
}

}
?>
[/php]

I couldn’t find anything wrong with it… can anyone help?

Thanks! :)

Wow, lots of code. And I’m too lazy to go through it all :P But I know where to look:

[php]
//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))
    {
        move_uploaded_file($image['tmp_name'], AD_IMAGES.addslashes($title));
        $image_url = SERVER_ROOT.AD_IMAGES.addslashes($title);
    }
    //If $image is the URL to an image hosted elsewhere.
    else
        $image_url = $image;

    // Create Query - REQUIRED FOR ECHOING
    $sql = "INSERT INTO ads SET owner = '".$owner_id;
    $sql .= "', title = '".$title."', image = '".$image_url;
    $sql .= "', category = '".$category."', price = '".$price;
    $sql .= "', info = '".$info."', date = '".time()."', location = '".$location."'";

    echo $sql;
    
    //Query to add this information into the db
    $this->sql->query($sql);
    
    return TRUE;
} 

[/php]

Try this and tell us what you see.

Thanks for the reply! :slight_smile:

You could have the script die, outputting the query, Of course do not leave it this way, you don’t want anyone knowing the query, but if I did that right, it should kick out the full mysql query then stop…

[code]
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))
    {
        move_uploaded_file($image['tmp_name'], AD_IMAGES.addslashes($title));
        $image_url = SERVER_ROOT.AD_IMAGES.addslashes($title);
    }
    //If $image is the URL to an image hosted elsewhere.
    else
        $image_url = $image;

//Not Writing Info, Die to see if is present in insert query
die(“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");

    //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;
} [/code]

Thanks, but I get this error:

Parse error: syntax error, unexpected ',' in /home/boatingr/public_html/CAd.php on line 61

So what do you do? You search the CAd.php line 61 for an obsolete comma :) Parse errors are about the easiest errors to fix in PHP.

Sponsor our Newsletter | Privacy Policy | Terms of Service