-1

I have a problem in delete item. My problem is when I click on delete the button the item does not disappear but when I refresh it it does disappear.

And this is my code :

index.php

<?php
    $config = mysql_connect("localhost", "root", "") or die (mysql_error());
    $db = mysql_select_db("darith_upload_img",$config) or die (mysql_error());
?>
<!DOCTYPE HTML>
<html>
    <head></head>
<body>

        <form action="upload_process.php" enctype="multipart/form-data" method="POST">
            <input type="file" name="img" /><br />


            <input type="submit" value="upload Now" />

        </form>
        <?php
            $result = mysql_query("SELECT * FROM table_img");
            while($row = mysql_fetch_array($result))

            {

            $id = $row['id_img'];
                ?>
            <table border="1">
                    <tr>
                        <td><?php echo $row['id_img']; ?></td>
                        <td width="200" ><?php echo $row['location_img']; ?></td>
                        <td><?php echo '<img src="' . $row['location_img'] . '" width="60"/>'; ?></td>
                        <td><a href="index.php?id=<?php echo $id;?> " onClick="return confirm('Are you sure?')">Delete</a></td>
                    </tr>
                </table>
        <?php
            }

        ?>

        <?php
            if(isset($_GET['id'])){
                $id_img=$_GET['id'];

                $q_del=mysql_query("DELETE FROM table_img where id_img ='$id_img'");
                header("Location: http://example.com/path/to/index.php");

            }
        ?>
</body>

</html>

upload_process.php

<?php
    $config = mysql_connect("localhost", "root", "") or die (mysql_error());
    $db = mysql_select_db("darith_upload_img", $config) or die (mysql_error());

    if(isset($_FILES["img"]["tmp_name"]))
    {
        move_uploaded_file($_FILES["img"]["tmp_name"], "picture/". $_FILES["img"]["name"]);


        $location = "picture/".$_FILES["img"]["name"];

    $save = mysql_query("INSERT INTO table_img (location_img) VALUES ('$location')");

    header("Location: http://example.com/path/to/index.php");
    }
    exit();



?>
mas.morozov
  • 2,666
  • 1
  • 22
  • 22
Darith
  • 9
  • 7

4 Answers4

2
header("Location: http://example.com/path/to/index.php");

This code will not refresh the page after deletion, because you can send headers only before the first byte of response body, otherwise they are ignored.

Put your deletion code on the very top of the page (just after $db = ... line) to get header function working.

hakre
  • 193,403
  • 52
  • 435
  • 836
mas.morozov
  • 2,666
  • 1
  • 22
  • 22
  • There doesn't seem to be any output before that line (unless there is some debug output). You are looking at the wrong file. – John Dvorak Aug 31 '13 at 08:59
  • @JanDvorak No I am looking the right file, see where deletion occurs. – mas.morozov Aug 31 '13 at 09:03
  • there is no need to redirect after deletion, however – John Dvorak Aug 31 '13 at 09:07
  • @JanDvorak You will end up with crowd of users, occasionally refreshing pages with deletion queries in URL and smashing your database :) – mas.morozov Aug 31 '13 at 09:14
  • @mas.morozov: You are still guessing, because if output buffering is enabled, this is not a problem (and for IO reasons, output buffering should be enabled). Better tell the OP how to actually look for error reporting so that he can find out at which line actually the problem was. – hakre Aug 31 '13 at 09:50
1

Put the DELETE part above to display part. This way when your SELECT runs. The Row is already deleted.

Bytheway this is a general good practice for all kind of modifications for the DB. For INSERTS and UPDATES to. This way all the changes are reflected.

If you are changing things in multiple tables and display data from these tables on other parts of the page (like tags on many sites as an example), the best is to just put all such handling at the very top of the page. And do all your content generation after that.

  • This code without the redirect (header location) has one major problem - until you refresh the page with URL `...?id=X`, the rows with id=X gets deleted again and again! And, in principle, this ID can get reused, then deleted again. – mas.morozov Aug 31 '13 at 09:05
  • That is true. But as it goes with ID (which i guess is unique) It does not create a real problem. in this case at least. But ok. I see your point. – Thomas Martin Klein Aug 31 '13 at 09:10
0

try this code:

<?php

    $config = mysql_connect("localhost", "root", "") or die (mysql_error());
    $db = mysql_select_db("darith_upload_img",$config) or die (mysql_error());
   //delete part
   if(isset($_GET['id'])){
        $id_img=$_GET['id'];

        $q_del=mysql_query("DELETE FROM table_img where id_img ='$id_img'");
        header("location:index.php");

    } 

?>

    <form action="upload_process.php" enctype="multipart/form-data" method="POST">
        <input type="file" name="img" /><br />


        <input type="submit" value="upload Now" />

    </form>
    <?php
    $result = mysql_query("SELECT * FROM table_img");
        while($row = mysql_fetch_array($result))
        {

        $id = $row['id_img'];
            ?>
        <table border="1">
                <tr>
                    <td><?php echo $row['id_img']; ?></td>
                    <td width="200" ><?php echo $row['location_img']; ?></td>
                    <td><?php echo '<img src="' . $row['location_img'] . '" width="60"/>'; ?></td>
                    <td><a href="index.php?id=<?php echo $id;?> " onClick="return confirm('Are you sure?')">Delete</a></td>
                </tr>
            </table>
    <?php } ?>

Mani
  • 888
  • 6
  • 19
0

Sorry for my bad english

if(isset($_FILES["img"]["tmp_name"]))
    {
        move_uploaded_file($_FILES["img"]["tmp_name"], "picture/". $_FILES["img"]["name"]);

    $location = "picture/".$_FILES["img"]["name"];

$save = mysql_query("INSERT INTO table_img (location_img) VALUES ('$location')");

header("location:index.php");
exit();
}

First Change your exit(); function and write it in if condition

<?php
        $result = mysql_query("SELECT * FROM table_img");
        while($row = mysql_fetch_array($result))

        {

        $id = $row['id_img'];
            ?>
        <table border="1">
                <tr>
                    <td><?php echo $row['id_img']; ?></td>
                    <td width="200" ><?php echo $row['location_img']; ?></td>
                    <td><?php echo '<img src="' . $row['location_img'] . '" width="60"/>'; ?></td>
                    <td><a href="delete.php?id=<?php echo $id;?> " onClick="return confirm('Are you sure?')">Delete</a></td>
                </tr>
            </table>
    <?php
        }

    ?>

create delete.php file and that code write there

<?php
        if(isset($_GET['id'])){
            $id_img=$_GET['id'];

            $q_del=mysql_query("DELETE FROM table_img WHERE id_img ='".$id_img."'");
            header("location:index.php");
            exit();
        }
    ?>
user2727841
  • 715
  • 6
  • 21