Using a delete button to delete a student from the database and from the form

Hi everybody!
I am pretty new to my sql and I am very confused because I would like to delete a specific row I know you will tell me that I just need to get its id but the thing is there all similar rows with delete buttons that look like this and that I get from the database (result contains the retrieved data)

`<form name = "students" id = "students" action= "modify_course.php" method = "post">
<div class="my_student">
<p><label for="studentfirstname">studentfirstname: </label><input type = "text" name = "studentfirstname" id = "firstname" value = "<?echo result['studentfirstname']?>"  size = "10"/></p>
<p><label for="studentlastname">studentlastname: </label><input type = "text" name = "studentlastname" id = "lastname" value = "<?echo result['studentlastname']?>" size = "10"/></p>
<p><label for="studentage">studentage: </label><input type = "text" name = "studentage" id = "age" value = "<?echo result['studentage']?>" size = "10"/></p>
<input type = "submit" name = "Delete" class = "Delete" value = "Delete this student"/>
</div>
<input type = "submit" name = "modify" value = "Modify a student">
</form>`

I would like to delete one student using the delete button both from the form and from the database. For the form I will use jquery to remove the containing div but how do I delete a particular row since they are all the same from the database? please I need help.

Your database should have a primary key as a unique ID for every entry.
That ID could be put in a hidden from input, then put into the delete query.
In the form somehing like

<input type="hidden" name="studentid" value="<?php echo $row['id']; ?>

and in the query something like

DELETE FROM Students WHERE id = :id

thanks for answering. does it mean i will have an input type hidden for each row i retrieved ? Imagine i have three students and each should be deleted from the database if i click on its delete button for example

Only for info that you need to process the query. For a delete you only need identify which entry to delete, so the id is all you need.

Yes. You’ll need the same to make your ‘modify’ button work as well, I’d think - that will also need to know which student record you want to edit.

2 Likes

Don’t forget to validate the form data. If the user ID is a numeric a quick and easy way to validate it will be to typecast the submitted value as an integer, if the submitted value wasn’t an interger it gets converted to a 0

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.