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

WT_Practical_7

This document outlines a practical exercise for PHP programming with MySQL database integration. It includes step-by-step instructions for creating a database connection, creating a database, creating a customer table, and implementing a customer feedback form. Additionally, it provides code examples for each task and instructions for displaying customer information in a formatted table.

Uploaded by

Op Megh
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)
8 views

WT_Practical_7

This document outlines a practical exercise for PHP programming with MySQL database integration. It includes step-by-step instructions for creating a database connection, creating a database, creating a customer table, and implementing a customer feedback form. Additionally, it provides code examples for each task and instructions for displaying customer information in a formatted table.

Uploaded by

Op Megh
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/ 8

2CEIT6PE1_WEB TECHNOLOGY Practical_7

PRACTICAL 7

AIM: Perform following programs of PHP with database.


Exercise:
1. How do you create a connection between PHP and MySQL and explain its step by step
procedure?
Code(P7_1.php):
● Define connection variables: $servername, $username, $password, $dbname.
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database_name";
● Use mysqli_connect() to establish a connection.
$conn = mysqli_connect($servername, $username, $password, $dbname);
● Check if the connection is successful with if (!$conn).
● Use mysqli_connect_error() to display an error message if connection fails.
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
● Close the connection with mysqli_close($conn) when done.
echo "Connected successfully";
mysqli_close($conn);
2. Write a PHP Program to create a database at run time by passing the database name in
the text box.
Code(P7_2.php):
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$dbname = $_POST['dbname'];
$conn = mysqli_connect("localhost", "root", "");
if (!$conn) {
echo "Connection failed: " . mysqli_connect_error();
exit();
}
$sql = "CREATE DATABASE $dbname";
if (mysqli_query($conn, $sql)) {
echo "Database created successfully";
} else {
echo "Error creating database: " . mysqli_error($conn);
}
mysqli_close($conn);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Create Database</title>

21012011092_PATEL MEGH Page | 38


2CEIT6PE1_WEB TECHNOLOGY Practical_7

</head>
<body>
<h2>Create a New Database</h2>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<label for="dbname">Database Name:</label>
<input type="text" id="dbname" name="dbname" required>
<input type="submit" value="Create">
</form>
</body>
</html>
Output(P7_2.php):

3. Write a PHP Program to create a Customer table at run time with C_Id, C_name,
Name_Item_purchased and review_product fields.
Code(P7_3.php):
<?php
$conn = mysqli_connect("localhost", "root", "", "Customer");
if (!$conn) {
echo "Connection failed: " . mysqli_connect_error();
exit();
}
$query = "CREATE TABLE IF NOT EXISTS Customer (
C_Id VARCHAR(255) PRIMARY KEY,
C_name VARCHAR(255) NOT NULL,
Name_Item_purchased VARCHAR(255) NOT NULL,
product_review VARCHAR(255)
)";
if (mysqli_query($conn, $query)) {
echo "Table Customer created successfully";
} else {
echo "Error creating table: " . mysqli_error($conn);
}
mysqli_close($conn);
?>

21012011092_PATEL MEGH Page | 39


2CEIT6PE1_WEB TECHNOLOGY Practical_7

Output(P7_3.php):

4. Write a PHP program to create ‘customer feedback form’ and store data into the
Customer table.
Code(P7_4.php):
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['c_name'];
$product_name = $_POST['product_name'];
$product_rating = $_POST['product_rating'];
$product_review = $_POST['product_review'];
$conn = mysqli_connect("localhost", "root", "", "Customer");
if (!$conn) {
echo "Connection failed: " . mysqli_connect_error();
exit();
}
$query = "CREATE TABLE IF NOT EXISTS Customer (
C_Id INT(10) AUTO_INCREMENT PRIMARY KEY,
C_name VARCHAR(255),
Name_Item_purchased VARCHAR(255),
product_rating INT(10),
product_review VARCHAR(255)
)";
if (!mysqli_query($conn, $query)) {
echo "Error creating table: " . mysqli_error($conn);
exit();
}
$insert_query = "INSERT INTO Customer (C_name, Name_Item_purchased, product_rating,
product_review) VALUES ('$name', '$product_name', '$product_rating', '$product_review')";

