mexabet 49 Good Learner

I'm doing both SELECT and UPDATE queries using named placeholders. But course data from the database doesn't populate form, as expected. The database table row records don't populate, each time I try to edit a course in the browser. I connect to the database usining PDO. And I do SELECT and UPDATE queries using named placeholders. The following is my "modify-course.php":

 <?php

    // configuration
    require("../includes/config.php");       

    //select a particular admin by id
    $admin_id = isset($_GET["admin_id"]) ? $_GET["admin_id"] : ''; 

    $stmt = $pdo->prepare("SELECT * FROM admin WHERE admin_id=:admin_id");
    $stmt->execute(['admin_id' => $admin_id]); 
    $admin = $stmt->fetch(); # get admin data

    if (!$admin)
    {
        header("Location: login.php");
    }

    //Class import for image uploading
    //classes is the map where the class file is stored (one above the root)
    include ("../classes/upload/upload_class.php");         

    //select a particular course by id
    $course_id = isset($_GET["course_id"]) ? $_GET["course_id"] : ''; 

    $stmt = $pdo->prepare("SELECT * FROM courses WHERE course_id=:course_id");
    $stmt->execute(['course_id' => $course_id]); 
    $course = $stmt->fetch(); # get course data

    if ($_SERVER["REQUEST_METHOD"] == "POST")
    {

        // validate submission
        if (empty($_POST["coursename"]))
        {
             echo "Provide the course name.";
        }
        if (empty($_POST["courseduration"]))
        {
            echo "Provide the course duration.";
        }
        if (empty($_POST["coursecode"]))
        {
            echo "Provide the course code.";
        }
        if (empty($_POST["fees"]))
        {
            echo "Enter total fees for the course.";
        }

            // validate coursename
            //$coursename = ($_POST["coursename"]);
            //if (!preg_match("/^[a-zA-Z0-9]*$/", $coursename))
           // {
             //   echo "A course name must contain only letters and numbers.";
           // }
            if (strlen($_POST["coursename"]) < 20 || strlen($_POST["coursename"]) > 50)
            {
                echo "A course name must be from 20 to 50 characters.";
            }
            // validate course duration
            //$courseduration = ($_POST["courseduration"]);
            //if (!preg_match("/^[a-zA-Z0-9]*$/", $courseduration))
            //{
            //    echo "Invalid course duration.";
            //}
            // validate coursecode
            //$coursecode = ($_POST["coursecode"]);
            //if (!preg_match("/^[a-zA-Z0-9]*$/", $coursecode))
            //{
             //   echo "A course ID can only contain letters and numbers.";
            //}
            if (strlen($_POST["coursecode"]) < 3 || strlen($_POST["coursecode"]) > 10)
            {
                echo "A course code must be from 3 to 10 characters.";
            }
            if ($_POST["coursecode"] === false)
            {
                echo "The course code has already been taken.";
            }

        //This is the directory where images will be saved 
        $max_size = 1024*250; // the max. size for uploading

        $my_upload = new file_upload;

        $my_upload->upload_dir = "../images/courses/"; // "files" is the folder for the uploaded files (you have to create this folder)
        $my_upload->extensions = array(".png", ".gif", ".jpeg", ".jpg"); // specify the allowed extensions here
        // $my_upload->extensions = "de"; // use this to switch the messages into an other language (translate first!!!)
        $my_upload->max_length_filename = 50; // change this value to fit your field length in your database (standard 100)
        $my_upload->rename_file = true;

        $my_upload->the_temp_file = $_FILES['courseimage']['tmp_name'];
        $my_upload->the_file = $_FILES['courseimage']['name'];
        $my_upload->http_error = $_FILES['courseimage']['error'];
        $my_upload->replace = "y";
        $my_upload->do_filename_check = "n"; // use this boolean to check for a valid filename
        if ($my_upload->upload()) // new name is an additional filename information, use this to rename the uploaded file
        {
            $full_path = $my_upload->upload_dir.$my_upload->file_copy;
            $imagename = $my_upload->file_copy;
        }
        else
        {
            $imagename = "";
        }

        if (!empty($_POST["coursename"]))
        { 

            $data = [
                'coursename' => $coursename,
                'course_title' => $course_title,
                'meta_keywords' => $meta_keywords,
                'meta_description' => $meta_description,
                'short_desc' => $short_desc,
                'coursedesc' => $coursedesc,
                'courseduration' => $courseduration,    
                'coursecode' => $coursecode,
                'fees' => $fees,
                'courseimage' => $my_upload->file_copy,
                ];

            // insert form input into database
            $result = "UPDATE courses SET coursename=:coursename, course_title=:course_title, meta_keywords=:meta_keywords, meta_description=:meta_description, short_desc=:short_desc, coursedesc=:coursedesc, courseduration=:courseduration, coursecode=:coursecode, fees=:fees', courseimage=:courseimage WHERE course_id=:course_id";
            $stmt= $dpo->prepare($result);
            $stmt->execute($data);

            // if username is in database
            if ($stmt === false)
            {
                echo "There was an error modifying this course.";
            }

            // redirect to list courses page
            header("Location: list-courses.php");
        }

    }

    // render the header
    include("templates/header.php");

    // render modify course template
    include("templates/modify-course_template.php");

    // render the footer
    include("templates/footer.php");

