WT_Practical_7
WT_Practical_7
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);
?>
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>
<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;
}
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>
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;
}
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>
Output(P7_5.php):