Continuation, mga 'tol.
Part 6.
Using HTML FORM to populate data into your database table.
CREATE - READ - UPDATE - DELETE (CRUD) functions.
Save ALL THESE codes in the same project folder.
filename: database_connection.php
filename: index.php
filename: administer_student_table.php
****TAKE NOTE: Straight-forward form lang ito. Wala itong validation functions.********
******Next, I will show you how to add data to a table using PHPMYADMIN******
Part 6.
Using HTML FORM to populate data into your database table.
CREATE - READ - UPDATE - DELETE (CRUD) functions.
Save ALL THESE codes in the same project folder.
filename: database_connection.php
Code:
<?php
DEFINE("HOSTNAME","localhost"); //put your DB server address
DEFINE("USERNAME","codyscott"); //put your DB user
DEFINE("PASSWORD","secrett"); //put your DB password
DEFINE("DATABASE","phcorner"); //put your DB name
$connection=mysqli_connect(HOSTNAME,USERNAME,PASSWORD,DATABASE); //create connection variable
//check connection
if(!$connection){
die ("<html><script language='JavaScript'>alert('Ops! Cannot connect with the server at this time. Please try again later.'),history.go(-1)</script></html>");
echo "STATUS: DB Connection Failed!";
} else {
//echo "STATUS: DB Connection Success!";
}
?>
filename: index.php
Code:
<!DOCTYPE html>
<html>
<head>
<title>Phcorner Database Design Tutorial</title>
</head>
<body>
<h2>MENU</h2>
<p>Create - Read - Update - Delete (CRUD)</p>
<ul>
<li><a href="administer_student_table.php">Administer Student Table</a></li>
<li>Administer Course Table</li>
<li>Administer Level Table</li>
<li>Administer School Table</li>
<li>Administer Gender Table</li>
<li>View Student Survey</li>
</ul>
</body>
</html>
filename: administer_student_table.php
****TAKE NOTE: Straight-forward form lang ito. Wala itong validation functions.********
Code:
<?php
include("database_connection.php");
$message = "";
//This will process the ADD A STUDENT form
if( isset($_POST["submit_new_student"]) ){
//step 1. get values from the HTML form
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$age = $_POST["age"];
//step 2: create a INSERT query string
$insert_student_query = "INSERT INTO tbl_student ";
$insert_student_query .= "(student_firstname,student_lastname,student_age) ";
$insert_student_query .= "VALUES ('$firstname','$lastname','$age') ";
//step 3: let execute the INSERT query (and we catch the result if for success or fail)
$result = mysqli_query($connection, $insert_student_query);
//step 4: optional. we want to display something if success or fail
if($result){
$message = "1 student successfully added";
} else {
$message = mysqli_error($connection);
}
}
//end end of ADD A STUDENT process
//This will process the EDIT A STUDENT button
if( isset($_POST["edit_student"]) ){
//step 1. get values from the HTML form
$id = $_POST["id"];
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$age = $_POST["age"];
//step 2: create a UPDATE query string
$update_student_query = "UPDATE tbl_student ";
$update_student_query .= "SET student_firstname='$firstname', student_lastname='$lastname', student_age='$age' ";
$update_student_query .= "WHERE student_id='$id' ";
//step 3: let execute the UPDATE query (and we catch the result if for success or fail)
$result = mysqli_query($connection, $update_student_query);
//step 4: optional. we want to display something if success or fail
if($result){
$message = "1 student successfully updated";
} else {
$message = mysqli_error($connection);
}
}
//end end of EDIT A STUDENT button
//This will process the DELETE A STUDENT button
if( isset($_POST["delete_student"]) ){
//step 1. get values from the HTML form
$id = $_POST["id"];
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$age = $_POST["age"];
//step 2: create a DELETE query string
$update_student_query = "DELETE FROM tbl_student ";
$update_student_query .= "WHERE student_id='$id' ";
//step 3: let execute the DELETE query (and we catch the result if for success or fail)
$result = mysqli_query($connection, $update_student_query);
//step 4: optional. we want to display something if success or fail
if($result){
$message = "1 student successfully deleted";
} else {
$message = mysqli_error($connection);
}
}
//end end of DELETE A STUDENT button
?>
<!DOCTYPE html>
<html>
<head>
<title>Administer Students Table</title>
<style>
.student_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="student_admin"><!-- STUDENT ADMIN div-->
<div class="leftcontainer"><!-- Left Container-->
<div><!-- ADD A STUDENT-->
<p>ADD A STUDENT</p>
<p style="color: green; font-style: italic; size: .7em"><?php echo $message; ?></p>
<form method="POST" action="administer_student_table.php">
Student First Name:<input type="text" name="firstname"><br>
Student Last Name:<input type="text" name="lastname"><br>
Student Age:<input type="text" name="age"><br>
<input type="submit" name="submit_new_student">
</form>
</div><!-- end of ADD A STUDENT-->
</div><!-- end of left container-->
<div class="rightcontainer">
<div><!-- DISPLAY ALL STUDENTS with EDIT + DELETE FUNCTIONS-->
<p>ALL STUDENTS</p>
<?php
$query_all_students = "SELECT * FROM tbl_student";
$result = mysqli_query($connection,$query_all_students);
if($result){
while($record=mysqli_fetch_array($result)){
echo '<form method="POST" action="administer_student_table.php">';
$id = $record['student_id'];
echo '<input type="hidden" name="id" value="'.$id.'">';
echo 'Student ID' . $id . " - ";
echo '<input type="text" name="firstname" value="'. $record['student_firstname'] .'">';
echo '<input type="text" name="lastname" value="'. $record['student_lastname'] .'">';
echo '<input type="text" name="age" value="'. $record['student_age'] .'">';
echo '<input type="submit" name="edit_student" value="Edit">';
echo '<input type="submit" name="delete_student" value="Delete" onclick="return confirm(\'Are you 100% sure to delete?\')">';
echo '</form>';
}
}
?>
</div><!-- end of DISPLAY ALL STUDENTS with EDIT + DELETE FUNCTIONS-->
</div><!-- end of right container-->
<div class="clearfix"></div>
</div><!-- end STUDENT ADMIN div-->
</div><!-- end of PAGE div-->
<p><center><a href="index.php">Back to Main Menu</a></center></p>
</body>
</html>
******Next, I will show you how to add data to a table using PHPMYADMIN******