I am new to php and SQL and just toying around with a project for my own understanding of accessing, updating and deleting data from my Database.

Đang xem: Php mysql delete query

I have managed to show the selected data, create a button to delete a specific Id but really needing some assistance with deleting the selected row or record instead of hard coding in the ID in my delete php script.

Here is an example of my script:

query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo “
” . $row<"id">. “: ” . $row<"firstname"> . ” ” . $row<"lastname">. ” ” . $row<"joinDate">. ” ” . ” Delete ” . ” ” . “Edit” . “
“; } } else { echo “0 results”; } $conn->close();?>Here is the delete.php

query($sql) === TRUE) { header(“Location: index.php”); } else { echo “Error deleting record: ” . $conn->error; } $conn->close();?>what I would like it to do is, delete the row from which you hit the delete button from and not just delete the row I have specified in the delete.php script. I understand HOW it should work by posting the id but not sure how to do it.

*

*

Do like this

query($sql) === TRUE) { header(“Location: index.php”); } else { echo “Error deleting record: ” . $conn->error; } $conn->close();?>query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo “
” . $row<"id">. “: ” . $row<"firstname"> . ” ” . $row<"lastname">. ” ” . $row<"joinDate">. ” ” . “Delete” . “Edit” . “

*

in place of your form use this

“>DELETEand in your delete query must be like below

$sql = “DELETE FROM customers WHERE id=””.$_GET<"id">.”” “;

*

” . $row<"id">. “: ” . $row<"firstname"> . ” ” . $row<"lastname">. ” ” . $row<"joinDate">. ” ” . ” Delete ” . ” ” . “Edit” . “
“; }And in your delete.php :

query($sql) === TRUE) { header(“Location: index.php”); } else { echo “Error deleting record: ” . $conn->error; } $conn->close();?>

*

No need to add extra form element for Delete or Edit purpose. Try this way to pass the id of row for Eelete or Edit operation

while($row = $result->fetch_assoc()) { $id=$row<"id">;// capture your row id & pass to your delete & edit echo “
” . $row<"id">. “: ” . $row<"firstname"> . ” ” . $row<"lastname">. ” ” . $row<"joinDate">. ” ” . ” “>Delete ” . ” ” . ” “>Edit ” . ”
“; }EDIT:Then catch the id on your relevant page for your operation.

Xem thêm: Top 11 Phần Mềm Bán Hàng Miễn Phí Tốt Nhất Năm 2020, Phần Mềm Quản Lý Bán Hàng Miễn Phí

//deleteMember.php

query($sql) === TRUE) { header(“Location: index.php”); } else { echo “Error deleting record: ” . $conn->error; } $conn->close();?>Note: Please Use Prepared Statements of PDO or MYSQLi instead to avoid SQL Injection and manual escaping.

Thanks for contributing an answer to Stack Overflow!

Please be sure to answer the question. Provide details and share your research!

But avoid

Asking for help, clarification, or responding to other answers.Making statements based on opinion; back them up with references or personal experience.

Xem thêm:

To learn more, see our tips on writing great answers.

Post Your Answer Discard

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged php mysql forms or ask your own question.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev2021.3.22.38863

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *