0

I want to update my data in SQL database but i am facing issues like as you can see i already defined the request method as POST but when i gonna check it, it doesn't work like:

if (
                isset($_POST["form"])
            ) {
              ...
            }
else{ echo "form's method is not set as POST"}

this condition get false and print "form's method is not set as POST".

This is my form HTML

 <form name="form" method="post" action="courses.php">
                                        <div class=" mb-3">
                                            <label for="c_name_u" class="form-label">Course Name</label>
                                            <input type="text" class="form-control" name="c_name_u" id="c_name_u" require>
                                        </div>
                                        <div class=" mb-3">
                                            <label for="credit_hours_u" class="form-label">Credite Hours</label>
                                            <input type="text" class="form-control" name="credit_hours_u" id="credit_hours_u" require>
                                        </div>

                                        <a style="text-decoration: none; color: white;" href="courses.php?edit_task=<?php echo $course_id ?>">
                                            <button type="submit" class="btn btn-primary btn-md">
                                                Update Course
                                            </button>
                                        </a>
                             </form>

and my PHP code is:

       <?php

        if (isset($_GET['edit_task'])) {

            if (
                isset($_POST["form"])
            ) {

                $c_name_u = $_POST["c_name_u"];
                $credit_hours_u =  $_POST["credit_hours_u"];

                $course_id = $_GET['edit_task'];

                $query = "UPDATE `courses` SET `Course_name` = '$c_name_u', `Credit_hours` = '$credit_hours_u' WHERE `courses`.`Course_id` =" . $course_id;

                $update_db = $conn->query($query);

                if (!$update_db) {
                    echo " data is not saved";
                }
            }
        }
        ?>
  • Please click the "edit" link under your question and explain fully how you're testing and what goes wrong; "it doesn't work" is not a useful problem description. See [ask] – IMSoP Mar 10 '22 at 17:31
  • As a side note, your code is **wide open to attack**. Please get in the habit of using parameterised queries to protect yourself from SQL Injection. See [How can I prevent SQL injection in PHP?](https://stackoverflow.com/q/60174/157957) – IMSoP Mar 10 '22 at 17:33
  • `isset($_POST["form"])` - Does the form's name get included in the POST? I've never used it like that, so this is mostly a guess. But it's more common to give the name to the submit button, not the form itself. Aside from that, now is a good time to start familiarizing yourself with debugging. You can use your browser's debugging tools to observe the exact request being sent to the server and what data it contains, you can add debugging output to your PHP code to observe more about what it's doing. How specifically does it fail when you do this? – David Mar 10 '22 at 17:33
  • I can't close as a duplicate because of technical restrictions (I can't change from one close reason to another), but the check you are using is incorrect. Depending on what you want, see either [Detecting request type in PHP (GET, POST, PUT or DELETE)](https://stackoverflow.com/q/359047/157957) or [How can I tell which button was clicked in a PHP form submit?](https://stackoverflow.com/q/2680160/157957) – IMSoP Mar 10 '22 at 18:14

0 Answers0