0% found this document useful (0 votes)
43 views18 pages

Practical File 172

The document contains 15 questions related to PHP programming concepts. The questions cover topics like conditional statements, loops, functions, arrays, sessions, cookies, file handling, database connectivity, CRUD operations etc. Sample code is provided as an answer for each question to demonstrate the concept.

Uploaded by

Lagneshwar S
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)
43 views18 pages

Practical File 172

The document contains 15 questions related to PHP programming concepts. The questions cover topics like conditional statements, loops, functions, arrays, sessions, cookies, file handling, database connectivity, CRUD operations etc. Sample code is provided as an answer for each question to demonstrate the concept.

Uploaded by

Lagneshwar S
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/ 18

COMM-IT CAREER academy

(Affiliated GGSIP University)


FC-1 Sheikh Sarai,
Chirag Delhi – 110017.

Practical file
of
wbp lab
(BCA 172)

SUBMITTED BY submitted to
LAgneshwar.s mr.lekhraj prajapati
(PROFESSOR)

Q1. Write a program to show the usage of nested if


statement?
<?php
$current_day = date('D');

if ($current_day == "Mon") {
echo "The day is Monday";

$current_hour = date('H');

if ($current_hour >= 18) {


echo "It is night time";
}
}
Q2. Write a program to create menu driven program and
show the usage of switch case?
<?php
$num=20;
switch($num){
case 10:
echo("number is equals to 10");
break;
case 20:
echo("number is equal to 20");
break;
case 30:
echo("number is equal to 30");
break;
default:
echo("number is not equal to 10, 20 or 30");
}
?>

Q3. Write a program to perform all four types of sorting?

Q4. Write a program to show the application of user


defined functions?
Q5. Write a program to show the usage of cookie?
<?php
setcookie("user", "Sonoo");
?>
<html>
<body>
<?php
if(!isset($_COOKIE["user"])) {
echo "Sorry, cookie is not found!";
} else {
echo "<br/>Cookie Value: " . $_COOKIE["user"];
}
?>
</body>
</html>
Q6. Write a program to show the usage of session?
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
?>

</body>
</html>
Q7. Write a program to implement oops concepts?
<?php
class demo
{
private $a= "hello javatpoint";
public function display()
{
echo $this->a;
}
}
$obj = new demo();
$obj->display();
?>
Q8. Write a program to create file?
<?php

$numbers = [1, 2, 3, 4, 5];


$filename = 'numbers.dat';

$f = fopen($filename, 'wb');
if (!$f) {
die('Error creating the file ' . $filename);
}

foreach ($numbers as $number) {


fputs($f, $number);
}
fclose($f);
Q9. Write a program to create a table and insert few
records into it using form?
<?php
$user = 'root';
$pass = '';
$db = 'sample tutorial';
//Replace 'sample tutorial' with the name of your database

$con = mysqli_connect("localhost", $user, $pass, $db);

if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);

$CarID = $_POST['CarID'];
$BrandName = $_POST['BrandName'];
$OwnersName = $_POST['OwnersName'];
$RegistrationNumber = $_POST['RegistrationNumber'];

$sql = "INSERT INTO parkinglot1


(CarID,BrandName,OwnersName, RegistrationNumber)
VALUES ('$CarID','$BrandName','$OwnersName',
'$RegistrationNumber')";
mysqli_query($con, $sql);

header("Location: ../form.php"); // Return to where our form is


stored
?>
Q10. Write a program to select all the records and display
in it table?
<?php
//including the database connection file
include("mysql_connect.php");
mysql_select_db("employee");
$result = mysql_query("SELECT * FROM employee_record");
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Position</th>
<th>Age</th>
<th>Salary</th>
<th>Email</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['f_name']."</td>";
echo "<td>".$row['l_name']."</td>";
echo "<td>".$row['position']."</td>";
echo "<td>".$row['age']."</td>";
echo "<td>".$row['salary']."</td>";
echo "<td>".$row['email']."</td>";
echo "</tr>";
}
echo "</table>";
?>

Q11. Write a program to create a MySQL database?


<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Create database
$sql = "CREATE DATABASE myDB";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}

