Web-5 - 841
Web-5 - 841
XAVIER’S COLLEGE
MAITIGHAR, KATHMANDU, NEPAL
Phone: 01-5321365, 01-5344636
Email: [email protected]
WEB ASSIGNMENT- 5
II
1. Connect to server
CODE:
<html>
<body>
<?php
$host = "localhost";
$user = "root";
$password = "";
if(!$link){
die("Connection Failed: " .mysqli_connect_error());
}
echo "Connected Successfully";
echo "Program executed by Rejina Bhattarai";
?>
</body>
</html>
OUTPUT:
2. Create database
CODE:
<!DOCTYPE html>
<html>
<head> </head>
<body>
<?php
include "connect_server.php";
//create database
$sql = "CREATE DATABASE `group`";
//check sql code
if($link -> query($sql)===TRUE){
echo"<br> Database created succefully";
2
}
else{
echo " <br> Error creating Database: " .$link -> error;
}
?>
</body>
</html>
OUTPUT:
3. Create table
CODE:
<html>
<body>
<?php
include"connect_server.php";
//conneting to database
$sql = "USE `group`";
if($link -> query($sql) === TRUE){
echo"database 'group' selected successfully<br>";
}else{
echo "error selecting database: " .$link -> error;
}
//create table
$sql = "CREATE TABLE IF NOT EXISTS students(
id INT(7) AUTO_INCREMENT PRIMARY KEY,
Firstname VARCHAR(255) NOT NULL,
Lastname VARCHAR(255) NOT NULL,
Email VARCHAR(50)
)";
3
echo"<br>table created successfully";
}else{
echo "error creating table " .$link -> error;
}
?>
</body>
</html>
OUTPUT:
</table>
<input type="submit" value="submit">
</form>
</body>
</HTML>
4
OUTPUT:
PHP CODE:
<?php
include ("connect_server.php");
$fname = $_POST['fname'];
$lname = $_POST ['lname'];
$email = $_POST['email'];
5
{
echo("error in inserting data: ".$link -> error);
}
OUTPUT:
6
if ($result->num_rows > 0)
{
echo "<table border ='1'>
<tr> <th>id</th>
<th> Firstname</th>
<th>Lastname</th> <th>Email</th></tr>";
while($rows = $result-> fetch_assoc())
{
echo "<tr>";
echo "<td>".$rows['id']."</td>";
echo "<td>".$rows['Firstname']."</td>";
echo "<td>".$rows['Lastname']."</td>";
echo "<td>".$rows['Email']."</td>";
echo "<tr>";
}
echo "</table>";
}
$link-> close();
?>
OUTPUT:
</table>
</form>
</body>
</html>
Update1.php
<?php
include "connect_server.php";
$sql = "USE `group`";
if ($link -> query ($sql) === TRUE)
{
echo "database selected.";
}
$id = $_POST['id'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
8
set Firstname='$fname',
Lastname = '$lname',
Email = '$email' where id= $id;";
7. Delete record
(I accidently deleted the previous table so I made a new one.)
CODE:
<html>
<body>
<?php
include "display-data.php";
?>
<form action="" method="POST">
<table>
<tr><h3>enter the id of the record you want to delete</h3></tr>
<tr> <td>Id</td> <td><input type="text" name="id"></td>
<td><input type="submit" value="delete"></td></tr>
</table>
</form>
9
<?php
$host = "localhost";
$user = "root";
$password = "";
$dbname = "school";
if (!$link)
{
die ("connection failed: " .mysqli_connect_error());
}
?>
</body>
</html>
OUTPUT:
10
After deletion:
8. List out function of mysqli , it’s syntax and use in above programs.
Conclusion
In conclusion, the topics discussed highlight the fundamental steps for building dynamic, data-
driven web applications with PHP and MySQL. These include connecting to a server, creating
databases and tables, inserting data via forms, displaying it in a browser, and performing updates
or deletions. The MySQLi extension in PHP streamlines these processes with features like
prepared statements, robust error handling, and versatile data-fetching methods.
11