What's new

Course Database design tutorial - part 7 - crud with phpmyadmin - course table

Status
Not open for further replies.
This is the part 7, mga 'tol.
Demo kung paano mag insert ng data by using the PHPMYADMIN dashboard.

(though I also included here the HTML format)

- download pdf.
- copy and paste this code to your project folder (CRUD for tbl_course, html form)

filename: administer_course_table.php
Code:
<?php
include("database_connection.php");
$message = "";

//This will process the ADD A COURSE form
if( isset($_POST["submit_new_course"]) ){
    //step 1. get values from the HTML form
    $coursename = $_POST["coursename"];
   
    //step 2: create a INSERT query string
    $insert_course_query = "INSERT INTO tbl_course ";
    $insert_course_query .= "(course_name) ";
    $insert_course_query .= "VALUES ('$coursename') ";   
   
    //step 3: let execute the INSERT query (and we catch the result if for success or fail)
    $result = mysqli_query($connection, $insert_course_query);

    //step 4: optional. we want to display something if success or fail
    if($result){
        $message = "1 course successfully added";
    } else {
        $message = mysqli_error($connection);
    }
}
//end end of ADD A COURSE process


//This will process the EDIT A COURSE button
if( isset($_POST["edit_course"]) ){
    //step 1. get values from the HTML form
    $id = $_POST["id"];
    $coursename = $_POST["coursename"];
   
    //step 2: create a UPDATE query string
    $update_course_query = "UPDATE tbl_course ";
    $update_course_query .= "SET course_name='$coursename' ";
    $update_course_query .= "WHERE course_id='$id' ";   
   
    //step 3: let execute the UPDATE query (and we catch the result if for success or fail)
    $result = mysqli_query($connection, $update_course_query);

    //step 4: optional. we want to display something if success or fail
    if($result){
        $message = "1 course successfully updated";
    } else {
        $message = mysqli_error($connection);
    }
}
//end end of EDIT A COURSE button


//This will process the DELETE A COURSE button
if( isset($_POST["delete_course"]) ){
    //step 1. get values from the HTML form
    $id = $_POST["id"];
    $coursename = $_POST["coursename"];

    //step 2: create a DELETE query string
    $update_course_query = "DELETE FROM tbl_course ";
    $update_course_query .= "WHERE course_id='$id' ";   
   
    //step 3: let execute the DELETE query (and we catch the result if for success or fail)
    $result = mysqli_query($connection, $update_course_query);

    //step 4: optional. we want to display something if success or fail
    if($result){
        $message = "1 course successfully deleted";
    } else {
        $message = mysqli_error($connection);
    }
}
//end end of DELETE A COURSE button


?>
<!DOCTYPE html>
<html>
<head>
    <title>Administer Course Table</title>
    <style>
    .div_admin{
        border: 1px solid #000;
        padding: 10px;
    }
    .leftcontainer{
        width: 50%;
        float: left;
    }
    .rightcontainer{
        width: 50%;
        float: left;
    }
    .clearfix{
        clear: both;
    }
    </style>
</head>
<body>
<div><!-- PAGE div-->

    <div class="div_admin"><!-- COURSE ADMIN div-->

        <div class="leftcontainer"><!-- Left Container-->
            <div><!-- ADD A COURSE-->
                    <p>ADD A COURSE</p>
                    <p style="color: green; font-style: italic; size: .7em"><?php echo $message; ?></p>
                    <form method="POST" action="administer_course_table.php">
                        Course Name:<input type="text" name="coursename">
                        <input type="submit" name="submit_new_course">
                    </form>
            </div><!-- end of ADD A COURSE-->
        </div><!-- end of left container-->



        <div class="rightcontainer">
            <div><!-- DISPLAY ALL COURSES with EDIT + DELETE FUNCTIONS-->
                <p>ALL COURSES</p>
                <?php
                    $query_all_courses = "SELECT * FROM tbl_course";
                    $result = mysqli_query($connection,$query_all_courses);
                    if($result){
                        while($record=mysqli_fetch_array($result)){
                            echo '<form method="POST" action="administer_course_table.php">';
                            $id = $record['course_id'];
                            echo '<input type="hidden" name="id" value="'.$id.'">';
                            echo 'Course ID' . $id . " - ";
                            echo '<input type="text" name="coursename" value="'. $record['course_name'] .'">';
                            echo '<input type="submit" name="edit_course" value="Edit">';
                            echo '<input type="submit" name="delete_course" value="Delete" onclick="return confirm(\'Are you 100% sure to delete?\')">';
                            echo '</form>';
                        }
                    }
                ?>
            </div><!-- end of DISPLAY ALL COURSES with EDIT + DELETE FUNCTIONS-->
        </div><!-- end of right container-->

        <div class="clearfix"></div>

    </div><!-- end COURSE ADMIN div-->

</div><!-- end of PAGE div-->

<p><center><a href="index.php">Back to Main Menu</a></center></p>

</body>
</html>
 

Attachments

Status
Not open for further replies.

Similar threads

About this Thread

  • 1
    Replies
  • 514
    Views
  • 2
    Participants
Last reply from:
Gmlorenz

Online statistics

Members online
1,283
Guests online
5,356
Total visitors
6,639
Back
Top