$conn->close();
?>
Q12. Design a form which upload and display image in
php?
<!DOCTYPE html>
<html>
<head>
<title>Image Upload And Display</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
<label>Title</label>
<input type="text" name="title">
<label>File Upload</label>
<input type="File" name="file">
<input type="submit" name="submit">
<input type="submit" name="display" value="Display">
</form>
<table>
<tr>
<th>name</th>
<th>image</th>
</tr>
<?php
$localhost = "localhost"; #localhost
$dbusername = "root"; #username of phpmyadmin
$dbpassword = ""; #password of phpmyadmin
$dbname = "dbase"; #database name
#connection string
$conn = mysqli_connect($localhost,$dbusername,$dbpassword,
$dbname);
if (isset($_POST["submit"]))
{
#retrieve file title
$title = $_POST["title"];
#file name with a random number so that similar dont get replaced
$pname = rand(1000,10000)."-".$_FILES["file"]["name"];
#temporary file name to store file
$tname = $_FILES["file"]["tmp_name"];
#upload directory path
$uploads_dir = 'img';
#TO move the uploaded file to specific location
move_uploaded_file($tname, $uploads_dir.'/'.$pname);
#sql query to insert into database
$sql = "INSERT into fileup(title,image)
VALUES('$title','$pname')";
if(mysqli_query($conn,$sql)){
echo "File Sucessfully uploaded";
}
else{
echo "Error";
}
}if( (isset($_POST["display"]))){
$sql = "SELECT * FROM fileup";
$q=mysqli_query($conn,$sql);
while($row=mysqli_fetch_array($q)){
?>
<tr>
<td><?php echo $row['title'];?></td>
<td><img src="img/<?php echo $row["image"];?>"
height="100" width="100"></td>
</tr>
<?php }}
?>
</table>
</body>
</html>
Q13. Create admin login, logout form using session
variables?
Form.php
<!DOCTYPE html>
<html>
<body>

<form method="post" action="login.php">


User Id: <input type="text" name="userid"><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Login">
</form>

</body>
</html>

Login.PHP

<!DOCTYPE html>
<html>
<body>

<?php
$uid = $_POST['userid'];
$pw = $_POST['password'];

if($uid == 'ben' and $pw == 'ben23')


{
session_start();
$_SESSION['sid']=session_id();
echo "Logged in successfully";
}
?>

</body>
</html>

Logout.PHP

<!DOCTYPE html>
<html>
<body>

<?php

echo "Logged out successfully";

session_start();
session_destroy();

?>

</body>
</html>

Q14.Do form handling in PHP design a personal


information form ,then submit and retrieve the form date
using $GET(),$POST(),and $REQUEST() variables?
<!DOCTYPE html>
<html>
<head>
<title>POST Body</title>
<style>
form {
margin: 30px 0px;
}
input {
display: block;
margin: 10px 15px;
padding: 8px 10px;
font-size: 16px;
}
div {
font-size: 20px;
margin: 0px 15px;
}
h2 {
color: green;
margin: 20px 15px;
}
</style>
</head>
<body>
<h2>GeeksforGeeks</h2>
<form method="post">
<input type="text" name="username"
placeholder="Enter Username">
<input type="password" name="password"
placeholder="Enter
Password">
<input type="submit" name="submit-btn"
value="submit">
</form>
<br>
</body>
</html>

<?php
if( $_GET["name"] || $_GET["age"] ) {
echo "Welcome ". $_GET['name']. "<br />";
echo "You are ". $_GET['age']. " years old.";

exit();
}
?>
<html>
<body>

<form action = "<?php $_PHP_SELF ?>" method = "GET">


Name: <input type = "text" name = "name" />
Age: <input type = "text" name = "age" />
<input type = "submit" />
</form>

</body>
</html>

Q15. Write a program to modify a table?


<?php
$link = mysqli_connect("localhost", "root", "", "Mydb");

if($link === false){


die("ERROR: Could not connect. "
. mysqli_connect_error());
}

$sql = "UPDATE data SET Age='28' WHERE id=201";


if(mysqli_query($link, $sql)){
echo "Record was updated successfully.";
} else {
echo "ERROR: Could not able to execute $sql. "
. mysqli_error($link);
}
mysqli_close($link);
?>

You might also like