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

PHP Ut2 QB

The document contains 10 code examples demonstrating various PHP concepts: 1) Creating a class with a constructor 2) Creating a form with multiple submit buttons 3) Creating a calculator form with addition and subtraction buttons 4) Validating an email field with a regular expression 5) Creating, modifying, and deleting cookies 6) Starting and destroying sessions 7) Updating records in a database 8) Fetching data from a database 9) Inserting data into a database table 10) Deleting records from a database table

Uploaded by

Purva Jage
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 views13 pages

PHP Ut2 QB

The document contains 10 code examples demonstrating various PHP concepts: 1) Creating a class with a constructor 2) Creating a form with multiple submit buttons 3) Creating a calculator form with addition and subtraction buttons 4) Validating an email field with a regular expression 5) Creating, modifying, and deleting cookies 6) Starting and destroying sessions 7) Updating records in a database 8) Fetching data from a database 9) Inserting data into a database table 10) Deleting records from a database table

Uploaded by

Purva Jage
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/ 13

Php ut2 qb

1] <?php

//PHP program to demonstrate the parameterized constructor.

class emp

private $fname;

private $lname;

public function __construct($fname, $lname)

echo "initializing object..<br>";

$this->fname=$fname;

$this->lname=$lname;

public function showname()

echo "first name: ".$this->fname."<br>";

echo "last name: ".$this->lname."<br>";

$S = new emp("raj","patil");

$S->showname();

?>

Output :

2] multipleform.html:

<!DOCTYPE html>

<html>
<head>

<title>Multiple Forms Example</title>

</head>

<body>

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

<label for="name">Name:</label>

<input type="text" name="name" id="name">

<br>

<label for="email">Email:</label>

<input type="email" name="email" id="email">

<br>

<button type="submit" name="form1_submit">Submit</button>

</form>

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

<label for="message">Message:</label>

<textarea name="message" id="message"></textarea>

<br>

<button type="submit" name="form2_submit">Submit</button>

</form>

</body>

</html>

Multipleform.php :
<?php

// Form 1

