通过pdo select查询生成mysql表行数据

xxb16uws  于 2021-06-18  发布在  Mysql
关注(0)|答案(0)|浏览(187)

我使用pdo连接到我的数据库。我正在尝试使用名为“modify course.php”的文件中的命名占位符来选择和更新查询,但没有成功。但数据库中的课程数据并不像预期的那样填充表单。下面是我的“modify course.php”:

<?php
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/";
    $my_upload->extensions = array(".png", ".gif", ".jpeg", ".jpg");
    // $my_upload->extensions = "de"; 
    $my_upload->max_length_filename = 50; 
    $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"; 
    if ($my_upload->upload()) 
    {
        $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,
            ];

        $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");

?>

在我的模板文件“modify-course\u template.php”中,我有如下代码:

<?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();
echo "$rows[0]['coursename']";
echo "$rows[0]['course_title']";
?>

有人能告诉我是什么阻止了数据库的填充吗?
你的洞察力将不胜感激。感谢您的预先输入。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题