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
https://forums.phpfreaks.com/topic/223234-delete-entry-from-database/
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.

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>";
}

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>";

} 

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.