if(isset($_POST['form1_submit'])) {

$name = $_POST['name'];

$email = $_POST['email'];

// Process data and display message

echo "<p><b>Thank you for submitting Form 1, $name ($email)!</b></p>";

// Form 2
if(isset($_POST['form2_submit'])) {

$message = $_POST['message'];

// Process data and display message

echo "<p><b>Thank you for submitting Form 2, your message is: $message</b></p>";

?>

Output :

3] multiplebutton.html:
<!DOCTYPE html>

<html>

<head>

<title>Calculator</title>

</head>

<body>

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

<label for="num1">First Number:</label>

<input type="number" id="num1" name="num1"><br><br>

<label for="num2">Second Number:</label>

<input type="number" id="num2" name="num2"><br><br>

<button type="submit" name="add">Addition</button>

<button type="submit" name="sub">Subtraction</button>


</form>

</body>

</html>

Multiplebutton.php:
<?php

if($_SERVER["REQUEST_METHOD"] == "POST"){

$num1 = $_POST['num1'];

$num2 = $_POST['num2'];

if(isset($_POST['add'])){

$result = $num1 + $num2;

echo "<p>addition of these two number is = $result</p>";

elseif(isset($_POST['sub'])){

$result = $num1 - $num2;

echo "<p>subtraction of these two number is = $result</p>";

?>

Output :

4] validation.html:
<html>

<head>

<title>email validation</title>

</head>

<body>

<h1>EMAIL-ID VALIDATION </h1>

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

Name:<input type="text" name="name" id="name"/><br/>

Mobile no:<input type="text" name="number" id="mobileno"/><br/>

Email id : <input type="text" name="email" id="email"/><br>

<input type="submit" name="submit_btn" value="submit"/>

</form>

</body>

</html>

Validation.php :
<?php

if($_SERVER['REQUEST_METHOD']==='POST'){

if($_SERVER ['REQUEST_METHOD']==='POST'){

if(empty($_POST['name']))

echo "name can`t be blank<br>";

if(is_numeric($_POST['number']))

echo "enter valid mobile number<br>";

$pattern="/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/";

if(preg_match($pattern,$_POST['email']))

echo "sucessfull<br>";

else{

echo "enter valid email-id";


}

?>

Output:

5] <?php
// Create a cookie
$cookie_name = "user";
$cookie_value = "admin123@";
$cookie_expiration = time() + (86400 * 30); // 30 days
setcookie($cookie_name, $cookie_value, $cookie_expiration, "/"); // The last parameter "/" means that the
cookie is available in the entire website
echo "cookie name :$cookie_name <br>";
echo "cookie value :$cookie_value <br>";
// Modify a cookie
if(isset($_COOKIE[$cookie_name])){
$cookie_value = "admin456@";
setcookie($cookie_name, $cookie_value, $cookie_expiration, "/");
echo "modifying cookie : <br> ";
echo "cookie name : $cookie_name <br>";
echo "cookie value : $cookie_value <br>";
}
// Delete a cookie
if(isset($_COOKIE[$cookie_name])){
setcookie($cookie_name, "", time() - 3600, "/");
echo "The cookie " . $cookie_name . " has been deleted.";
}
?>
Output :

6] <?php
// Start the session
session_start();

// Set session variables


$_SESSION["username"] = "abc";
$_SESSION["email"] = "[email protected]";

// Print the session variables


echo "session started...<br>";
echo "Session variables are set.<br>";
echo "Username is : " . $_SESSION["username"] . "<br>";
echo "Email is : " . $_SESSION["email"] . "<br>";

// Destroy the session


session_unset();
session_destroy();
echo "Session destroyed....<br>";
?>
Output :

7] updating records:
<?php
// Connecting to the Database
$servername = "localhost";
$username = "root";
$password = "abc6263@";
$database = "student";
// Create a connection
$conn = mysqli_connect($servername, $username, $password, $database);
// Die if connection was not successful
if (!$conn){
die("Sorry we failed to connect: ". mysqli_connect_error());
}
else{
echo "Connection was successful<br>";
}

$sql="UPDATE stud SET name='raj patil' WHERE rollno=9578";


if(mysqli_query($conn,$sql)){
echo "record updated sucessfully<br>";
}
else{
echo "could not update record;".mysqli_error($conn);
}
mysqli_close($conn);
?>
Output :

8] fetch data :
<?php
$host='localhost';
$user='root';
$pass='abc6263@';
$dbname='employee_db';
$conn=mysqli_connect($host,$user,$pass,$dbname);
if(!$conn){
die('could not connect:'.mysqli_connect_error());
}
echo "connected sucessfully..";
$sql='select *from employee';
$retval=mysqli_query($conn,$sql);
if(mysqli_num_rows($retval) > 0){
while($row=mysqli_fetch_assoc($retval)){
echo "name:{$row['NAME']}<BR>";
echo "age:{$row['AGE']}<BR>";
echo "salary:{$row['SALARY']}<BR>";
echo"-------------------------<br>";
}
}
else
{
echo "0 results";
}
mysqli_close($conn);
?>
OUTPUT :

9] inserting into student record :

<?php
// Define the database connection details
$servername = "localhost"; // Replace with your database server name
$username = "root"; // Replace with your MySQL username
$password = "abc6263@"; // Replace with your MySQL password
$dbname = "student"; // Replace with your MySQL database name

// Create a connection to the MySQL database


$conn = new mysqli($servername, $username, $password, $dbname);

// Check the connection for errors


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

// Prepare the SQL statement to insert a new record


$sql = "INSERT INTO stud1 (roll_no, name, city, mobile_no) VALUES ('9507', 'aditya jambhale', 'koparkhairane',
'345678912')";

// Execute the SQL statement


if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

// Close the database connection


$conn->close();
?>

Output :
10] delete records:
<?php
// MySQL database credentials
$host = 'localhost';
$username = 'root';
$password = 'abc6263@';
$dbname = 'student';

// Establishing connection to the MySQL database


$conn = mysqli_connect($host, $username, $password, $dbname);

// Checking the connection


if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

// SQL query to delete records from the table


$sql = "DELETE FROM stud1 WHERE roll_no='9506'";

// Execute the query


if (mysqli_query($conn, $sql)) {
echo "Records deleted successfully.";
} else {
echo "Error deleting records: " . mysqli_error($conn);
}

// Close the database connection


mysqli_close($conn);
?>
Output :

You might also like