Question: Hello I need help getting my delete and additem button to work. The delete button needs to use the file delete_record.php and the add item

Hello I need help getting my delete and additem button to work. The delete button needs to use the file delete_record.php and the add item button needs to use the file add_item.php.. The problem I am running into is that neither button is wanting to post anything. The AddItem button needs to post new information to an sql server called todolistand a table named todoitems. The delete button needs to delete those records if the user so wishes from the database. Each time the button is clicked it should send the user back to the index.php page. The todolist and todoitems have the followers columns ItenNum - int(11), Title - varchar(20), and Description - varchar(50). Below is the files I need fixed.

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:

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();

}

?>

add_item.php:

require("database.php");

// Check if the form has been submitted

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

// Retrieve the form data

$title = $_POST['title'];

$description = $_POST['description'];

// Insert the new item into the todoitems table

$query = "INSERT INTO todoitems (Title, Description) VALUES (:title, :description)";

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

$statement->bindValue(':title', $title);

$statement->bindValue(':description', $description);

$statement->execute();

// Redirect the user to the index page

header('Location: index.php');

exit();

}

include("index.php");

?>

delete_record.php:

require_once('database.php');

// here Check if the ItemNum parameter has been passed

if (!isset($_GET['ItemNum'])) {

header('Location: index.php');

exit();

}

// Retrieve the ItemNum parameter.

$itemNum = $_GET['ItemNum'];

// Delete the item from todoitems.

$query = "DELETE FROM todoitems WHERE ItemNum = :itemNum";

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

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

$statement->execute();

// Redirect the user to the index page

header('Location: index.php');

exit();

include("index.php");

?>

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!