0

this code will display all courses user has

<h1>YOUR COURSES</h1> <form action="delete.php" method="POST"><ol>
    <?php
    $con=mysqli_connect("localhost","FYP","123","FYP");
$sql= mysqli_query($con, "SELECT C_Code FROM F_COURSES WHERE F_ID=".$_SESSION['userid']);
while($row = mysqli_fetch_array($sql)){
echo "<li name='course'>".$row['C_Code']."<input type='submit'  value='Delete'>";}

    ?></ol></form>

I need when user click on delete button that course delete from F_courses

<?php
session_start();
if (! empty($_SESSION['logged_in']))
{
    $con=mysqli_connect("localhost","FYP","123","FYP");
    $id=$_SESSION['userid'];
    $course=$_POST["course"];


            $sql="DELETE FROM F_COURSES WHERE F_ID='$id' AND C_Code='$course'";
            mysqli_query($con,$sql);

            header ("location: mycourses.php");

}
else
{
    echo 'You are not logged in. <a href="login.html">Click here</a> to log in.';
}

Could you please help me how to correct my code
Thank you

2
Contributors
1
Reply
26
Views
2 Years
Discussion Span
Last Post by broj1
1

Enclose each row (and ID and a delete button) within form tags and add a hidden input that holds the value. You will have as many forms as there rows. When you delete (submit), the value in the hidden input field will get over to the next script. And you do not need a name attribute in the list item element.

<h1>YOUR COURSES</h1>
<ol>
<?php
$con=mysqli_connect("localhost","FYP","123","FYP");
$sql= mysqli_query($con, "SELECT C_Code FROM F_COURSES WHERE F_ID=".$_SESSION['userid']);
while($row = mysqli_fetch_array($sql)){

    // I added a little bit of a styling to the form to display correctly
    echo "<li><form method='post' action='delete.php' style='display:inline;'>" . $row['C_Code'] . "<input type='submit' value='Delete'>";

    // this line adds a hidden input
    echo "<input name='course' type='hidden' value='" . $row['C_Code'] . "'></form></li>";
}
?>
</ol>

I hope having a form inside a list item element isn't bad coding. But I've seen that arround.

Edited by broj1

Votes + Comments
Thank you it work
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.