Deleting Item using Date

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gregwhitworth
Forum Commoner
Posts: 53
Joined: Tue Oct 09, 2007 1:00 am
Location: Wasilla, Alaska

Deleting Item using Date

Post by gregwhitworth »

Okay, let's see if I can explain this in a way that makes sense. I have my database displaying news fields for a CMS in the following mannor:

Code: Select all

News Heading - Date
Description
-------------------------
in the admin area it is displayed as follows:

Code: Select all

Delete Button 
News Heading - Date
Description
What I want to accomplish is, when someone clicks the delete button I want it to delete that entry. The problem I have is connecting the delete button with the SQL item. Please help.

Here is the display for the admin area:

Code: Select all

while($col=mysql_fetch_assoc($result))
        {
        
        $col['date'] = date("M,d,Y");
        $pull = strtotime($col['pull']);
        
        if ($pull < $time) {
		$query = "DELETE FROM news WHERE pull <= $time";}
		
        	echo '<div class="newsitem">';
        		
        		//Delete button settings
        		echo '<form action="admin.php" method="post">'."\n";
	        	echo '<div class="deleteButton">'."\n";
	        	echo '<input type="submit" value="Delete" name="delete">'."\n";
	        	echo "</div>\n";
	        	
               //Overall Item Display Settings
				echo "<h4>".$col['header']."</h4>";
                echo date('m-d-Y',strtotime($col['date']))."\n";
                echo "<br><br>\n";
                echo $col['description']."<br>\n";
           echo "</div>";     
        }
Here is the PHP that processes the form:

Code: Select all

<?php
	
	//connection settings
	$username = "";
	$password = "";
	$hostname = "localhost";
	$select = "SELECT header,date,description,pull FROM news"; 
	$dbh = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
	$db_table = "news";
	print ("Connected to MySQL<br>");
	
	//form variables	
	$header = $_POST['header'];
	$date = $_POST['date'];
	$description = $_POST['description'];
	$pull = $_POST['pull'];
	
	$sql = "INSERT INTO $db_table(header,date,description,pull) VALUES('$header','$date','$description','$pull')";

	
	$selected = mysql_select_db("biglakebaptist",$dbh) or die("Could not select biglakebaptist");
	print ("Selected biglakebaptist<br>");
	
	$result = mysql_query($select) or die("Could not select news");
	print ("Selected news table<br>");	
	
	if (isset($_REQUEST['delete'])) {
		$query = "DELETE FROM news";}
	
	
	if (isset($_REQUEST['save']))
	{	
	
	if($result = mysql_query($sql, $dbh)) {
		echo "Your info has been added";		
	}
	else {
		echo "ERROR: ".mysql_error();
		}
	}		
		
	header("Location: admin_form.php");
			
	mysql_close($dbh);
	
?>
Last edited by gregwhitworth on Fri Oct 19, 2007 2:24 am, edited 1 time in total.
bob_the _builder
Forum Contributor
Posts: 131
Joined: Sat Aug 28, 2004 12:25 am

Post by bob_the _builder »

Hi,

Looks like you need to add a hidden field in your form posting the ID for the record you wish to delete.

ie:

Code: Select all

<input name="newsID" type="hidden" value="'.$col['newsID'].'" />
Then delete will be:

Code: Select all

if (isset($_REQUEST['delete'])) { 
                mysql_query("DELETE FROM news WHERE newsID = '.$_POST['newsID'].'");
 }



Think thats what you are looking for?

hth

ps Nice clean site you have
User avatar
gregwhitworth
Forum Commoner
Posts: 53
Joined: Tue Oct 09, 2007 1:00 am
Location: Wasilla, Alaska

Post by gregwhitworth »

ps Nice clean site you have
Not sure what site you're looking at?

Anyways, it did work, and thanks. My current hurdle is making news items be deleted if the pull date is less than the current date. Here is all of the php that concerns this:

Code: Select all

$time = strtotime("now");
$col['date'] = date("M,d,Y");
$pullDate = strtotime($col['pull']);
        
if ($pullDate < $time) {
mysql_query("DELETE FROM news WHERE pull = '$time'");
Thanks for all of your guys' help, I don't know what I would do without you guys.

--
Greg
bob_the _builder
Forum Contributor
Posts: 131
Joined: Sat Aug 28, 2004 12:25 am

Post by bob_the _builder »

HI,

The website in your profile link.

So you are trying to delete any news in the database that is older than todays date?
User avatar
gregwhitworth
Forum Commoner
Posts: 53
Joined: Tue Oct 09, 2007 1:00 am
Location: Wasilla, Alaska

Post by gregwhitworth »

yep -

Oh that one -
Thanks. I run a business with my wife. I do web design, development, and photography. My wife does artwork; so I came up with a site design and layout to encompass both aspects. Her site can be seen at karenwhitworth.net

The gallery isn't done yet, but I wanted to get the hang of data driven stuff before I tackled it, but you can get a look of all of the prints she has for sale at the moment.

Thanks again for the help

--
Greg
User avatar
gregwhitworth
Forum Commoner
Posts: 53
Joined: Tue Oct 09, 2007 1:00 am
Location: Wasilla, Alaska

Post by gregwhitworth »

Ok - Since no one decided to help me out in the past day - I did some of my own research and I have the following script created to achieve the deletion of an item if the pull date is before today:

Code: Select all

mysql_query("DELETE FROM news WHERE pull < $time);
The above deletes the entry just fine, but it also makes it so that when you insert a new entry it deletes the previous one. For example I have the following:

Code: Select all

Heading 1
Date 1
Description 1
I then enter another entry and instead of seeing them both I get the following:

Code: Select all

Heading 2
Date 2
Description 2
So, I changed my approach on the script and did the following:

Code: Select all

mysql_query("DELETE FROM news WHERE pull < time() );
This allowed me to see all of my values, but would not delete any of the values, no matter what the pull date was. PLEASE HELP!!

I have had enough coffee and am now going to bed leaving it to God and the PHP guru's in Canada (HA K!)

--
Greg
Post Reply