<? 
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","******","******");
	
//select which database you want to edit
mysql_select_db("*******"); 

//If cmd is not hit
if(!isset($cmd)) 
{
   //display all the news
   $result = mysql_query("select * from Cars"); 
   
   //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
     
      echo "<a href='delete.php?cmd=delete&Id=$Id'>$Title - Delete</a>";
      echo "<br>";
    }
}
?>

<?
if($cmd=="delete")
{
    $sql = "DELETE FROM Cars WHERE Id=$Id";
    $result = mysql_query($sql);
    echo "Row deleted!";
}
?>

It shows up all the information, however when I press delete it does not do anything. Any suggestions?

Thanks

YOU Must write follwowing code in delete.php

if($_GET['cmd']=="delete")
{    $sql = "DELETE FROM Cars WHERE Id={$_GET['Id']}";    
      $result = mysql_query($sql);    echo "Row deleted!";
}

It seems you are not reading the $cmd value from the request

Add this in the top of your script.

$cmd = $_REQUEST;

hope it helps you

<? 
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","******","******");
	
//select which database you want to edit
mysql_select_db("*******"); 

//If cmd is not hit
if(!isset($cmd)) 
{
   //display all the news
   $result = mysql_query("select * from Cars"); 
   
   //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
     
      echo "<a href='delete.php?cmd=delete&Id=$Id'>$Title - Delete</a>";
      echo "<br>";
    }
}
?>

<?
if($cmd=="delete")
{
    $sql = "DELETE FROM Cars WHERE Id=$Id";
    $result = mysql_query($sql);
    echo "Row deleted!";
}
?>

It shows up all the information, however when I press delete it does not do anything. Any suggestions?

Thanks

Member Avatar for rajarajan2017

First get the value to process as utrivedi said

<?
   $command=$_GET['cmd'];
   $key=$_GET['id'];
   if ($command=='delete')
 {
    $sql = "DELETE FROM Cars WHERE Id=$key";
    $result = mysql_query($sql);
    echo "Row deleted!";
}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.