Page 1 of 1

Deleting Individual records.

Posted: Thu Oct 23, 2008 1:14 pm
by CoolAsCarlito
Okay with my following code when a user clicks delete on a record in the html table I want it to delete that record and that's it instead of having it go to showname.php?action=edit&id='.$row['id'].' So how do I go about accomplishing that.

Code: Select all

 
<html>
<head>
<title>Show Name Script</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
 
<body>
 
<?php
 
/* showname.php */
/* This form after submission takes the results of the form and inserts the values into the database as a new show name is created. */
 
require ('database.php');
 
//This code runs if the form has been submitted
if (isset($_POST['submit']))
    {
      // checks if the show name is in use
      if (!get_magic_quotes_gpc())
      {
         $_POST['showname'] = addslashes($_POST['showname']);
      }
      $showname = $_POST['showname'];
      $check = mysql_query("SELECT showname FROM shownames WHERE showname = '$showname'")   or die(mysql_error());
      $check2 = mysql_num_rows($check);
    
      //if the name exists it gives an error
      if ($check2 != 0)
      {
         die('Sorry, the show name '.$_POST['showname'].' is already in use.');
      }
    
      // now we insert it into the database
      $insert = "INSERT INTO shownames (type, showname) VALUES ('".$_POST['type']."','".$_POST['showname']."')";
      
      $add_show = mysql_query($insert) or die(mysql_error());
    }
 
print '<center><caption><strong>Add A Show</strong></caption></center>';
print '<table border="1" style="margin: auto; width: 60%;">';
print '<form name="showname" method="post">';
print '<tr><td>Show Type:</td> ';
print '<td><select name="type"><option value="">Select a Show Type</option><option value="Weekly Show">Weekly Show</option><option value="Pay Per View">Pay Per View</option></select></td>';
print '<tr><td>Show Name:</td> ';
print '<td><input name="showname" type="text"></td></tr>';
print '<tr><th colspan=2><input type="submit" name="submit" value="Add Show Name" /><input name="sumbitted" type="hidden" value="TRUE"></th></tr></table></form><br><br><br>';
 
print '<center><caption><strong>List of Shows</strong></caption></center>';
print '<table width="60%" border="1" align="center">';
print '<tr><th align="center">Type</th><th align="center">Show Name</th><th align="center">Edit</th><th align="center">Delete</th></tr>';
 
if(!isset($_GET['action']) && !isset($_POST['name']))
{
   //Define the query
   $query = "SELECT * FROM shownames";
   if ($r = mysql_query ($query)) // Run the query.
   {
       if (mysql_num_rows($r) > 0)
       {
           // Retrieve and print every record
           while ($row = mysql_fetch_array ($r))
           {
              print '<tr><td align="center">'.$row['type'].'</td><td align="center">'.$row['showname'].'</td><td align="center"><a href="showname.php?action=edit&id='.$row['id'].'"><center>Edit</center></a></td><td align="center"><a href="showname.php?action=delete&id='.$row['id'].'">Delete</a></td></tr>';
           }
        }else{
           print "<center>No Shows</center>\n";
        }
   }else{
      die ('<p>Could not retrieve the data because <b>' . mysql_error() . '</b>. The query was '."$query.".'</p>');
   } //End of query IF
   print '</table>';
   
}
?>
 
</body>
</html>
 

Re: Deleting Individual records.

Posted: Thu Oct 23, 2008 2:12 pm
by aceconcepts

Code: Select all

 
if(isset($_GET['action']) && isset($_GET['id']))
{
   if($_GET['action']=="delete");
   {
      $id=$_GET['id'];
      //delete from database where id =$id
   }
}
 

Re: Deleting Individual records.

Posted: Thu Oct 23, 2008 2:26 pm
by CoolAsCarlito
So you mean it should look like this?

Code: Select all

 
if(!empty($_GET['action']) && !empty($_GET['id']))
{
   switch($_GET['action'])
   {
      case 'edit':
         $query = "SELECT * FROM shows WHERE id = '".$_GET['id']."'";
         $res = mysql_fetch_array(mysql_query($query));
         print('<form action="'.$_SERVER['PHP_SELF'].'" method="post" name="form1">');
         print('<table border=1 cellpadding=5 cellspacing=0 width=350>');
         print('<tr><td>Name of show:</td><td><input type="text" name="name" value="'.$res['showname'].'"/></td></tr>');
         print('<tr><td>Show Type Type:</td><td><select name="type">');
         $types = array('Weekly Show','Pay Per View');
         foreach($types as $type)
         {
            if($type == $res['type'])
            {
               print('<option value="'.$type.'" selected="selected">'.$type.'</option>');
            }else{
               print('<option value="'.$type.'">'.$type.'</option>');
            }
         }
         print('</select></td></tr>');
         print('<tr><th colspan=2><input type="hidden" name="id" value="'.$_GET['id'].'" /><input type="submit" value="Edit Show" /></th></tr></table></form></center>');
      break;
      case 'delete':
         {
        $id=$_GET['id'];
        //delete from database where id =$id
         }
      break;
   }
}
 
 

Re: Deleting Individual records.

Posted: Thu Oct 23, 2008 3:56 pm
by aceconcepts
That looks rather good - have you tested it?

Re: Deleting Individual records.

Posted: Thu Oct 23, 2008 4:06 pm
by CoolAsCarlito
Yeah but it didn't do anything.