Do not redirect correctly with header

Deletion action and page redirection, but URL after deletion http://localhost:/manage_items.php?yesdelete=23Why is this? It should bemanage_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
source share
3 answers
heading

must be called before any output goes to the browser for proper operation, even I do not see all the code. I see that you are outputting an empty string to:

  ?>
  <?php

delete all such places and possible others where the exit can be performed - then the redirection will work

Also, I really suggest you use the correct redirection:

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();
}
0
source

Similar to the case of an already sent case.

You must do the following.

: 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();
}
?>
0

That was the problem. It was in my session block at the top of the page

session_start();

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

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

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

Bad habit

0
source

All Articles