Notice: Undefined variable: confirmation in C:\xampp\htdocs\RustoleumCustomCMS\administrator\admin.php on line 126

    if (!empty($_REQUEST['id']) && !empty($_REQUEST['mode'])){
    if ($_REQUEST['mode'] == "delete"){
        $id = $_REQUEST['id'];
        $result = mysql_query("DELETE FROM static_page WHERE id =".$id) or die(mysql_error());

        $confirmation = ($result) ? "Data telah terhapus." : "Gagal menghapus data."; 
        }
     }

?>
<div align="center">
    <div style="width:700px;text-align:left;padding-top:5px;">
        <?php echo $confirmation; ?> 
        <form method="get" action="<?php $_SERVER['PHP_SELF'] ?>">

                                    <br/>
                        <a class="topLink" href="input_berita_static.php">Berita Static Baru >></a><br><br>

        <?php
                //LOAD NEWS

                $result = mysql_query("SELECT * FROM static_page") or die(mysql_error());
                ?>
                <table border="1" cellpadding="2" cellspacing="0">
                    <tr>
                        <th>Static Page</th><th>Judul</th><th>Action</th>
                    </tr>
                    <?php
                    while ($data = mysql_fetch_array($result)){
                        echo '<tr>';
                            echo '<td>'.$data['page'].'</td>';
                            echo '<td>'.$data['judul'].'</td>';
                            echo '<td><a href="admin.php?mode=delete&id='.$data['id'].'">Hapus</a> |<a href="input_berita_static.php?id='.$data['id'].'">Edit</a></td>';
                        echo '</tr>';
                    }
                    ?>
                </table>

line 126: <?php echo $confirmation; ?>

Dani AI

Generated

The immediate cause is PHP’s empty() behavior: the value "0" (string or integer 0) is considered empty. That explains why the link you posted (/ showed id=0) never enters the delete branch and $confirmation is never set. It also explains why a plain page load (no params) prints an empty array for $_REQUEST (/ asked you to inspect it) — the branch only runs when the parameters are present and pass your checks.

Practical fixes (minimal and robust): stop using empty() for an ID that can legitimately be 0. Check existence and non-empty-string explicitly, or validate as an integer. Use a clear input source ($_GET or $_POST rather than $_REQUEST), validate/cast, run the DELETE with a prepared statement, then confirm success with affected-rows logic and escape any output.

$id   = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT, ['options'=>['default'=>null]]);
$mode = filter_input(INPUT_GET, 'mode', FILTER_SANITIZE_STRING);

if ($mode === 'delete' && $id !== null) {
    // run a prepared DELETE (mysqli or PDO)
    // verify success with mysqli_affected_rows() or $stmt->rowCount()
    // set $confirmation and echo with htmlspecialchars($confirmation)
}

Best-practice notes: move off deprecated mysql_* functions to mysqli or PDO with prepared statements, avoid doing deletes via GET (use POST + CSRF token), and use the PRG pattern (redirect after the action) or a session "flash" message for the confirmation. On the thread: ’s idea of setting a default confirmation is sensible, ’s suggestion to guard the echo with an existence check is useful, and ’s claim that DELETE “returns nothing” is incorrect — mysql_query returns TRUE/FALSE and the number of rows removed is available through the affected-rows function.

Recommended Answers

All 11 Replies

What puzzles you ? Why $confirmation is undefined ? You define , it inside two conditional statements so logically those conditions are not satisfied.

It doesn't work, huh? Well, basically I am trying to say if that data is successfully deleted than the first text shows up otherwise the second one.

It's a confirmation whether the data is successfully deleted or not.

In this case you don’t need two separate ifs.

Example without changing the way you have thought it.

if ( (!empty($_REQUEST['id']) && !empty($_REQUEST['mode'])) && $_REQUEST['mode'] == "delete")
{
    $id = $_REQUEST['id'];
    $result = mysql_query("DELETE FROM static_page WHERE id =".$id) or die(mysql_error());
    $confirmation = ($result) ? "Data telah terhapus." : "Gagal menghapus data.";
}
else 
{
  $confirmation = "No change performed"; 
}

According to me delete querry of sql does not return anything.Whether the querry is correct or not could be checked by values in database...Insert/delete querries don't return any value

Generally, undefined variable means that it has no value and it is just a warning, change to this:

<?php if (isset($confirmation)) { echo $confirmation; } ?>

This should get rid of the warning but doesn't help as to why the variable is empty.

Then for your if statement shpould work like this:

$confirmation = !$result ? "Data telah terhapus." : "Gagal menghapus data.";
// or whichever way round you need it

The error disappeared. Message : No change performed

Why is it? It suppose to delete the row.

it is not going in the following if condition

if ( (!empty($_REQUEST['id']) && !empty($_REQUEST['mode'])) && $_REQUEST['mode'] == "delete")

add 3 lines before if and see what if gives in request array, copy and paste that output here.

echo "<pre>"
print_r($_REQUEST);
echo "</pre>";
if ( (!empty($_REQUEST['id']) && !empty($_REQUEST['mode'])) && $_REQUEST['mode'] == "delete")
{
.......

Array
(
)

if I press Hapus(delete) this shows up:

Array
(
    [mode] => delete
    [id] => 0
)
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.