so i have a zoom.php where i have a link.

<a href='deleteimage.php?imgid={$row['image_id']}'>Delete</a>

if user click on it it goes to deleteimage.php where i have a form. in form i have field where i store imagid.

<form class="form" action = 'deleteimage.php' method = 'post'>
    <input type='submit' class='button' value='Yes'>
    <input type="hidden" name ="random" class="random" value="<?php echo($_GET['imgid']);?>" />
                <br/><br/>
                <br/>
                <br/>
</form>

now if user click on submit button it run php code. note its still on deleteimage.php page.

if(isset($_SESSION['username']))      /*** user is loged in ***/
{
    if($_SERVER['REQUEST_METHOD'] == 'POST')     //user hit submit button
    {
        $user_id_s = $_SESSION['user_id'];
        $image_id_g  = $_POST['random'];    

if(!$tester = mysql_query("SELECT * FROM image WHERE  image_id = '$image_id_g' AND user_id = '$user_id_s'"))
        {
                $del_image = mysql_query("DELETE FROM image WHERE image_id = '$image_id_g'");
                $del_comment = mysql_query("Delete From comment Where image_id = '$image_id_g'");
        }

here i get a error. and have no idea who to fix it.

<br /><b>Notice</b>:  Undefined index: imgid in <b>C:\xampp\htdocs\a_upload\deleteimage.php</b> on line <b>51</b><br />

note. line 51 is:
<input type="hidden" name ="random" class="random" value="<?php echo($_GET['imgid']);?>" />

Recommended Answers

All 11 Replies

I think you should echo the $row['image_id'] value like:

<a href='deleteimage.php?imgid=<?php echo $row['image_id'];?>'>Delete</a>

ic just one question i have it in php echo alread for ex.

<?php
echo"
...
<a href='deleteimage.php?imgid=<?php echo $row['image_id'];?>'>Delete</a>
..
";
?>

do you know how can i fix the inner echo?

If you echo the whole line then your version from the first post is OK. But maybe you should check if $row['image_id'] returns a valid value:

if(is_numeric($row['image_id']) and $row['image_id'] > 0) {
    echo "<a href='deleteimage.php?imgid={$row['image_id']}'>Delete</a>";    
} else {
    echo 'An error has occurred.';
}

provided that the image ID is a numeric value.

<a href='deleteimage.php?imgid={$row['image_id']}'>Delete</a>

i already have that and i check it and it does return the right value. when i click the submit button it button it give me this error:

<br /><b>Notice</b>: Undefined index: imgid in <b>C:\xampp\htdocs\a_upload\deleteimage.php</b> on line <b>51</b><br />

inside the hidden field

If you look at the html code of deleteimage.php in the browser does the hidden input have the required value (the image ID)?

yes it has the required value (image id)

You get the error only after you click the yes button. Since the form gets submitted to itself and you have no querystring in your action attribute the $_GET array is empty. After deleting you should probably redirect user o the previous page. If this is so use header('location:zoom.php') or something like that.

if(isset($_SESSION['username'])) /*** user is loged in ***/
{
if($_SERVER['REQUEST_METHOD'] == 'POST') //user hit submit button
{
    $user_id_s = $_SESSION['user_id'];
    $image_id_g = $_POST['random'];
    if(!$tester = mysql_query("SELECT * FROM image WHERE image_id = '$image_id_g' AND user_id = '$user_id_s'"))
    {
        $del_image = mysql_query("DELETE FROM image WHERE image_id = '$image_id_g'");
        $del_comment = mysql_query("Delete From comment Where image_id = '$image_id_g'");

        // redirect the user
        header('location:zoom.php');
    }
    ...

i tried

header('location:zoom.php');

butit seem doesnt seem to make a difference. still get same error.
and it prints
echo "This is not your image! "

i have no idea how wha tto do :(

The redirection won't work if there was html output sent before it. You should put the if block to the beginning of the script (i.e. first check if the form was submitted) and then draw the form. There should be not even a space before the header.

<?php
if(isset($_SESSION['username'])) /*** user is loged in ***/
{
    if($_SERVER['REQUEST_METHOD'] == 'POST') //user hit submit button
    {
        $user_id_s = $_SESSION['user_id'];
        $image_id_g = $_POST['random'];
        if(!$tester = mysql_query("SELECT * FROM image WHERE image_id = '$image_id_g' AND user_id = '$user_id_s'"))
        {
            $del_image = mysql_query("DELETE FROM image WHERE image_id = '$image_id_g'");
            $del_comment = mysql_query("Delete From comment Where image_id = '$image_id_g'");
            header('location:zoom.php');

        } else {

            // display the error message or handle the situation some other way
            echo '<div>Error deleting th image.</div>';
        }
    }
}
?>
<form class="form" action = 'deleteimage.php' method = 'post'>
<input type='submit' class='button' value='Yes'>
<input type="hidden" name ="random" class="random" value="<?php echo($_GET['imgid']);?>" />
<br/><br/>
<br/>
<br/>
</form>

yes, i just put it at first but do dfifierent. here is my full code.
deleteimage.php

<?php
include("include/header.php");

$delete_error = "";

//check, if user is loged in or not
if(isset($_SESSION['username']))      /*** user is loged in ***/
{
    if($_SERVER['REQUEST_METHOD'] == 'POST')     //user hit submit button
    {
        $user_id_s = $_SESSION['user_id'];
        $image_id_g  = $_POST['random'];    

        echo"$image_id_g" ;
        if(!$tester = mysql_query("SELECT * FROM image WHERE  image_id = '$image_id_g' AND user_id = '$user_id_s'"))
        {
                $del_image = mysql_query("DELETE FROM image WHERE image_id = '$image_id_g'");
                $del_comment = mysql_query("Delete From comment Where image_id = '$image_id_g'");

        header('Location: zoom.php');
        }
        else
        {
            $delete_error .= "This is not your image!";
        }
    }
}
else
{
    header('Location: error.php');  
}
$_SESSION['delete_error'] = $delete_error;
?>

<!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>Untitled Document</title>
<link rel='stylesheet' type='text/css' href='css/top_menu.css' />
    <link rel='stylesheet' type='text/css' href='css/all_css.css' />
</head>

<body>
    <div id ='bg4'>
        <div id='context4'>              
             <form class="form" action = 'deleteimage.php' method = 'post'>
                 <pre><h1><span style='background-color:#D00000;'> Delete Image</h1></span><hr/></pre>
                 Are you sure you want to delete this image?
                <input type='submit' class='button' value='Yes'>
                <input type="" name ="random" class="random" value="<?php echo($_GET['imgid']);?>" />
                <br/><br/>
                <?php
                    //print errors
                    if(array_key_exists('delete_error', $_SESSION) && !empty($_SESSION['delete_error']))
                    {
                        $delete_error_r = $_SESSION['delete_error'];
                        echo "<span style='background-color:#D00000;'> $delete_error_r </span>";
                        unset($_SESSION['delete_error']);
                    }
                ?>
                <br/>
                <br/>
             </form>
             <br/>
        </div>
    </div>
</body>
</html>


<?php
    include("include/footer.php");
?>

Sorry, I've been away on holiday for a couple of days.

The problem is in line 14: echo"$image_id_g" where you send some output to the browser. You can not use header() function once you send output to the browser since the headers are sent automatically before the output. Comment out line 14 and it should work. Also make sure that in the include file header.php there is no output sent (not even a space).

If you realy must send output before header() function then use output buffering.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.