0% found this document useful (0 votes)
27 views14 pages

Ronak PHP Set 5

Uploaded by

hetmp2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views14 pages

Ronak PHP Set 5

Uploaded by

hetmp2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

1

Practical SET 05
Practical:-01
AIM:-Write a PHP script to connect to the MySQL server from your
website.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
OUTPUT:-

Practical:-02
AIM:-Write a program to read customer information like cust_no,
cust_name, Item_purchase, and mob_no, from 4 customer tables and
display all this information in table format on the output screen.

1
Name:- Parmar Ronak Dineshbhaii
Enrollno:- 22291371044
2

<html>
<body>
<form action="pr5_2.php" method="POST">
<label for="cust_no">cust_no</label>
<input type="text" id="cust_no" name="cust_no"><br><br>
<label for="cust_name">cust_name</label>
<input type="text" id="cust_name" name="cust_name"><br><br>
<label for="item">item</label>
<input type="text" id="item" name="item"><br><br>
<label for="mob_no">mob_no</label>
<input type="number" id="mob_no" name="mob_no"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

Second Program:-
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "php";
$conn = new mysqli($servername, $username, $password, $database);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$cust_no = $_REQUEST['cust_no'];

2
Name:- Parmar Ronak Dineshbhaii
Enrollno:- 22291371044
3

$cust_name = $_REQUEST['cust_name'];
$item = $_REQUEST['item'];
$mob_no = $_REQUEST['mob_no'];
if (!mysqli_select_db($conn, $database)) {
die("Database selection failed: " . mysqli_error($conn));
}
$sql = "INSERT INTO customer VALUES
('$cust_no','$cust_name','$item','$mob_no')";
if(mysqli_query($conn,$sql)){
echo "<h3>data stored in a database successfully."
. " Please browse your localhost php my admin"
. " to view the updated data</h3>";
echo nl2br("\n$cust_no\n $cust_name\n "."$item\n $mob_no");
} else{
echo "ERROR: Hush! Sorry $sql.". mysqli_error($conn);
}
// Close connection
mysqli_close($conn);
?>
OUTPUT:-

3
Name:- Parmar Ronak Dineshbhaii
Enrollno:- 22291371044
4

4
Name:- Parmar Ronak Dineshbhaii
Enrollno:- 22291371044
5

Practical:-03
AIM:-Write a program to edit the name of the customer to “Bob”
with cust_no =1, and to delete records with cust_no=3.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "php";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Select the database
if (!mysqli_select_db($conn, $database)) {
die("Database selection failed: " . mysqli_error($conn));
}
// Edit the name of the customer with cust_no = 1 to "Bob"
$sql_edit = "UPDATE customer SET cust_name = 'Bob' WHERE cust_no = 1";
if (mysqli_query($conn, $sql_edit)) {
echo "Name updated successfully<br>";
} else {
echo "Error updating name: " . mysqli_error($conn) . "<br>";
}
// Delete the record with cust_no = 3
$sql_delete = "DELETE FROM customer WHERE cust_no = 3";

5
Name:- Parmar Ronak Dineshbhaii
Enrollno:- 22291371044
6

if (mysqli_query($conn, $sql_delete)) {
echo "Record deleted successfully<br>";
} else {
echo "Error deleting record: " . mysqli_error($conn) . "<br>";
}
// Close connection
mysqli_close($conn);
?>
OUTPUT:-

Practical:-04
AIM:- Write a program to read employee information like emp_no,
emp_name, designation and salary from the EMP table and display all
this information using table format.

<html>
<body>

6
Name:- Parmar Ronak Dineshbhaii
Enrollno:- 22291371044
7

<form action="pr5_4_2.php" method="POST">


<label for="emp_no">emp_no</label>
<input type="text" id="emp_no" name="emp_no"><br><br>
<label for="emp_name">emp_name</label>
<input type="text" id="emp_name" name="emp_name"><br><br>
<label for="salary">salary</label>
<input type="number" id="salary" name="salary"><br><br>
<label for="mob_no">mob_no</label>
<input type="number" id="mob_no" name="mob_no"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

