PHP delete script works only on local, and not on

2019-08-10 02:48发布

问题:

I am building a website for the purpose of learning php, developing website locally and then transferring to remote. One of the folders in root is 'clients', which consists of scripts to list, insert, update and delete client feedback from a form. Locally (XAMPP) all the scripts work perfectly. But when I transfer it to remote, everything works fine, EXCEPT for the delete script. When I try to delete the entries on remote, it simply gives a page not found error. I have attached the relevant codes as below. I would be grateful for assistance. 1. Below is the page with the delete script.

<?php require_once('scripts/db_delete_feedback.php'); ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Delete Feedback</title>
<link href="../css/admin.css" rel="stylesheet" type="text/css" />
</head>

<body>
<h1>Delete Feedback</h1>
<p><a href="index.php">Admin menu</a></p>
<p>Please confirm that you want to delete the following record. This cannot be undone.</p>
<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>.">
<p><strong>Client Name:</strong><?php echo $feedback['name']; ?></p>
<p><strong>Feedback summary:</strong><?php echo $feedback['punch']; ?></p>
<p><strong>Rating:</strong><?php echo $feedback['rating']; ?></p>
<?php if ($photos) { ?>
<p>The following photos are linked with this client feedback. Select the checkbox next to each photo you want to delete at the same time as this feedback. To delete images without deleting the feedback, use <a href="list_photos.php">photo management page</a>.</p>
<table width="600">
<?php
$num = 0;
foreach ($photos as $photo) { ?>
  <tr>
    <td><input type="checkbox" name="photo[]" id="photo<?php echo $num;?>" value="<?php echo $photo['filename']; ?>" />
      <label for="photo<?php echo $num++; ?>" class="checkbox_label">Delete photo</label></td>
    <td><img src="../images/<?php echo $photo['filename']; ?>" width="200" alt="" /><br /><?php echo $photo['caption']; ?></td>
    </tr>
    <?php } ?>
</table>
<?php } ?>
<p>
  <input type="submit" name="delete_feedback" id="delete_feedback" value="Confirm Deletion" />
  <input type="submit" name="cancel" id="cancel" value="Cancel" />
  <input name="returnto" type="hidden" id="returnto" value="<?php echo $returnto; ?>" />
  <input name="client_id" type="hidden" id="client_id" value="<?php echo $client_id; ?>" />
</p>
</form>
</body>
</html>
  1. Below is the 'db_delete_feedback.php' file included by the above script:

        <?php
    $errors = array();
    require_once('library.php');
    try {
      require_once('db_definitions.php');
      $client_id = checkId('client_id', 'list_feedback.php');
      if (isset($_POST['delete_feedback'])) {
        // delete and redirect
        $dbWrite->delete('clients', "client_id = $client_id");
      $dbWrite->delete('client2photo', "client_id = $client_id");
      if (isset($_POST['photo'])) {
        foreach ($_POST['photo'] as $filename) {
          $dbWrite->delete('photos', "filename = '$filename'");
          unlink($destination . '/' . $filename);
        }
      }
      header('Location: ' . $_POST['returnto']);
      exit;
      }  elseif (isset($_POST['cancel'])) {
        header('Location: ' . $_POST['returnto']);
        exit;
      }
      $feedback = getFeedback($dbRead, $client_id);
      $photos = getRelatedPhotos($dbRead, $client_id);
      if (isset($_SERVER['HTTP_REFERER'])) {
        $returnto = $_SERVER['HTTP_REFERER'];
      } else {
        $returnto = 'list_feedback.php';
      }
    } catch (Exception $e) {
      echo $e->getMessage();
    }
    

The included library.php file contains the path to zend framework library. Please help as I am unable to find the cause of this issue.

回答1:

If you got an "page not found error", it probably doesn't have anything to do with the script itself since the script wasn't found by the server.

Make sure you

  1. have uploaded the delete script for sure.
  2. The script file has proper file permissions, so the server could access that file.
  3. If that doesn't work, check your servers error log file (e.g. /var/log/apache2/error.log) for PHP errors.
  4. Check in which cases the redirect does occur. You do not check, if there is any URL given in $_POST['returnto'] before doing the redirect (header( 'Location: ' . $_POST['returnto'] );). Maybe there wasn't any URL given and thus the redirect goes to nowhere – hence the "page not found error".


回答2:

Finally, I found the cause. It was a silly coding error. The extra . in the form action

This doesn't work on the remote server, but works on Local:

<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>.">

And this works on the remote server:

<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">