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

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.

commented: Thank you it work +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.