Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

running into errors in trying to get this php form from when the user clicks the

ID: 3752580 • Letter: R

Question

running into errors in trying to get this php form from when the user clicks the delete button it deletes the subjects from the list

when i hit the delete button it goes to Invalid subject ID. page . and when i try to add something to a list it shows Invalid category data. Check all fields and try again.i feel like i almost have it i just need an extra eye on it .

this is the code i have Subject_list.php:

<?php
require_once('database1.php');

// Get all subjects
$query = 'SELECT * FROM subjects
ORDER BY subjectID';
$statement = $db->prepare($query);
$statement->execute();
$subjects = $statement->fetchAll();
$statement->closeCursor();
?>
<!DOCTYPE html>
<html>

<!-- the head section -->
<head>
<title>Thomas University</title>
<link rel="stylesheet" type="text/css" href="main.css" />
</head>

<!-- the body section -->
<body>
<header><h1>Course Manager</h1></header>
<main>
<h1>Subject List</h1>
<table>
<tr>
<th>Name</th>
<th>&nbsp;</th>
</tr>
<?php foreach ($subjects as $subject) :?>
<tr>
<td><?php echo $subject['subjectName']; ?></td>
<td><form action="delete_subject.php" method="post"
id="delete_subject">
<input type="hidden" name="subject_id"
value ="<?php echo $subject['subjectID'];?>">
<input type="submit" value="Delete">
</form>
</td>
</tr>
<?php endforeach;?>
  
</table>

<h2>Add Subject</h2>
<label>Name </label>
<form action="add_subject.php" method ="post">
<input type="text" name ="subjectID">
<input id="add_subject_button" type="submit" value="Add">


<!-- add code for the form here -->
</form>
<br>
<p><a href="index.php">List Courses</a></p>

</main>

<footer>
<p>&copy; <?php echo date("Y"); ?> Thomas University, Inc.</p>
</footer>
</body>
</html>

Here is the delete_subject.php code:

<?php
// Get ID
$subjectName = filter_input(INPUT_POST, 'subjectName', FILTER_VALIDATE_INT);

// Validate inputs
if ($subjectName == null || $subjectName== false) {
$error = "Invalid subject ID.";
include('error.php');
} else {
require_once('database1.php');

// Add the book to the database
$query = 'DELETE FROM subjects
WHERE subjectID = :subject_id';
$statement = $db->prepare($query);
$statement->bindValue(':subject_list', $subjectName);
$statement->execute();
$statement->closeCursor();

// Display the subject List page
include('subject_list.php');
}
?>

Now on the bottom of the form i need when the user clicks the Add button it adds another subject to the list :

my Add_subject.php code:

<?php
// Get the category data
$subject = $subjectName = filter_input(INPUT_POST, 'subjectName');

// Validate inputs
if ($subject == null) {
$error = "Invalid category data. Check all fields and try again.";
include('error.php');
} else {
require_once('database1.php');

// Add the subject to the database
$query = "INSERT INTO subjects (subjectName)
VALUES (:subject)";
$statement = $db->prepare($query);
$statement->bindValue(':subject_list', $subjectName);
$statement->execute();
$statement->closeCursor();

// Display the Category List page
include('subject_list.php');
}
?>

Database1.php code:

$dsn = 'mysql:host=localhost;dbname=my_courses';

$username = 'mgs_user';

$password = 'pa55word';

try {

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

} catch (PDOException $e) {

$error_message = $e->getMessage();

include('database_error.php');

exit();

}

?>

Course Manager Subject List Name Computer Info Systems Delete English American LiteratureDelete American History Music World History Delete Delete Delete Delete Add Subject Name PHP Add List Courses

Explanation / Answer

Here are my thoughts:

About delete action:

2. The value you are sending is integer?

3. Does the key name matches subjectName?

4. Try echoing it in the else case. If you get false, mistake is at parsing. If you get NULL, you are not setting the key properly.

Same applies to add subject php also.

If you are still facing the problem, please comment. I will help you to solve it.