1

I have a page which shows items which users have saved to a MYSQL database, and echos it out along with a a check box. I would like when a user clicks this check box and clicks the save button that the record will be deleted but i am met with the errors in my title.

Show holidays form.

        <form action="deleteProcess.php">
<?php
        foreach($db->query($sql) as $row)
        {
            echo "<p>" . $row['title'] . "<br \>" . 
                         $row['description'] . "<br \>" . 
                         $row['link'] . "<br \>" .
                         $row['pubDate'] . 
                         "<input type='checkbox' name='del[]' value='$row/['title'/]'/>" . 
                         "</p>";    
        }
?>
        <input type="submit" name="delete" value="submit" />
        </form>

delete process.php

foreach($_POST['del'] as $check_value)
        {
            //takes session value and assigns it to a variable so that when a holiday is saved it can be distinguished by user
            $user = $_SESSION['name'];
            //assign the value of previous checkbox to $check
            $check = $check_value;
            //assign xpath query to variable $fav
            $fav = "channel/item [title = \"$check\"]";
            //loads instance of xml file as $holidayDoc
            $holidayDoc = simplexml_load_file('holidays.xml');
            //executes xpath search within holidays.xml and results stored in $favourites
            $favourites = $holidayDoc->xpath($fav);

        //for each element of the associative array $favourites, key will be referred to as $currentFav. 
        foreach($favourites as $currentFav)
        {
            echo "{$currentFav->link}". "<br \>";
            echo "{$currentFav->title}". "<br \>";
            echo "{$currentFav->description}". "<br \>";
            echo "{$currentFav->pubDate} ". "<br \>";
            //sql statement that states which values will be inserted into which column
            $sql = "DELETE FROM `saved_holidays` (`subscriberID`, `link`, `pubDate`, `title`, `description`) 
        VALUES ('$user', '$currentFav->link', '$currentFav->pubDate', '$currentFav->title', '$currentFav->description')";

            //connect to database then execute the sql statement.
            $db->exec($sql);
            //close connection to the database
            $db = null;

The error is showing at the foreach($_POST['del'] as $check_value) line and i cant understand why its not working, any help would be appreciated.

Notice: Undefined index: del in /var/www/vhosts/numyspace.co.uk/web_users/home/~unn_w11026991/public_html/Ass/deleteProcess.php on line 14

Warning: Invalid argument supplied for foreach() in /var/www/vhosts/numyspace.co.uk/web_users/home/~unn_w11026991/public_html/Ass/deleteProcess.php on line 14

pb2q
  • 58,613
  • 19
  • 146
  • 147
Darren Burgess
  • 4,200
  • 6
  • 27
  • 43
  • Did you try a var_dump on $_POST? Is there any data? As it looks, the del index for $_POST isn't set which then would cause an invalid foreach. – Ahatius Apr 26 '12 at 10:21

6 Answers6

3

Your del checkboxes aren't being POSTed from the form at all. You need to add method="post" to your form.

<form action="deleteProcess.php" method="post">

Nadh
  • 6,987
  • 2
  • 21
  • 21
1

You should first check if $_POST['del'] is set because if no checkboxes are checked it won't be.

if(isset($_POST['del'])){
    foreach($_POST['del'] as $del){}
}
1

You access your checkbox values via $_POST, but your Form-Tag has no method defined! you have to set <form method="POST">, otherwise you have to use $_GET['del']

Stefan Pöltl
  • 362
  • 3
  • 9
1

method="post" is missing from <form action.

Also your SQL syntax for delete is incorrect. While deleting records from the database, you don't specify the columns. You delete the entire record.

The delete syntax is:

DELETE FROM table_name WHERE condition
codaddict
  • 445,704
  • 82
  • 492
  • 529
1

You are missing method="post" from your form so nothing is being posted, this is your problem.

Darren Burgess
  • 4,200
  • 6
  • 27
  • 43
0

This error is pretty clear.

$_POST['del']

doesn't exists since del isn't a valid field into the posted form.

If del is the name of checkboxes, you have first of all to retrive them by adding post action to your form

DonCallisto
  • 29,419
  • 9
  • 72
  • 100