Why is this short php script not deleting a record?

Discussion in 'PHP' started by tyler_durden, Mar 27, 2007.

  1. #1
    I can't figure out why this simple script will not delete a record. This is the 3rd different script I have tried, so I think it may be something i'm missing. The $remid is coming from a previous page, where the $remid is actually in the url as "delete-record.php?remid=636369". Furthermore, the script is going to the 2nd header listed, which shows that the remid is being seen/found.
    <?php
      // get  remid
      $remid = $_GET['remid'];
        
      // if the script has been called without ID or the user hit "Cancel" just return to listing
      if (empty($remid)) { 
        Header("Location: reminders.php"); 
        exit; 
      }
        
      
      $query = "DELETE FROM reminders WHERE remid='".$remid."'";
      $result = $database->query($query);
    
      Header("Location: my-reminders.php"); 
      exit; 
    ?>
    Code (markup):
    Here is my database table. I'm wondering, can you only delete a record by a key or index field? The field i'm trying to delete by is not.
    CREATE TABLE `reminders` (
      `date_field` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
      `email` varchar(200) NOT NULL default '',
      `comment` blob NOT NULL,
      `recur_num` tinyint(3) unsigned NOT NULL default '0',
      `notify_num` tinyint(3) unsigned NOT NULL default '0',
      `notify_val` varchar(14) NOT NULL default '',
      `recurring` char(3) default 'no',
      `recur_val` varchar(14) NOT NULL default '',
      `adv_notice` char(3) default 'no',
      `remid` varchar(14) NOT NULL default '',
      `subject` varchar(100) NOT NULL default '',
      `usrname` varchar(50) NOT NULL default '',
      `recipient` varchar(50) NOT NULL default '',
      KEY `date_field` (`date_field`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
    Code (markup):

     
    tyler_durden, Mar 27, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Echo mysql_affected_rows(), and see if it finds a matching record with this ID. Also echo the querysting or $remid and see if it contains the expected content.
     
    nico_swd, Mar 27, 2007 IP
  3. tyler_durden

    tyler_durden Peon

    Messages:
    340
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes, it looks like I have some errors somewhere else. Thanks!
     
    tyler_durden, Mar 27, 2007 IP
  4. tyler_durden

    tyler_durden Peon

    Messages:
    340
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Ok, I found what the problem is, but I don't know why. The code below displays the list properly, EXCEPT the remid is the same on every delete link (highlighted in red)

    <?
    function db2me( $time )
    {    
    return date( "M d, Y", strtotime( $time ) );
    }
    if( !($result = mysql_query("SELECT * FROM reminders WHERE usrname='$my->username' ORDER BY date_field ASC ")) ): die("Cannot query mysql server");
    elseif( !mysql_num_rows( $result ) ): 
    printf("No records found for %s", $my->username );
    else: 
    
    $date_field=mysql_result($result,$i,"date_field");
    $subject=mysql_result($result,$i,"subject");
    $recipient=mysql_result($result,$i,"recipient");
    $recurring=mysql_result($result,$i,"recurring");
    $remid=mysql_result($result,$i,"remid");
    
    
    echo "<table border='1'>\n".   
    "<tr>\n".   
    "<td>Date</td>".   
    "<td>Occasion</td>".   
    "<td>Recipient</td>". 
    "<td>Reoccuring</td>".
    "<td>Edit</td>".
    "<td>Delete</td>".  
    "</tr>\n";   
    while( $array = mysql_fetch_assoc( $result ) ):
        echo "<tr>\n";
        printf( "<td>%s</td>\n", db2me( $array['date_field'] ) );
        printf( "<td>%s</td>\n", $array['subject'] );
        printf( "<td>%s</td>\n", $array['recipient'] );
        printf( "<td>%s</td>\n", $array['recurring'] );
        printf( "<td>edit</td>\n" );
        [COLOR="Red"]printf( "<td><a href='delete-reminder.php?remid=$remid'>Delete</a></td>\n" );[/COLOR]    echo "</tr>\n";
       endwhile;
     echo "</table>";
    endif;
    ?>
    Code (markup):
     
    tyler_durden, Mar 27, 2007 IP