NAME : Kavali Roshan PRN NO : ADT24MGTM0937
DIV : C CLASS : MCA-I-Sem-II-DS
SUBJECT : Web Technologies
1. PHP MySQL Connection
Question: Write a PHP script to connect to a MySQL database using mysqli.
<!DOCTYPE html>
<html>
<head><title>MySQL Connection</title></head>
<body>
<h2>PHP MySQL Connection Test</h2>
<?php
$conn = new mysqli("localhost", "root", "", "php_journal");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
echo "Connected successfully!";
?>
</body>
</html>
OUTPUT :
1 (24MCAL201) Web Technologies
NAME : Kavali Roshan PRN NO : ADT24MGTM0937
DIV : C CLASS : MCA-I-Sem-II-DS
SUBJECT : Web Technologies
2. Insert Data into MySQL
Question: Create an HTML form that takes name and email, then write a
PHP script to insert the data into a MySQL table.
<!DOCTYPE html>
<html>
<head>
<title>Insert User</title>
</head>
<body>
<h2>Insert User Data</h2>
<form action="" method="post">
Name: <input type="text" name="name" required><br><br>
Email: <input type="email" name="email" required><br><br>
<input type="submit" value="Insert">
</form>
<br>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$conn = new mysqli("localhost", "root", "", "php_journal");
2 (24MCAL201) Web Technologies
NAME : Kavali Roshan PRN NO : ADT24MGTM0937
DIV : C CLASS : MCA-I-Sem-II-DS
SUBJECT : Web Technologies
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
$name = $_POST['name'];
$email = $_POST['email'];
$sql = "INSERT INTO users (name, email) VALUES ('$name', '$email')";
if ($conn->query($sql) === TRUE) {
echo "<p style='color:green;'> Record inserted successfully!</p>";
} else {
echo "<p style='color:red;'> Error: " . $conn->error . "</p>";
$conn->close();
?>
</body>
</html>
3 (24MCAL201) Web Technologies
NAME : Kavali Roshan PRN NO : ADT24MGTM0937
DIV : C CLASS : MCA-I-Sem-II-DS
SUBJECT : Web Technologies
OUTPUT :
4 (24MCAL201) Web Technologies
NAME : Kavali Roshan PRN NO : ADT24MGTM0937
DIV : C CLASS : MCA-I-Sem-II-DS
SUBJECT : Web Technologies
3. Retrieve Data from MySQL
Question: Write PHP code to fetch and display all records from a MySQL
table in an HTML table.
<!DOCTYPE html>
<html>
<head><title>Display Users</title></head>
<body>
<h2>User Records</h2>
<table border="1">
<tr><th>ID</th><th>Name</th><th>Email</th></tr>
<?php
$conn = new mysqli("localhost", "root", "", "php_journal");
$result = $conn->query("SELECT * FROM users");
while ($row = $result->fetch_assoc()) {
echo "<tr><td>{$row['id']}</td><td>{$row['name']}</td><td>{$row['email']}</
td></tr>";
}
?>
</table>
</body>
</html>
OUTPUT :
5 (24MCAL201) Web Technologies
NAME : Kavali Roshan PRN NO : ADT24MGTM0937
DIV : C CLASS : MCA-I-Sem-II-DS
SUBJECT : Web Technologies
4. Update MySQL Records
Question: Create a form that allows updating a user's email by entering
their ID.
<!DOCTYPE html>
<html>
<head><title>Update User Email</title></head>
<body>
<h2>Update User Email</h2>
<form action="" method="post">
Enter User Name: <input type="text" name="name" required><br><br>
New Email: <input type="email" name="email" required><br><br>
<input type="submit" value="Update">
</form>
<br>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$conn = new mysqli("localhost", "root", "", "php_journal");
if ($conn->connect_error) {
die("Connection failed");
}
$name = $_POST['name'];
$email = $_POST['email'];
$sql = "UPDATE users SET email='$email' WHERE name='$name'";
if ($conn->query($sql) === TRUE) {
if ($conn->affected_rows > 0) {
6 (24MCAL201) Web Technologies
NAME : Kavali Roshan PRN NO : ADT24MGTM0937
DIV : C CLASS : MCA-I-Sem-II-DS
SUBJECT : Web Technologies
echo "<p style='color:green;'> Email updated successfully for User:
$name</p>";
} else {
echo "<p style='color:orange;'> No user found with name: $name</p>";
}
} else {
echo "<p style='color:red;'> Error: " . $conn->error . "</p>";
}
$conn->close();
}
?>
</body>
</html>
OUTPUT :
7 (24MCAL201) Web Technologies
NAME : Kavali Roshan PRN NO : ADT24MGTM0937
DIV : C CLASS : MCA-I-Sem-II-DS
SUBJECT : Web Technologies
8 (24MCAL201) Web Technologies
NAME : Kavali Roshan PRN NO : ADT24MGTM0937
DIV : C CLASS : MCA-I-Sem-II-DS
SUBJECT : Web Technologies
5. Delete a Record from MySQL
Question: Write a PHP script to delete a specific record from a table based
on the user ID passed via GET.
<!DOCTYPE html>
<html>
<head><title>Delete User Record</title></head>
<body>
<h2>Delete User by Name</h2>
<form action="" method="POST">
Enter User Name: <input type="text" name="name" required><br><br>
<input type="submit" value="Delete User">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$conn = new mysqli("localhost", "root", "", "php_journal");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$name = $_POST['name'];
// Prepare the SQL query to delete the user by name
$sql = "DELETE FROM users WHERE name='$name'";
if ($conn->query($sql) === TRUE) {
if ($conn->affected_rows > 0) {
echo "User with name '$name' deleted successfully.";
} else {
echo "No user found with name '$name'.";
9 (24MCAL201) Web Technologies
NAME : Kavali Roshan PRN NO : ADT24MGTM0937
DIV : C CLASS : MCA-I-Sem-II-DS
SUBJECT : Web Technologies
}
} else {
echo "Error: " . $conn->error;
}
$conn->close();
}
?>
</body>
</html>
OUTPUT :
10 (24MCAL201) Web Technologies
NAME : Kavali Roshan PRN NO : ADT24MGTM0937
DIV : C CLASS : MCA-I-Sem-II-DS
SUBJECT : Web Technologies
6. User Registration System
Question: Build a user registration form and store the user's data (name,
email, password) in the database securely using password hashing.
<!DOCTYPE html>
<html>
<head><title>User Registration</title></head>
<body>
<h2>Register</h2>
<form method="post">
Name: <input type="text" name="name" required><br><br>
Email: <input type="email" name="email" required><br><br>
Password: <input type="password" name="password" required><br><br>
<input type="submit" value="Register">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$conn = new mysqli("localhost", "root", "", "php_journal");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$name = $_POST["name"];
$email = $_POST["email"];
$password = password_hash($_POST["password"], PASSWORD_DEFAULT);
$sql = "INSERT INTO users (name, email, password) VALUES ('$name',
'$email', '$password')";
if ($conn->query($sql) === TRUE) {
echo "<p>User registered successfully!</p>";
11 (24MCAL201) Web Technologies
NAME : Kavali Roshan PRN NO : ADT24MGTM0937
DIV : C CLASS : MCA-I-Sem-II-DS
SUBJECT : Web Technologies
} else {
echo "<p>Error: " . $conn->error . "</p>";
}
$conn->close();
}
?>
</body>
</html>
OUTPUT :
7. User Login System
12 (24MCAL201) Web Technologies
NAME : Kavali Roshan PRN NO : ADT24MGTM0937
DIV : C CLASS : MCA-I-Sem-II-DS
SUBJECT : Web Technologies
Question: Create a login form and authenticate users using data stored in
the MySQL table.
<!DOCTYPE html>
<html>
<head><title>User Login</title></head>
<body>
<h2>Login</h2>
<form method="post">
Email: <input type="email" name="email" required><br><br>
Password: <input type="password" name="password" required><br><br>
<input type="submit" value="Login">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$conn = new mysqli("localhost", "root", "", "php_journal");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$email = $_POST["email"];
$password = $_POST["password"];
$result = $conn->query("SELECT * FROM users WHERE email='$email'");
if ($result && $result->num_rows > 0) {
$user = $result->fetch_assoc();
if ($password === $user["password"]) {
echo "<p>Welcome, " . $user["name"] . "!</p>";
} else {
echo "<p>Invalid password.</p>";
}
13 (24MCAL201) Web Technologies
NAME : Kavali Roshan PRN NO : ADT24MGTM0937
DIV : C CLASS : MCA-I-Sem-II-DS
SUBJECT : Web Technologies
} else {
echo "<p>Email not found.</p>";
}
$conn->close();
}
?>
</body>
</html>
OUTPUT :
14 (24MCAL201) Web Technologies
NAME : Kavali Roshan PRN NO : ADT24MGTM0937
DIV : C CLASS : MCA-I-Sem-II-DS
SUBJECT : Web Technologies
8. Search Data in MySQL Table
Question: Build a search form that filters and shows records matching the
entered keyword from a MySQL table.
<!DOCTYPE html>
<html>
<head>
<title>Search by Name</title>
</head>
<body>
<h2>Search Users by Name</h2>
<form method="get">
Name: <input type="text" name="keyword" required>
<input type="submit" value="Search">
</form>
<?php
if (isset($_GET['keyword']) && !empty($_GET['keyword'])) {
$conn = new mysqli("localhost", "root", "", "php_journal");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$keyword = $conn->real_escape_string($_GET['keyword']);
$sql = "SELECT * FROM users WHERE name LIKE '%$keyword%'";
$result = $conn->query($sql);
if ($result && $result->num_rows > 0) {
echo "<h3>Search Results:</h3>";
echo "<table border='1'>
<tr>
15 (24MCAL201) Web Technologies
NAME : Kavali Roshan PRN NO : ADT24MGTM0937
DIV : C CLASS : MCA-I-Sem-II-DS
SUBJECT : Web Technologies
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr>
<td>{$row['id']}</td>
<td>{$row['name']}</td>
<td>{$row['email']}</td>
</tr>";
}
echo "</table>";
} else {
echo "<p>No users found with that name.</p>";
}
$conn->close();
}
?>
</body>
</html>
OUTPUT :
16 (24MCAL201) Web Technologies
NAME : Kavali Roshan PRN NO : ADT24MGTM0937
DIV : C CLASS : MCA-I-Sem-II-DS
SUBJECT : Web Technologies
9. Display Data with Pagination
Question: Retrieve and display records from a database with pagination
(e.g., 5 records per page).
<!DOCTYPE html>
<html>
<head>
<title>Paginated User List</title>
</head>
<body>
<h2>Users (5 per page)</h2>
<?php
$conn = new mysqli("localhost", "root", "", "php_journal");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? (int)
$_GET['page'] : 1;
$limit = 5;
$offset = ($page - 1) * $limit;
$totalResult = $conn->query("SELECT COUNT(*) AS total FROM users");
$totalRow = $totalResult->fetch_assoc();
$totalRecords = $totalRow['total'];
$totalPages = ceil($totalRecords / $limit);
$sql = "SELECT * FROM users LIMIT $limit OFFSET $offset";
$result = $conn->query($sql);
17 (24MCAL201) Web Technologies
NAME : Kavali Roshan PRN NO : ADT24MGTM0937
DIV : C CLASS : MCA-I-Sem-II-DS
SUBJECT : Web Technologies
if ($result && $result->num_rows > 0) {
echo "<table border='1'>
<tr><th>ID</th><th>Name</th><th>Email</th></tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr>
<td>{$row['id']}</td>
<td>{$row['name']}</td>
<td>{$row['email']}</td>
</tr>";
}
echo "</table>";
} else {
echo "<p>No records found.</p>";
}
echo "<div style='margin-top:20px;'>";
if ($page > 1) {
echo "<a href='?page=" . ($page - 1) . "'>« Prev</a> ";
}
for ($i = 1; $i <= $totalPages; $i++) {
echo "<a href='?page=$i' style='margin:0 5px;'>" . ($i == $page ?
"<strong>$i</strong>" : $i) . "</a>";
}
if ($page < $totalPages) {
echo "<a href='?page=" . ($page + 1) . "'>Next »</a>";
}
echo "</div>";
$conn->close();
?>
</body>
</html>
18 (24MCAL201) Web Technologies
NAME : Kavali Roshan PRN NO : ADT24MGTM0937
DIV : C CLASS : MCA-I-Sem-II-DS
SUBJECT : Web Technologies
OUTPUT :
19 (24MCAL201) Web Technologies
NAME : Kavali Roshan PRN NO : ADT24MGTM0937
DIV : C CLASS : MCA-I-Sem-II-DS
SUBJECT : Web Technologies
10. Upload and Save Image to MySQL
Question: Create an image upload form. Save the image to a folder and
store the file name/path in a MySQL table.
<!DOCTYPE html>
<html>
<head><title>Image Upload</title></head>
<body>
<h2>Upload an Image</h2>
<form action="" method="post" enctype="multipart/form-data">
Select image to upload: <input type="file" name="image" required><br><br>
<input type="submit" value="Upload Image">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$conn = new mysqli("localhost", "root", "", "php_journal");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (isset($_FILES["image"]) && $_FILES["image"]["error"] == 0) {
$imageName = $_FILES["image"]["name"];
$imageTmpName = $_FILES["image"]["tmp_name"];
$imageType = mime_content_type($imageTmpName);
$allowedTypes = ["image/jpeg", "image/png", "image/gif"];
if (in_array($imageType, $allowedTypes)) {
$uploadDir = "uploads/";
if (!is_dir($uploadDir)) {
mkdir($uploadDir, 0777, true);
20 (24MCAL201) Web Technologies
NAME : Kavali Roshan PRN NO : ADT24MGTM0937
DIV : C CLASS : MCA-I-Sem-II-DS
SUBJECT : Web Technologies
$newImageName = uniqid() . "-" . basename($imageName);
$uploadPath = $uploadDir . $newImageName;
if (move_uploaded_file($imageTmpName, $uploadPath)) {
$stmt = $conn->prepare("INSERT INTO images (image_path) VALUES
(?)");
$stmt->bind_param("s", $uploadPath);
if ($stmt->execute()) {
echo "Image uploaded successfully!";
} else {
echo "Error: Could not save to database.";
}
$stmt->close();
} else {
echo "Error: Could not upload the image.";
}
} else {
echo "Error: Invalid image file type.";
}
} else {
echo "Error: No file selected or file upload error.";
}
$conn->close();
}
?>
</body>
</html>
21 (24MCAL201) Web Technologies
NAME : Kavali Roshan PRN NO : ADT24MGTM0937
DIV : C CLASS : MCA-I-Sem-II-DS
SUBJECT : Web Technologies
OUTPUT :
22 (24MCAL201) Web Technologies