PHP Practical
PHP Practical
<?php
$n = 10;
$first = 0;
$second = 1;
$first = $second;
$second = $next;
?>
<!DOCTYPE html>
<html>
<head>
<title>Employee Details</title>
</head>
<body>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = htmlspecialchars($_POST['name']);
$age = htmlspecialchars($_POST['age']);
$position = htmlspecialchars($_POST['position']);
?>
</body>
</html>
3..Write a PHP program to prepare the student marks list
<!DOCTYPE html>
<html>
<head>
</head>
<body>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = htmlspecialchars($_POST['name']);
$subject1 = (int)$_POST['subject1'];
$subject2 = (int)$_POST['subject2'];
$subject3 = (int)$_POST['subject3'];
?>
</body>
</html>
<?php
// Define matrices
$matrixA = [
[1, 2, 3],
[4, 5, 6]
];
$matrixB = [
[7, 8],
[9, 10],
[11, 12]
];
// Get matrix dimensions
$rowsA = count($matrixA);
$colsA = count($matrixA[0]);
$rowsB = count($matrixB);
$colsB = count($matrixB[0]);
if ($colsA != $rowsB) {
echo "Matrix multiplication not possible. Number of columns in Matrix A must equal number of rows
in Matrix B.";
exit;
$result = [];
$result[$i][$j] = 0;
echo "<br>";
?>
5.Create student registration form using text box, check box, radio button, select, submit button. And
display user inserted value in new PHP page.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<label for="name">Name:</label>
<label for="age">Age:</label>
<label for="gender">Gender:</label><br>
<option value="Mathematics">Mathematics</option>
<option value="Physics">Physics</option>
<option value="Chemistry">Chemistry</option>
</select><br><br>
<label>Hobbies:</label><br>
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
$name = htmlspecialchars($_POST['name']);
$age = htmlspecialchars($_POST['age']);
$gender = htmlspecialchars($_POST['gender']);
$course = htmlspecialchars($_POST['course']);
if (!empty($hobbies)) {
} else {
echo "None";
?>
</body>
</html>
6.Create Website Registration Form using text box, check box, radio button, select, submit button. And
display user inserted value in new PHP page.
<label for="email">Email:</label>
<input type="email" name="email" required><br><br>
<label for="password">Password:</label>
<input type="password" name="password" required><br><br>
<label for="gender">Gender:</label><br>
<input type="radio" name="gender" value="Male" required> Male<br>
<input type="radio" name="gender" value="Female" required> Female<br>
<input type="radio" name="gender" value="Other" required> Other<br><br>
<label for="country">Country:</label>
<select name="country" required>
<option value="United States">United States</option>
<option value="Canada">Canada</option>
<option value="United Kingdom">United Kingdom</option>
<option value="Australia">Australia</option>
</select><br><br>
<label>Interests:</label><br>
<input type="checkbox" name="interests[]" value="Technology">
Technology<br>
<input type="checkbox" name="interests[]" value="Sports"> Sports<br>
<input type="checkbox" name="interests[]" value="Music"> Music<br>
<input type="checkbox" name="interests[]" value="Travel"> Travel<br><br>
</body>
</html>
2. Display Page (e.g., display_registration.php):
<!DOCTYPE html>
<html>
<head>
<title>Registration Details</title>
</head>
<body>
<h2>Registration Details</h2>
<?php
// Retrieve and sanitize form data
$username = htmlspecialchars($_POST['username']);
$email = htmlspecialchars($_POST['email']);
$password = htmlspecialchars($_POST['password']);
$gender = htmlspecialchars($_POST['gender']);
$country = htmlspecialchars($_POST['country']);
$interests = isset($_POST['interests']) ? $_POST['interests'] : [];
</body>
</html>
7.Write PHP script to demonstrate passing variables with cookies.
1. Set the Cookie (e.g., set_cookie.php):
<?php
// Set a cookie named "user" with the value "John Doe" and an expiration time
of 1 hour
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + 3600, "/"); // 3600 seconds =
1 hour
USE test_db;
<label for="age">Age:</label>
<input type="number" name="age" required><br><br>
<label for="email">Email:</label>
<input type="email" name="email" required><br><br>
</body>
</html>
3. Insert Data into the Table (e.g., insert_student.php):
<?php
// Database connection parameters
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test_db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
USE test_db;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($result->num_rows > 0) {
$student = $result->fetch_assoc();
} else {
echo "No record found.";
exit;
}
} else {
echo "No ID specified.";
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Edit Student</title>
</head>
<body>
<h2>Edit Student</h2>
<label for="name">Name:</label>
<input type="text" name="name" value="<?php echo
htmlspecialchars($student['name']); ?>" required><br><br>
<label for="age">Age:</label>
<input type="number" name="age" value="<?php echo $student['age']; ?>"
required><br><br>
<label for="email">Email:</label>
<input type="email" name="email" value="<?php echo
htmlspecialchars($student['email']); ?>" required><br><br>
</body>
</html>
<?php
// Close the connection
$conn->close();
?>
3. Update the Record in the Database (e.g., update_student.php):
<?php
// Database connection parameters
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test_db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
9.Write a program to read customer information like cust-no, cust-name, item- purchased, and mob-no,
from customer table and display all these information in table format on output screen.
1. Database Setup
USE test_db;
);
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test_db";
// Create connection
// Check connection
if ($conn->connect_error) {
$result = $conn->query($sql);
if ($result->num_rows > 0) {
<tr>
<th>Customer No</th>
<th>Customer Name</th>
<th>Item Purchased</th>
<th>Mobile No</th>
</tr>";
echo "<tr>
</tr>";
echo "</table>";
} else {
$conn->close();
?>
10.Write a program to read employee information like emp-no, emp-name, designation and salary from
EMP table and display all this information using table format in your website.
1. Database Setup
USE test_db;
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test_db";
// Create connection
// Check connection
if ($conn->connect_error) {
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Start the HTML table
<tr>
<th>Employee No</th>
<th>Employee Name</th>
<th>Designation</th>
<th>Salary</th>
</tr>";
echo "<tr>
</tr>";
echo "</table>";
} else {
$conn->close();
?>