advancedfuture Posted January 8, 2009 Share Posted January 8, 2009 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(); ?> Link to comment https://forums.phpfreaks.com/topic/140090-error-with-foreach-deleting-database-results/ Share on other sites More sharing options...
.josh Posted January 9, 2009 Share Posted January 9, 2009 -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? Link to comment https://forums.phpfreaks.com/topic/140090-error-with-foreach-deleting-database-results/#findComment-732987 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.