4

删除有效,页面重定向,但删除后的 url 是http://localhost:/manage_items.php?yesdelete=23为什么?它应该是manage_items.php

while ($row = $get_products->fetch()) {
  $item_id =  $row['item_id'];
  $user_id =  $row['user_id'];
  $item_name = $row['item_name'];
  $date = $row['add_date'];
  $image = $row['photopath'];
  $products .= "<br/><img src = $image><img> Item ID: $item_id UserID: $user_id NAME: 
                  $item_name Added on: $date &nbsp
                  <a href='item_edit.php?pid=$item_id'>Edit</a>&nbsp 
                  <a href='manage_items.php?deleted=$item_id'>Delete</a>";
}

//delete Item

if(isset($_GET['deleted'])) {
   echo 'delete this product?'.$_GET['deleted'].'<a 
         href="manage_items.php?yesdelete='.$_GET['deleted'].'">Yes<a/> 
         / <a href ="manage_items.php">No</a>';
   exit();
}

if(isset($_GET['yesdelete'])) {
  $deleteid = $_GET['yesdelete'];
  $sql = $db->exec("DELETE FROM item WHERE `item_id` = '$deleteid' LIMIT 1");
  $image_delete = 'file_to/$deleteid';

  if(file_exists($image_delete)) {
    unlink($image_delete);
  }
  header("Location: manage_items.php");
  exit();
}
4

3 回答 3

0

这就是问题所在。它在页面顶部的会话块中

session_start();

if(!isset($_SESSION['id']))

{
header("Location: yackimo_register_form.php");

exit();
}
$userID = $_SESSION['id'];
echo "";<<<------------

坏习惯

于 2013-10-23T05:55:23.220 回答
0

必须在任何类型的输出进入浏览器之前调用标头才能正常工作,即使我没有看到整个代码我看到你正在输出空白字符串:

  ?>
  <?php

删除所有这些地方和可能的其他地方可以完成输出 - 然后重定向将起作用

另外,我真的建议您使用正确的重定向:

function redirect($url)
{
    if (!headers_sent())
    {
        if (strtoupper($_SERVER['SERVER_PROTOCOL']) == 'HTTP/1.1')
            @header('HTTP/1.1 303 See Other', true, 303);
        else
            @header('HTTP/1.0 302 Found', true, 302);
        @header('Location: ' . $url);
        @header('Content-type: text/html');
    }

    echo '<html><head><title>Redirect</title><meta http-equiv="Refresh" content="0;URL='.htmlspecialchars($url).'"></head><body>'.
        '<a href="'.htmlspecialchars($url).'">Click here</a>'.
        '</body></html>';
    @ob_flush();
    exit();
}
于 2013-10-23T05:23:46.127 回答
0

看起来像一个标头已经发送的案例。

您必须执行以下操作。

  1. 删除代码块。

    ?> <?php

  2. 删除第一个之前的所有空格和换行符<?

  3. 删除最后一个?>或至少确保之后没有空格或换行符。
  4. 您可以使用输出缓冲ob_startob_end_flush

编辑:您还在重定向之前发送 html。在这里,我重新安排了您的代码。我已将删除块移至开头。

<?php
//delete Item

if(isset($_GET['yesdelete']))
{
    $deleteid = $_GET['yesdelete'];

    $sql = $db->exec("DELETE FROM item WHERE `item_id` = '$deleteid' LIMIT 1");

    $image_delete = 'file_to/$deleteid';

    if(file_exists($image_delete))
    {
        unlink($image_delete);

    }
    header("Location: manage_items.php");
    exit();
}
while ($row = $get_products->fetch())
{
    $item_id =  $row['item_id'];
    $user_id =  $row['user_id'];
    $item_name = $row['item_name'];
    $date = $row['add_date'];
    $image = $row['photopath'];
    $products .= "<br/><img src = $image><img> Item ID: $item_id UserID: $user_id NAME: 
                  $item_name Added on: $date &nbsp
                  <a href='item_edit.php?pid=$item_id'>Edit</a>&nbsp 
                  <a href='manage_items.php?deleted=$item_id'>Delete</a>";
}
if(isset($_GET['deleted']))
{
    echo 'delete this product?'.$_GET['deleted'].'<a 
           href="manage_items.php?yesdelete='.$_GET['deleted'].'">Yes<a/> 
           / <a href ="manage_items.php">No</a>';
    exit();
}
?>
于 2013-10-23T05:24:00.740 回答