if (mysqli_query($conn, $insert_query)) {
echo "<script>alert('Data Inserted Successfully')</script>";
} else {
echo "Error inserting data: " . mysqli_error($conn);
}
mysqli_close($conn);
}
?>
<!DOCTYPE html>

21012011092_PATEL MEGH Page | 40


2CEIT6PE1_WEB TECHNOLOGY Practical_7

<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Product Review Form</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
}
.container {
max-width: 600px;
margin: 50px auto;
background-color: #fff;
padding: 20px;
border-radius: 5px;
}
h1 {
text-align: center;
}
table {
width: 100%;
}
table td {
padding: 10px;
}
input, textarea {
width: 100%;
padding: 8px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
input[type="range"] {
width: 100%;
}
input[type="submit"] {
width: 100%;
background-color: #4caf50;
color: white;
padding: 10px 0;
border: none;
border-radius: 4px;
cursor: pointer;
}

21012011092_PATEL MEGH Page | 41


2CEIT6PE1_WEB TECHNOLOGY Practical_7

input[type="submit"]:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<div class="container">
<h1>Product Review Form</h1>
<form action="P7_4.php" method="post">
<table>
<tr>
<td><label for="c_name">Customer Name:</label></td>
<td><input type="text" id="c_name" name="c_name" required></td>
</tr>
<tr>
<td><label for="product_name">Product Name:</label></td>
<td><input type="text" id="product_name" name="product_name" required></td>
</tr>
<tr>
<td><label for="product_rating">Product Rating:</label></td>
<td><input type="range" id="product_rating" name="product_rating" min="0" max="5"
required></td>
</tr>
<tr>
<td><label for="product_review">Product Review:</label></td>
<td><textarea id="product_review" name="product_review" rows="4" required></textarea></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</div>
</body>
</html>

21012011092_PATEL MEGH Page | 42


2CEIT6PE1_WEB TECHNOLOGY Practical_7

Output(P7_4.php):

5. Write a PHP program to display information of Customer table with well formatting (table
form) on output screen.
Code(P7_5.php):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Customer Information</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
padding: 50px;
}
table {
width: 80%;
margin: 0 auto;
border: 1px solid #ddd;
}
th,
td {
padding: 15px;
text-align: left;
}

21012011092_PATEL MEGH Page | 43


2CEIT6PE1_WEB TECHNOLOGY Practical_7

th {
background-color: #f2f2f2;
}
tr:nth-child(even) {
background-color: #f9f9f9;
}
</style>
</head>
<body>
<h1>Customer Information</h1>
<?php
$conn = mysqli_connect("localhost", "root", "", "Customer");
if (!$conn) {
echo "Connection failed: " . mysqli_connect_error();
exit();
}
$query = "SELECT * FROM Customer";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
echo "<table border='1' cellpadding='10' cellspacing='0' style='margin: 20px auto; border-collapse:
collapse;'>
<tr>
<th>Customer ID</th>
<th>Customer Name</th>
<th>Item Purchased</th>
<th>Product Rating</th>
<th>Product Review</th> <!-- Added Product Review column -->
</tr>";
foreach ($result as $row) {
echo "<tr>
<td>" . $row['C_Id'] . "</td>
<td>" . $row['C_name'] . "</td>
<td>" . $row['Name_Item_purchased'] . "</td>
<td>" . $row['product_rating'] . "</td>
<td>" . $row['product_review'] . "</td> <!-- Added Product Review field -->
</tr>";
}
echo "</table>";
} else {
echo "No records found.";
}
mysqli_close($conn);
?>
</body>
</html>

21012011092_PATEL MEGH Page | 44


2CEIT6PE1_WEB TECHNOLOGY Practical_7

Output(P7_5.php):

21012011092_PATEL MEGH Page | 45

You might also like