Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
10 views6 pages

Web Applications Lab Manual 2

The document is a lab manual for creating web applications using PHP and MySQL. It covers user registration, login systems, CRUD operations for product management, a contact form for feedback, and session management for user control. Each section includes steps and sample code for implementation.

Uploaded by

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

Web Applications Lab Manual 2

The document is a lab manual for creating web applications using PHP and MySQL. It covers user registration, login systems, CRUD operations for product management, a contact form for feedback, and session management for user control. Each section includes steps and sample code for implementation.

Uploaded by

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

Web Applications Lab Manual 2 - PHP

and MySQL
1. User Registration with PHP and MySQL
Create a PHP form to register a user and store the data in a MySQL database.

Steps:
 Create `register.php` with a form to input name, email, and password.
 Connect to MySQL using `mysqli_connect`.
 Insert form data into a table `users` using `INSERT INTO` SQL query.
 Validate inputs before saving to database.
 Confirm successful registration with a message.

Code:

<?php
$conn = mysqli_connect("localhost", "root", "", "webapp");
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$pass = $_POST['password'];
$query = "INSERT INTO users(name, email, password) VALUES('$name', '$email', '$pass')";
mysqli_query($conn, $query);
echo "Registered Successfully!";
}
?>
<form method="post">
Name: <input type="text" name="name"><br>
Email: <input type="email" name="email"><br>
Password: <input type="password" name="password"><br>
<input type="submit" name="submit" value="Register">
</form>
2. User Login System
Create a login page in PHP that verifies credentials from a MySQL database.

Steps:
 Create `login.php` with username and password fields.
 Use `SELECT` query to check credentials in `users` table.
 Start a PHP session if credentials are valid.
 Redirect to `dashboard.php` on success.
 Show error message if login fails.

Code:

<?php
session_start();
$conn = mysqli_connect("localhost", "root", "", "webapp");
if(isset($_POST['login'])) {
$email = $_POST['email'];
$pass = $_POST['password'];
$res = mysqli_query($conn, "SELECT * FROM users WHERE email='$email' AND
password='$pass'");
if(mysqli_num_rows($res) == 1) {
$_SESSION['user'] = $email;
header("Location: dashboard.php");
} else {
echo "Invalid credentials.";
}
}
?>
<form method="post">
Email: <input type="email" name="email"><br>
Password: <input type="password" name="password"><br>
<input type="submit" name="login" value="Login">
</form>
3. CRUD Operations
Create a product manager in PHP with Add, View, Edit, and Delete functionalities.

Steps:
 Create `add_product.php` to insert product data.
 Create `view_products.php` to list all products with Edit/Delete links.
 Use `GET` method to pass product ID for editing and deleting.
 Update and delete using `UPDATE` and `DELETE` SQL queries.
 Connect pages together using navigation links.

Code:

// Sample snippet: add_product.php


<form method="post">
Product Name: <input type="text" name="pname"><br>
Price: <input type="text" name="price"><br>
<input type="submit" name="add" value="Add Product">
</form>
<?php
$conn = mysqli_connect("localhost", "root", "", "webapp");
if(isset($_POST['add'])) {
$name = $_POST['pname'];
$price = $_POST['price'];
mysqli_query($conn, "INSERT INTO products(name, price) VALUES('$name', '$price')");
echo "Product Added!";
}
?>
4. Contact Us Form with PHP
Create a contact form to collect feedback and store it in the database.

Steps:
 Create a form in `contact.php` for name, email, and message.
 Save data to a table `feedback` in MySQL.
 Use `htmlspecialchars` to secure input.
 Display a thank-you message on submission.
 Allow admin to view feedback via `view_feedback.php`.

Code:

<form method="post">
Name: <input type="text" name="name"><br>
Email: <input type="email" name="email"><br>
Message:<br><textarea name="msg"></textarea><br>
<input type="submit" name="send" value="Send">
</form>
<?php
$conn = mysqli_connect("localhost", "root", "", "webapp");
if(isset($_POST['send'])) {
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$msg = htmlspecialchars($_POST['msg']);
mysqli_query($conn, "INSERT INTO feedback(name, email, message) VALUES('$name',
'$email', '$msg')");
echo "Thank you for your feedback!";
}
?>
5. Session Management
Implement session control for user login/logout.

Steps:
 Start session in `login.php` using `session_start()`. Store user info in session variable.
 Use `isset($_SESSION['user'])` to protect dashboard page.
 Create `logout.php` to destroy session using `session_destroy()`. Redirect to login.
 Show user’s email in dashboard using `$_SESSION`.
 Ensure logout clears session data.

Code:

// logout.php
<?php
session_start();
session_destroy();
header("Location: login.php");
?>

You might also like