Question: I need help figuring out how to add a delete button to my php file. In my index.php I have a delete button for each

I need help figuring out how to add a delete button to my php file. In my index.php I have a delete button for each record that is in the database. For some reason I can't get mine to work. Can someone help me figure out why? All it does it goes to delete.php and stays there instead of going back to index.php. It also doesn't delete the current record the button is used for.

database.php:

$dsn ="mysql:host=localhost; dbname=ToDoList";

$username = 'root';

$password = '4895698As';

try

{

$db = new PDO($dsn, $username, $password);

} catch (PDOException $e)

{

$error_message = 'Database Error!';

$error_message .= $e->getMessage();

echo $error_message;

exit();

}

?>

index.php:

require('database.php');

//post data

$newTitle = filter_input(INPUT_POST, 'newTitle', FILTER_UNSAFE_RAW);

$description = filter_input(INPUT_POST, 'description', FILTER_UNSAFE_RAW);

//Get Data

$title = filter_input(INPUT_GET, 'tile', FILTER_UNSAFE_RAW);

$query = 'SELECT * FROM todoitems';// PUT YOUR SQL QUERY HERE

// Example: $query = 'SELECT * FROM customers';

$statement = $db->prepare($query);

$statement->execute();

$results = $statement->fetchAll();

$statement->closeCursor();

?>

ToDoList

ToDoList

Title Description

Sorry No Results!

Add Item:

delete.php:

require("database.php");

$itemNum = filter_input(INPUT_POST, "id", FILTER_VALIDATE_INT);

if ($itemNum) {

$query = 'DELETE FROM todoitems

WHERE ItemNum = :itemNum';

$statement = $db->prepare($query);

$statement->bindValue(":itemNum", $itemNum);

$success = $statement->execute();

$statement->closeCursor();

header('Location: index.php');

exit();

}

?>

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!