0% found this document useful (0 votes)
12 views

Last Code Info Bryan

The document shows how to create a student database using MySQL and connect it to a PHP web application to perform CRUD operations on student records including adding, retrieving, updating and deleting students.

Uploaded by

Nicko De Guzman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Last Code Info Bryan

The document shows how to create a student database using MySQL and connect it to a PHP web application to perform CRUD operations on student records including adding, retrieving, updating and deleting students.

Uploaded by

Nicko De Guzman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

MySql

CREATE DATABASE student_db;


USE student_db;

CREATE TABLE students (


id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
student_id VARCHAR(50) NOT NULL UNIQUE,
course VARCHAR(50) NOT NULL,
semester VARCHAR(50) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

2. Create a configuration file (config.php)

<?php
$servername = "localhost";
$username = "root"; // Your MySQL username
$password = ""; // Your MySQL password
$dbname = "student_db";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>

3. Create a functions file (functions.php)

<?php
require_once 'config.php';

function getAllStudents() {
global $conn;
$sql = "SELECT * FROM students";
$result = $conn->query($sql);
return $result;
}

function getStudentById($id) {
global $conn;
$sql = "SELECT * FROM students WHERE id=$id";
$result = $conn->query($sql);
return $result->fetch_assoc();
}

function addStudent($first_name, $last_name, $student_id, $course, $semester) {


global $conn;
$sql = "INSERT INTO students (first_name, last_name, student_id, course,
semester) VALUES ('$first_name', '$last_name', '$student_id', '$course',
'$semester')";
return $conn->query($sql);
}

function updateStudent($id, $first_name, $last_name, $student_id, $course,


$semester) {
global $conn;
$sql = "UPDATE students SET first_name='$first_name', last_name='$last_name',
student_id='$student_id', course='$course', semester='$semester' WHERE id=$id";
return $conn->query($sql);
}

function deleteStudent($id) {
global $conn;
$sql = "DELETE FROM students WHERE id=$id";
return $conn->query($sql);
}
?>

4. Create the main PHP file (index.php)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Student Card</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f5f5f5;
padding: 20px;
}

h1 {
color: #28b463;
margin-bottom: 30px;
}

form {
background-color: #fff;
padding: 20px;
border-radius: 4px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

label {
display: inline-block;
width: 150px;
text-align: right;
margin-right: 10px;
font-weight: bold;
color: #333;
}

input[type="text"] {
width: 300px;
padding: 5px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 16px;
line-height: 1.5;
color: #333;
}
input[type="submit"] {
background-color: #28b463;
color: #fff;
border: none;
padding: 10px 20px;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}

input[type="submit"]:hover {
background-color: #1a8c50;
}
</style>
</head>
<body>
<h1>Student Card</h1>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"
method="post">
<label for="first_name">First Name:</label>
<input type="text" id="first_name" name="first_name" required><br>
<label for="last_name">Last Name:</label>
<input type="text" id="last_name" name="last_name" required><br>
<label for="student_id">Student ID:</label>
<input type="text" id="student_id" name="student_id" required><br>
<label for="course">Course:</label>
<input type="text" id="course" name="course" required><br>
<label for="semester">Semester:</label>
<input type="text" id="semester" name="semester" required><br>
<input type="submit" value="Add Student">
</form>
</body>
</html>

You might also like