Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Machine Performance Modeling Methodologies And Evaluation Strategies Lncs 257

Authors: Francesca Cesarini ,Silvio Salza

1st Edition

3540179429, 978-3540179429

More Books

Students also viewed these Databases questions

Question

3. You can gain power by making others feel important.

Answered: 1 week ago