ASSIGNMENT-2
Q.1 Differentiate GET and POST. Create
an Application form and access the
values in PHP using POST
method.(Use Textbox-Name, Integer
box-Age, Check boxes-Hobbies, Radio
button-Gender, Button- Submit &
Reset).
1.
GET Method:
2.
Data is sent in the URL query string.
Limited amount of data can be sent (typically up to 2048 characters).
Data is visible to the user in the URL.
Suitable for non-sensitive data.
Used for retrieving data from the server.
3.
POST Method:
4.
Data is sent in the HTTP request body.
Larger amount of data can be sent.
Data is not visible to the user in the URL.
Suitable for sensitive data.
Used for submitting data to the server.
<!DOCTYPE html>
<html>
<head>
<title>Application Form</title>
</head>
<body>
<h2>Application Form</h2>
<form method="post" action="process_form.php">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name"><br>
<label for="age">Age:</label><br>
<input type="number" id="age" name="age"><br>
<label for="hobbies">Hobbies:</label><br>
<input type="checkbox" id="hobby1" name="hobbies[]"
value="Reading">Reading<br>
<input type="checkbox" id="hobby2" name="hobbies[]"
value="Cooking">Cooking<br>
<input type="checkbox" id="hobby3" name="hobbies[]"
value="Sports">Sports<br>
<label for="gender">Gender:</label><br>
<input type="radio" id="male" name="gender" value="male">Male<br>
<input type="radio" id="female" name="gender" value="female">Female<br>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form>
</body>
</html>
Now, in PHP (in a file named process_form.php), you can access the
values using the $_POST array:
<!DOCTYPE html>
<html>
<head>
<title>Processed Form</title>
</head>
<body>
<?php
// Retrieving values from the form
$name = $_POST['name'];
$age = $_POST['age'];
$hobbies = isset($_POST['hobbies']) ? $_POST['hobbies'] : array();
$gender = $_POST['gender'];
// Displaying submitted values
echo "<h2>Submitted Information</h2>";
echo "Name: " . $name . "<br>";
echo "Age: " . $age . "<br>";
echo "Hobbies: " . implode(", ", $hobbies) . "<br>";
echo "Gender: " . $gender . "<br>";
?>
</body>
</html>
This PHP code retrieves the values from the form submitted using the POST
method and displays them on a new page.
Q.2 What are commonly used PHP
file management functions? Explain
PHP provides several file management functions to work with files and directories.
Here are some commonly used ones:
1.
File Open/Close:
2.
fopen(): Opens a file or URL.
fclose(): Closes an open file pointer.
3.
File Read/Write:
4.
fgets(): Reads a line from an open file.
fread(): Reads from an open file.
fwrite(): Writes to an open file.
file_get_contents(): Reads entire file into a string.
5.
File Delete/Move:
6.
unlink(): Deletes a file.
rename(): Renames a file or directory.
7.
File Information:
8.
filesize(): Gets the file size.
file_exists(): Checks whether a file or directory exists.
is_file(): Checks whether the path is a regular file.
9.
File Permissions:
10.
chmod(): Changes file mode (permissions).
fileperms(): Gets file permissions.
11.
File Path:
12.
basename(): Returns filename component of path.
dirname(): Returns directory name component of path.
13.
Directory Management:
14.
opendir(): Opens a directory handle.
readdir(): Reads entry from directory handle.
closedir(): Closes directory handle.
mkdir(): Creates a directory.
rmdir(): Removes a directory.
15.
File Access:
16.
is_readable(): Checks whether a file is readable.
is_writable(): Checks whether a file is writable.
is_executable(): Checks whether a file is executable.
For example, you can use fopen() to open a file, fwrite() to write data to it, and
fclose() to close it. Similarly, you can use unlink() to delete a file, rename() to
rename it, and so on.
These functions enable you to perform various file operations like reading, writing,
deleting, moving, and managing files and directories in PHP.
Q.3 Write a PHP script to create a file
and read the contents of the file.
<?php
// File path
$file_path = "[Link]";
// Content to write to the file
$content = "Hello, this is some content written to the file.";
// Open the file for writing
$file_handle = fopen($file_path, "w");
// Check if file opened successfully
if ($file_handle === false) {
die("Failed to open file for writing.");
}
// Write content to the file
if (fwrite($file_handle, $content) === false) {
die("Failed to write to file.");
}
// Close the file handle
fclose($file_handle);
// Open the file for reading
$file_handle = fopen($file_path, "r");
// Check if file opened successfully
if ($file_handle === false) {
die("Failed to open file for reading.");
}
// Read content from the file
$file_content = fread($file_handle, filesize($file_path));
// Close the file handle
fclose($file_handle);
// Display the content
echo "Content of the file:<br>";
echo $file_content;
?>
This script does the following:
Q.4 What is MYSQL? What are the
different data types in MYSQL? In how
many ways we can retrieve the data in
the result set of MYSQL in PHP. Also
differentiate mysql_fetch_object and
mysql_fetch_array.
1.
Numeric Types:
2.
INT
TINYINT
SMALLINT
MEDIUMINT
BIGINT
FLOAT
DOUBLE
DECIMAL
3.
Date and Time Types:
4.
DATE
TIME
DATETIME
TIMESTAMP
YEAR
5.
String Types:
6.
CHAR
VARCHAR
BINARY
VARBINARY
BLOB
TEXT
7.
Other Types:
8.
ENUM
SET
JSON
mysql_fetch_object(): It fetches a result row as an object. Each column value is assigned to a
property of the object with the same name as the column. For example:
$row = mysql_fetch_object($result);
echo $row->column_name;
mysql_fetch_array(): It fetches a result row as an associative array, a numeric array, or both (by
default, it returns both). Each column value can be accessed using the column name or the column index.
For example:
$row = mysql_fetch_array($result);
echo $row['column_name'];
// or
echo $row[0]; // if the column index is 0
mysql_fetch_object() returns a row as an object, while mysql_fetch_array() returns a row as an
array.
Q.5 Write a PHP script to insert records
of employee table through Form and
display records in table format.
<!DOCTYPE html>
<html>
<head>
<title>Employee Records</title>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
th {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<h2>Add Employee Record</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name"><br>
<label for="designation">Designation:</label><br>
<input type="text" id="designation" name="designation"><br>
<input type="submit" value="Submit">
</form>
<?php
// Database connection
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Insert record if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
$designation = $_POST["designation"];
// SQL to insert record
$sql = "INSERT INTO employee (name, designation) VALUES ('$name', '$designation')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
// Display records
$sql = "SELECT * FROM employee";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<h2>Employee Records</h2>";
echo "<table>";
echo "<tr><th>ID</th><th>Name</th><th>Designation</th></tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["id"] . "</td><td>" . $row["name"] . "</td><td>" . $row["designation"] .
"</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
</body>
</html>
Q.6 Write a PHP script to create a table to
store Student details (Enrollment No,
Name, DOB). Write code to insert records
in it through form and display records in
table format.
<!DOCTYPE html>
<html>
<head>
<title>Student Records</title>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
th {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<h2>Add Student Record</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);
?>">
<label for="enrollment_no">Enrollment No:</label><br>
<input type="text" id="enrollment_no" name="enrollment_no"><br>
<label for="name">Name:</label><br>
<input type="text" id="name" name="name"><br>
<label for="dob">Date of Birth:</label><br>
<input type="date" id="dob" name="dob"><br>
<input type="submit" value="Submit">
</form>
<?php
// Database connection
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Insert record if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$enrollment_no = $_POST["enrollment_no"];
$name = $_POST["name"];
$dob = $_POST["dob"];
// SQL to insert record
$sql = "INSERT INTO students (enrollment_no, name, dob) VALUES ('$enrollment_no',
'$name', '$dob')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
// Display records
$sql = "SELECT * FROM students";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<h2>Student Records</h2>";
echo "<table>";
echo "<tr><th>Enrollment No</th><th>Name</th><th>Date of Birth</th></tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["enrollment_no"] . "</td><td>" . $row["name"] . "</td><td>" .
$row["dob"] . "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
</body>
</html>
Q.7 Write the steps to create a table
using MYSQL through Myadmin.
CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
age INT,
email VARCHAR(100)
);