Jump to content

Delete Entry From Database


daebat

Recommended Posts

I'm working on an old cms I built a few years ago and for some reason my Delete page isn't working.  It has to be something simple I'm overlooking.  The database connection is made just above this code and is pulling the thumb and title listed by id.  When I push delete I'm getting the cmd=delete&id=2 passed on so something is up with my last if statement (I think).  Here is the code:

 

if(!isset($cmd)) 
{
   //display all the news
   $result = mysql_query("select * from verizon order by id"); 
   

   
   //run the while loop that grabs all the news scripts
   while($r=mysql_fetch_array($result)) 
   { 
      //grab the title and the ID of the enws
      $title=$r["title"];//take out the title
      $id=$r["id"];//take out the id
      $thumb=$r["thumb"];
      
      echo "<div id='delete'>
      	     <table>
	<tr>
		<td width='350px'><strong>$title</strong></td><td><a href='verizondelete.php?cmd=delete&id=$id'>  Delete</a></td>
	</tr>
	<td colspan='2' align='center'><img src='../upload/verizon/$thumb'></td>
    </table>
         
          </div><br />";
    }
}

if($cmd=="delete")
{
    $sql = "DELETE FROM verizon WHERE id='$id'";
    $result = mysql_query($sql);
    echo "<h1 style='text-align:center;'>deleted!</h1>";
} 

}
?>

Link to comment
Share on other sites

Ok, I got it one step forward.  By setting

$cmd=$_GET['cmd'];

I got it to run through the if statement but now it's not performing the actual query to delete the item from the database.  I tried setting up

$id=$_GET['id'];

but it didn't work. 

Link to comment
Share on other sites

nah I just didn't show the portion just before it.  Here is the full script with my new stuff added:

 

if($session->logged_in){

$cmd=$_GET['cmd'];


//If cmd has not been initialized
if(!isset($cmd)) 
{
   //display all the news
   $result = mysql_query("select * from verizon order by id"); 
   

   
   //run the while loop that grabs all the news scripts
   while($r=mysql_fetch_array($result)) 
   { 
      //grab the title and the ID of the enws
      $title=$r["title"];//take out the title
      $id=$r["id"];//take out the id
      $thumb=$r["thumb"];
      
      echo "<div id='delete'>
      	     <table>
	<tr>
		<td width='350px'><strong>$title</strong></td><td><a href='verizondelete.php?cmd=delete&id=$id'>  Delete</a></td>
	</tr>
	<td colspan='2' align='center'><img src='../upload/verizon/$thumb'></td>
    </table>
         
          </div><br />";
    }
}

if($cmd=="delete")
{
    $sql = "DELETE * FROM verizon WHERE id=$id";
    $result = mysql_query($sql);
    echo "<h1 style='text-align:center;'>deleted!</h1>";
} 

}
else{
echo "<div style='text-align:center'>[<a href='../main.php'>Please Login</a>]   </div>";

}

 

The script is now returning the echo of "Deleted!" but the query isn't running because it never deletes.

Link to comment
Share on other sites

i have re written it a bit for you try this

 

if($session->logged_in){
$cmd=$_GET['cmd'];
$delID=$_GET['id'];
if(isset($cmd)&&$cmd=='delete')	{
    	$sql = "DELETE FROM verizon WHERE id='$delID'";
    	$result = mysql_query($sql);
    	echo "<h1 style='text-align:center;'>deleted!</h1>";
} 
//If cmd has not been initialized
if(!isset($cmd)) {
   		//display all the news
   		$result = mysql_query("select * from verizon order by id"); 
  		//run the while loop that grabs all the news scripts
  		while($r=mysql_fetch_array($result)) { 
      		//grab the title and the ID of the enws
     		 $title=$r["title"];//take out the title
      		$id=$r["id"];//take out the id
      		$thumb=$r["thumb"];
     		 echo "<div id='delete'>
     		<table>
			<tr>
				<td width='350px'><strong>$title</strong></td><td><a href='verizondelete.php?cmd=delete id=$id'>  Delete</a></td>
			</tr>
			<tr>
				<td colspan='2' align='center'><img src='../upload/verizon/$thumb'></td>
			</tr>
    		</table>
          	</div><br />";
    	}
}
}
else {
echo "<div style='text-align:center'>[<a href='../main.php'>Please Login</a>]   </div>";
}

Link to comment
Share on other sites

Still no dice... I'm getting the confirmation echo of 'deleted!' but something must be wrong with this:

 

if(isset($cmd)&&$cmd=='delete')	

{
    
$sql = "DELETE FROM verizon WHERE id='$delID'";  /* <------------- */ 

$result = mysql_query($sql); /* <------------ */
    
echo "<h1 style='text-align:center;'>deleted!</h1>";

} 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.