?> 

As follows is my "modify-course_template.php":

<h1>Admin - Modify a Course</h1>
<?php

    $course_id = isset($_GET["course_id"]) ? $_GET["course_id"] : '';

    $stmt = $pdo->prepare("SELECT * FROM courses WHERE course_id=:course_id");
    $stmt->execute(['course_id' => $course_id]); 
    $rows = $stmt->fetch(); # get course data   

    echo '<div class="form-group">';
    echo "Course ID#: $course_id";
    echo '</div>';

?>  
    <form enctype="multipart/form-data" action="modify-course.php?id=<?php echo $course_id; ?>" method="post">
 <?php

        echo '<fieldset>';
        echo '<div class="form-group">';
       echo 'Course Name: <textarea autofocus class="form-control" placeholder="Course Name" cols="32" rows="2" name="coursename">'.$rows[0]["coursename"].'</textarea>';
        echo '</div>';
        echo '<div class="form-group">';
        echo 'Course Title: <textarea autofocus class="form-control" placeholder="Course Title" cols="32" rows="2" name="course_title">'.$rows["course_title"].'</textarea>';
        echo '</div>';
        echo '<div class="form-group">';
        echo 'Meta Keywords: <textarea autofocus class="form-control" placeholder="Meta Keywords" cols="32" rows="2" name="meta_keywords">'.$rows[0]["meta_keywords"].'</textarea>';
        echo '</div>';
       echo '<div class="form-group">';
        echo 'Meta Description: <textarea autofocus class="form-control" placeholder="Meta Description" cols="32" rows="2" name="meta_description">'.$rows[0]["meta_description"].'</textarea>';
        echo '</div>';
        echo '<div class="form-group">';
        echo 'Short Description: <textarea autofocus class="form-control" placeholder="Short Description" cols="32" rows="2" name="short_desc">'.$rows[0]["short_desc"].'</textarea>';
        echo '</div>';
        echo '<div class="form-group">';
        echo 'Description: <textarea autofocus class="form-control" id="myTextarea" placeholder="Course Description" cols="32" rows="2" name="coursedesc">'.$rows[0]["coursedesc"].'</textarea>';
        echo '</div>';
        echo '<div class="form-group">';
        echo 'Duration: <input autofocus class="form-control" name="courseduration" . value="' . $rows[0]["courseduration"] .'" placeholder="Course Duration" type="text"/>';
        echo '</div>';
        echo '<div class="form-group">';
        echo 'Course Code: <input autofocus class="form-control" name="coursecode" . value="' . $rows[0]["coursecode"] .'" placeholder="Course Code" type="text"/>';
        echo '<input type="hidden" name="id" . value="' . $rows[0]["id"] .'" id="id"/>';
        echo '</div>';
        echo '<div class="form-group">Total Fees: &#8358; ';
        //printf('<input autofocus class="form-control" name="fees" value="' . $users[0]["fees"]="&#8358;" . number_format($users[0]["fees"], 2).'" placeholder="User Funds" type="text"/>');
        echo '<input autofocus class="form-control" name="fees" value="' . $rows[0]["fees"].'" placeholder="Fees" type="text"/>';        
        echo '</div>';
        echo '<div class="form-group">';
        echo "<img src='../images/courses/" . $rows[0]['courseimage']."'/>";
        echo '</div>';
        echo '<div class="form-group">';
        echo 'Course Photo: <input autofocus class="form-control" name="courseimage" . value="' . $rows[0]["courseimage"] .'" id="fileimage" placeholder="Course Photo" type="file"/>';
        echo '</div>';
        echo '<div class="form-group">';
        echo '<button type="submit" class="btn btn-default" name="Modify Course" value="Modify Course">Modify Course</button>';
        echo '</div>';
        echo '</fieldset>';
        echo '</form>';

?>

<div>
</div>
<br/>

Thanks in advance for your time and help.

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.