Jump to content

Recommended Posts

Okay so I'll try to make this seem as simple as possible.  It really is pretty simple...

 

The below code simply deletes a specific file chosen by the user and also removes it from the database.

 

The problem is when a user selects a page, for example: process.php

 

and the below code will for some reason delete the last record in the database and the file associated with it rather than the selected page.

 

<?PHP
// Attempt database connection
                  $connect = mysql_connect($hostname, $username, $password);
                  if($connect == false){
                     echo('<p class="error">We are having technical difficulties and apologize for the inconvenience. Please try again later.</p>');
                  }
                  
                  // Select database
                  $db = mysql_select_db($dbid);
               
                  // Query the database
                  $select = "SELECT * FROM pages";
                  $result = mysql_query($select);
               
                  switch ($_GET['delete']) {
                  
                     case ($_GET['delete'] == "?") :
                        echo "<h3>Select Page to Delete:</h3>";                        
                        while($row = mysql_fetch_array($result)){
                           extract($row);
                           if ($type == "basic") {
                              echo '<p>
                                    <a href="delete.php?delete='.$page.'"><img src="images/delbasp.jpg" width="125" height="125" alt="Edit '.$title.' Page" /></a>
                                    <p class="details">
                                       <strong>Page Name:</strong> '.$page.'.php<br/>
                                       <strong>Title:</strong> '.$title.'<br/><br/><br/><br/><br/>
                                       <a href="delete.php?delete='.$page.'">Delete</a>
                                    </p>
                                   </p>';
                              
                           } else {
                              echo '<p>
                                    <a href="delete.php?delete='.$page.'"><img src="images/delgalp.jpg" width="125" height="125" alt="Edit '.$title.' Page" /></a>
                                    <p class="details">
                                       <strong>Page Name:</strong> '.$page.'.php<br/>
                                       <strong>Title:</strong> '.$title.'<br/><br/><br/><br/><br/>
                                       <a href="delete.php?delete='.$page.'">Delete</a>
                                    </p>
                                   </p>';
                           }
                        }
                     break;
                     
                     case ($_GET['delete']) :
                        //Check if user is trying to delete index page
                        if ($page == "index") {
                           echo '<h3>Error:</h3>';
                           echo '<p class="error">We are unable to delete this page.</p>';
                           echo "<meta http-equiv='refresh' content='1;URL=delete.php'>";   
                        } else {                        
                           //Delete the actual file
                           $myFile = $page.".php";
                           unlink("../".$myFile);
                                                                     
                           // Build query to delete row from table
                           $delete = "DELETE FROM pages where title = '$title'";
                           echo $delete;
                           $result = mysql_query($delete);
                                       
                           if ($result == true) {
                              echo ('<h3>Success!</h3><p class="success">Deleting '.$title.' page.');   
                              echo "<meta http-equiv='refresh' content='1;URL=delete.php'>";   
                           } else {
                              echo '<p class="error">There was a problem deleting this page.</p>';
                           }
                        }         
                     break;
                  }                  
               ?>

Link to comment
https://forums.phpfreaks.com/topic/187214-problem-deleting-rows/
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.