0% found this document useful (0 votes)
16 views9 pages

Codes

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

Codes

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

1.

Database Design (MySQL)

You’ll need a database to store information. Below is an example SQL code to create tables for students,
teachers, and courses.

sql

Copy code

CREATE DATABASE school_management;

USE school_management;

-- Table for Students

CREATE TABLE students (

student_id INT AUTO_INCREMENT PRIMARY KEY,

first_name VARCHAR(100),

last_name VARCHAR(100),

dob DATE,

gender VARCHAR(10),

email VARCHAR(100),

phone VARCHAR(15)

);

-- Table for Teachers

CREATE TABLE teachers (

teacher_id INT AUTO_INCREMENT PRIMARY KEY,

first_name VARCHAR(100),

last_name VARCHAR(100),
subject VARCHAR(100),

email VARCHAR(100),

phone VARCHAR(15)

);

-- Table for Courses

CREATE TABLE courses (

course_id INT AUTO_INCREMENT PRIMARY KEY,

course_name VARCHAR(100),

teacher_id INT,

FOREIGN KEY (teacher_id) REFERENCES teachers(teacher_id)

);

2. PHP Code for Backend (CRUD Operations)

Create PHP scripts to interact with the database. Below are examples for adding, displaying, updating,
and deleting students, teachers, and courses.

Database Connection (db.php)

php

Copy code

<?php

$servername = "localhost";

$username = "root"; // Your database username

$password = ""; // Your database password

$dbname = "school_management"; // Your database name

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

// Check connection

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);

?>

Adding a Student (add_student.php)

php

Copy code

<?php

include('db.php');

if (isset($_POST['submit'])) {

$first_name = $_POST['first_name'];

$last_name = $_POST['last_name'];

$dob = $_POST['dob'];

$gender = $_POST['gender'];

$email = $_POST['email'];

$phone = $_POST['phone'];

$sql = "INSERT INTO students (first_name, last_name, dob, gender, email, phone) VALUES
('$first_name', '$last_name', '$dob', '$gender', '$email', '$phone')";

if ($conn->query($sql) === TRUE) {

echo "New student added successfully";


} else {

echo "Error: " . $sql . "<br>" . $conn->error;

?>

<form method="POST">

<input type="text" name="first_name" placeholder="First Name" required><br>

<input type="text" name="last_name" placeholder="Last Name" required><br>

<input type="date" name="dob" required><br>

<select name="gender">

<option value="Male">Male</option>

<option value="Female">Female</option>

</select><br>

<input type="email" name="email" placeholder="Email" required><br>

<input type="text" name="phone" placeholder="Phone" required><br>

<button type="submit" name="submit">Add Student</button>

</form>

Displaying Students (view_students.php)

php

Copy code

<?php

include('db.php');

$sql = "SELECT * FROM students";


$result = $conn->query($sql);

if ($result->num_rows > 0) {

while($row = $result->fetch_assoc()) {

echo "ID: " . $row["student_id"]. " - Name: " . $row["first_name"]. " " . $row["last_name"]. "<br>";

} else {

echo "0 results";

?>

Updating Student Data (update_student.php)

php

Copy code

<?php

include('db.php');

if (isset($_GET['id'])) {

$student_id = $_GET['id'];

$sql = "SELECT * FROM students WHERE student_id=$student_id";

$result = $conn->query($sql);

$student = $result->fetch_assoc();

if (isset($_POST['submit'])) {

$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];

$dob = $_POST['dob'];

$gender = $_POST['gender'];

$email = $_POST['email'];

$phone = $_POST['phone'];

$sql = "UPDATE students SET first_name='$first_name', last_name='$last_name', dob='$dob',


gender='$gender', email='$email', phone='$phone' WHERE student_id=$student_id";

if ($conn->query($sql) === TRUE) {

echo "Student updated successfully";

} else {

echo "Error: " . $conn->error;

?>

<form method="POST">

<input type="text" name="first_name" value="<?php echo $student['first_name']; ?>" required><br>

<input type="text" name="last_name" value="<?php echo $student['last_name']; ?>" required><br>

<input type="date" name="dob" value="<?php echo $student['dob']; ?>" required><br>

<select name="gender">

<option value="Male" <?php echo ($student['gender'] == 'Male') ? 'selected' : ''; ?>>Male</option>

<option value="Female" <?php echo ($student['gender'] == 'Female') ? 'selected' : '';


?>>Female</option>

</select><br>
<input type="email" name="email" value="<?php echo $student['email']; ?>" required><br>

<input type="text" name="phone" value="<?php echo $student['phone']; ?>" required><br>

<button type="submit" name="submit">Update Student</button>

</form>

3. Frontend Design (HTML and CSS)

The front-end pages will include forms and tables to display and interact with student data.

CSS (styles.css)

css

Copy code

body {

font-family: Arial, sans-serif;

margin: 0;

padding: 0;

background-color: #f4f4f4;

form {

background-color: #fff;

padding: 20px;

margin: 20px;

border-radius: 5px;

box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);

}
input[type="text"], input[type="email"], input[type="date"], select {

width: 100%;

padding: 10px;

margin: 5px 0;

border: 1px solid #ccc;

border-radius: 5px;

button {

background-color: #4CAF50;

color: white;

padding: 10px 20px;

border: none;

cursor: pointer;

border-radius: 5px;

button:hover {

background-color: #45a049;

4. Complete Example

You will have several PHP pages:

add_student.php: To add a new student.

view_students.php: To display all students.


update_student.php: To update student information.

db.php: To manage the connection to the database.

You might also like