Second Program:-
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "php";
$conn = new mysqli($servername, $username, $password, $database);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$emp_no = $_REQUEST['emp_no'];
$emp_name = $_REQUEST['emp_name'];
$salary = $_REQUEST['salary'];

7
Name:- Parmar Ronak Dineshbhaii
Enrollno:- 22291371044
8

$mob_no = $_REQUEST['mob_no'];
if (!mysqli_select_db($conn, $database)) {
die("Database selection failed: " . mysqli_error($conn));
}
$sql = "INSERT INTO employee VALUES
('$emp_no','$emp_name','$salary','$mob_no')";
if(mysqli_query($conn,$sql)){
echo "<h3>data stored in a database successfully."
. " Please browse your localhost php my admin"
. " to view the updated data</h3>";
echo nl2br("\n$emp_no\n $emp_name\n "."$salary\n $mob_no");
} else{
echo "ERROR: Hush! Sorry $sql.". mysqli_error($conn);
}
// Close connection
mysqli_close($conn);
?>

Third Program:-
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "php";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);
// Check connection
if (!$conn) {

8
Name:- Parmar Ronak Dineshbhaii
Enrollno:- 22291371044
9

die("Connection failed: " . mysqli_connect_error());


}
// SQL query to retrieve data
$sql = "SELECT * FROM employee";
$result = mysqli_query($conn, $sql);
?>
<!DOCTYPE html>
<html>
<head>
<title>Database Table</title>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<h2>employee data</h2>

9
Name:- Parmar Ronak Dineshbhaii
Enrollno:- 22291371044
10

<table>
<tr>
<th>emp_no</th>
<th>emp_name</th>
<th>salary</th>
<th>mob_no</th>
</tr>
<?php
// Output data of each row
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['emp_no'] . "</td>";
echo "<td>" . $row['emp_name'] . "</td>";
echo "<td>" . $row['salary'] . "</td>";
echo "<td>" . $row['mob_no'] . "</td>";
echo "</tr>";
}
?>
</table>
</body>
</html>
<?php
// Close connection
mysqli_close($conn);
?>
OUTPUT:-

10
Name:- Parmar Ronak Dineshbhaii
Enrollno:- 22291371044
11

Practical:-05
11
Name:- Parmar Ronak Dineshbhaii
Enrollno:- 22291371044
12

AIM:- Create a dynamic web site using PHP and MySQL


<html>
<body>
<form action="pr5_5_1.php" method="POST">
<label for="stud_no">stud_no</label>
<input type="text" id="stud_no" name="stud_no"><br><br>
<label for="stud_name">stud_name</label>
<input type="text" id="stud_name" name="stud_name"><br><br>
<label for="city">city</label>
<input type="text" id="city" name="city"><br><br>
<label for="mob_no">mob_no</label>
<input type="number" id="mob_no" name="mob_no"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

Second Program:-
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "php";
$conn = new mysqli($servername, $username, $password, $database);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
12
Name:- Parmar Ronak Dineshbhaii
Enrollno:- 22291371044
13

$stud_no = $_REQUEST['stud_no'];
$stud_name = $_REQUEST['stud_name'];
$city = $_REQUEST['city'];
$mob_no = $_REQUEST['mob_no'];
if (!mysqli_select_db($conn, $database)) {
die("Database selection failed: " . mysqli_error($conn));
}
$sql = "INSERT INTO student VALUES
('$stud_no','$stud_name','$city','$mob_no')";
if(mysqli_query($conn,$sql)){
echo "<h3>data stored in a database successfully."
. " Please browse your localhost php my admin"
. " to view the updated data</h3>";
//echo nl2br("\n$cust_no\n $cust_name\n "."$item\n $mob_no");
} else{
echo "ERROR: Hush! Sorry $sql.". mysqli_error($conn);
}
// Close connection
mysqli_close($conn);
?>
OUTPUT:-

13
Name:- Parmar Ronak Dineshbhaii
Enrollno:- 22291371044
14

*________*

Sign-________

14
Name:- Parmar Ronak Dineshbhaii
Enrollno:- 22291371044

You might also like