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

2nd php notes

Uploaded by

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

2nd php notes

Uploaded by

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

PHP Project Codes (Beginner to Advanced)

1. Simple Calculator using PHP


<!DOCTYPE html>
<html>
<body>
<form method="post">
Number 1: <input type="text" name="num1"><br>
Number 2: <input type="text" name="num2"><br>
<select name="operation">
<option value="add">Add</option>
<option value="sub">Subtract</option>
<option value="mul">Multiply</option>
<option value="div">Divide</option>
</select>
<input type="submit" name="submit" value="Calculate">
</form>
<?php
if(isset($_POST['submit'])) {
$a = $_POST['num1'];
$b = $_POST['num2'];
$op = $_POST['operation'];
if ($op == "add") echo $a + $b;
elseif ($op == "sub") echo $a - $b;
elseif ($op == "mul") echo $a * $b;
elseif ($op == "div") echo $b != 0 ? $a / $b : "Cannot divide by zero";
}
?>
</body>
</html>
2. Login and Registration System
-- MySQL Table --
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50),
password VARCHAR(255)
);

-- register.php --
<form method="post">
<input name="username"><input type="password" name="password"><input type="submit">
</form>
<?php
$conn = mysqli_connect('localhost','root','','test');
if ($_POST) {
$user = $_POST['username'];
$pass = password_hash($_POST['password'], PASSWORD_DEFAULT);
mysqli_query($conn, "INSERT INTO users (username, password) VALUES ('$user', '$pass')");
}
?>

-- login.php --
<form method="post">
<input name="username"><input type="password" name="password"><input type="submit">
</form>
<?php
$conn = mysqli_connect('localhost','root','','test');
if ($_POST) {
$user = $_POST['username'];
$pass = $_POST['password'];
$result = mysqli_query($conn, "SELECT * FROM users WHERE username='$user'");
$data = mysqli_fetch_assoc($result);
if (password_verify($pass, $data['password'])) echo 'Login Successful';
else echo 'Login Failed';
}
?>
3. Contact Form with Email Sending
<form method="post">
Name: <input name="name"><br>
Email: <input name="email"><br>
Message: <textarea name="message"></textarea><br>
<input type="submit">
</form>
<?php
if ($_POST) {
$to = "your@email.com";
$subject = "Contact from " . $_POST['name'];
$body = $_POST['message'];
$headers = "From: " . $_POST['email'];
mail($to, $subject, $body, $headers);
echo "Message Sent!";
}
?>
4. File Upload System
<form method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="upload" value="Upload">
</form>
<?php
if (isset($_POST['upload'])) {
$target = "uploads/" . basename($_FILES['file']['name']);
if (move_uploaded_file($_FILES['file']['tmp_name'], $target)) {
echo "Uploaded Successfully";
} else {
echo "Failed to upload";
}
}
?>
5. Basic Blog System
-- MySQL Table --
CREATE TABLE posts (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(100),
content TEXT
);

-- post.php --
<form method="post">
Title: <input name="title"><br>
Content: <textarea name="content"></textarea><br>
<input type="submit">
</form>
<?php
$conn = mysqli_connect('localhost','root','','test');
if ($_POST) {
$t = $_POST['title'];
$c = $_POST['content'];
mysqli_query($conn, "INSERT INTO posts (title, content) VALUES ('$t','$c')");
}
?>
6. Online Quiz Application
-- questions table: id, question, opt1, opt2, opt3, correct --
<?php
$conn = mysqli_connect('localhost','root','','test');
$result = mysqli_query($conn, "SELECT * FROM questions");
echo '<form method="post">';
while($row = mysqli_fetch_assoc($result)) {
echo "<p>{$row['question']}<br>";
echo "<input type='radio' name='q{$row['id']}' value='1'>{$row['opt1']}<br>";
echo "<input type='radio' name='q{$row['id']}' value='2'>{$row['opt2']}<br>";
echo "<input type='radio' name='q{$row['id']}' value='3'>{$row['opt3']}<br></p>";
}
echo "<input type='submit'></form>";
?>
7. Student Management System
-- MySQL: students(id, name, age, grade) --
<form method="post">
Name: <input name="name"> Age: <input name="age"> Grade: <input name="grade"><br>
<input type="submit" value="Add Student">
</form>
<?php
$conn = mysqli_connect('localhost','root','','test');
if ($_POST) {
$n = $_POST['name'];
$a = $_POST['age'];
$g = $_POST['grade'];
mysqli_query($conn, "INSERT INTO students (name, age, grade) VALUES ('$n','$a','$g')");
}
?>
8. Chat Application using PHP and AJAX
-- messages table (id, user, message) --
// chat.php + ajax.js to load and post messages
// Send: POST user + msg to PHP -> insert into DB
// Load: JS loads messages every few seconds using fetch/ajax
9. E-commerce Product Display Page
-- MySQL: products(id, name, price, image) --
<?php
$conn = mysqli_connect('localhost','root','','test');
$result = mysqli_query($conn, "SELECT * FROM products");
while($row = mysqli_fetch_assoc($result)) {
echo "<div><img src='img/{$row['image']}' width='100'><br>{$row['name']} -
{$row['price']}</div>";
}
?>
10. Smart Parking System
<?php
session_start();
if (!isset($_SESSION['total_spots'])) {
$_SESSION['total_spots'] = 100;
$_SESSION['occupied_spots'] = 0;
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$choice = $_POST['choice'];
if ($choice == 2 && $_SESSION['occupied_spots'] < $_SESSION['total_spots'])
$_SESSION['occupied_spots']++;
elseif ($choice == 3 && $_SESSION['occupied_spots'] > 0) $_SESSION['occupied_spots']--;
elseif ($choice == 4) { session_destroy(); header("Location:"); exit(); }
}
$available_spots = $_SESSION['total_spots'] - $_SESSION['occupied_spots'];
?>
<form method="post">
<button name="choice" value="2">Park a Car</button>
<button name="choice" value="3">Free a Spot</button>
<button name="choice" value="4">Reset</button>
</form>
<p>Available: <?= $available_spots ?> | Occupied: <?= $_SESSION['occupied_spots'] ?></p>

You might also like