Jump to content

Error with foreach deleting database results


advancedfuture

Recommended Posts

So I having a problem on one of my pages. The page displays all related advertisements. However when I select the ad I want to delete and press the form button it just refreshes the page without deleting it! Anyone able to see what the problem is in my code? Much thanks!

 

     <?php 
	//***********************************
	//***********************************
	//          SHOW THE USER ADS.
	//           
	//***********************************
	//***********************************

	//GET THE USERS ADS FROM THE DATABASE
	$query = "SELECT * FROM advertisements WHERE username = '$username'";
	$results = mysql_query($query);
	$num = mysql_num_rows($results);

	echo '<form id="form1" name="form1" method="post" action="profile.php">';
	echo '<table align="left" width="95%" border="0">';		

	if($num == 0)
	{
		echo "<center><strong>Sorry! You have no advertisements</strong></center>";
	}

	else
	{
		//ECHO HEADING FOR ADS TABLE
		echo '<tr><td bgcolor="#0066FF"><input type="checkbox" id="all" name="all" value=""></td>';
		echo '<td bgcolor="#0066FF"><strong>Title:</strong></td>';
		echo '<td bgcolor="#0066FF"><strong>Tagline:</strong></td>';
		echo '<td bgcolor="#0066FF"><strong>City:</strong></td>';
		echo '<td bgcolor="#0066FF"><strong>Phone:</strong></td>';
		echo '<td bgcolor="#0066FF"><strong>Service:</strong></td>';
		echo '<td bgcolor="#0066FF"><strong>Zip:</strong></td></tr>';

		//BEGIN LOOP TO OUTPUT ADS FOR USER
		while($rec=mysql_fetch_array($results))
		{
			$adid = $rec['adid'];
			$advertisement_title = $rec['advertisement_title'];
			$advertisement_tagline = $rec['advertisement_tagline'];
			$advertisement_city = $rec['advertisement_city'];
			$advertisement_phone = $rec['advertisement_phone'];
			$advertisement_service = $rec['advertisement_service'];
			$zip_code = $rec['zip_code'];
			echo '<tr><td><input type="checkbox" id="'.$adid.'" name="'.$adid.'" value="'.$adid.'"></td>';
			echo '<td>'.$advertisement_title.'</td>';
			echo '<td>'.$advertisement_tagline.'</td>';				
			echo '<td>'.$advertisement_city.'</td>';						
			echo '<td>'.$advertisement_phone.'</td>';
			echo '<td>'.$advertisement_service.'</td>';
			echo '<td>'.$zip_code.'</td></tr>';
		}

		echo '</table>';
		echo '<input type="submit" name="delete" id="delete" value="Delete Checked" />';
		echo '</form>';


		//IF THE USER DELETES ADS CYCLE THROUGH ALL THE CHECKBOXES
		if($_POST['delete'])
		{
			foreach($_POST as $adid)
			{
			$query = "DELETE FROM advertisements WHERE adid='$adid' LIMIT 1";
			$results = mysql_query($query) or die(mysql_error());
			}

			//REFRESH THE USERS ADS LIST
			if($results)
			{
			echo '<meta http-equiv="refresh" content="0"/>';
			}
		}
	}
mysql_close();
?>

-Form action is profile.php. Is this script profile.php?  Probably is, but you never know..

 

-View source on your form, make sure that your form checkbox name and value are being populated.

 

-You have your form displayed before the delete query, so when you hit submit, you aren't going to immediately see the change.  You have to refresh the page.  Did you refresh the page, or check in your database to see if the row is still there?

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.