Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
889 views

Web Technology Lab Report 4

The document outlines code to create a database and table, insert data, select and display data, update data, and delete data from a MySQL database using PHP. The code connects to a MySQL database, creates a database and table, inserts form data, selects and displays table data, updates data by changing first and last names, and deletes a record by ID. The code demonstrates basic CRUD operations on a MySQL database using PHP.

Uploaded by

Aaneill Crest
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
889 views

Web Technology Lab Report 4

The document outlines code to create a database and table, insert data, select and display data, update data, and delete data from a MySQL database using PHP. The code connects to a MySQL database, creates a database and table, inserts form data, selects and displays table data, updates data by changing first and last names, and deletes a record by ID. The code demonstrates basic CRUD operations on a MySQL database using PHP.

Uploaded by

Aaneill Crest
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Web Technology Lab Report 4

Submitted By:
Ayush Manandhar
BCA 3rd Semester
Roll no: 7
Create a database.
<?php
$server_name = "localhost";
$username="root";
$password="";
$connection =mysqli_connect($server_name, $username, $password);

if($connection){
echo "Connection established";
$sql_db="CREATE DATABASE `bcaRecordNew`";
if(mysqli_query($connection, $sql_db)){
echo"DataBase Created successfully";
}}
else{
echo"connection not established";
}
mysqli_close($connection);
?>
Create a table in database.
<?php
$server_name="localhost";
$username="root";
$password="";
$db_name="bcaRecordNew";
$connection = mysqli_connect($server_name, $username, $password, $db_name);
if($connection){
$sql="CREATE TABLE students (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(30) NOT NULL,
email VARCHAR(50),
)";
if(mysqli_query($connection, $sql)){
echo "Table created";
}
else{
echo mysqli_error($connection);
echo "Not our concern";
}
}
else{
echo "Connection not established";
}
mysqli_close($connection);
?>
Create a form to input data in database.
<?php
if($_POST){
$fname = $_POST['firstname'];
$lname = $_POST['lastname'];
$email = $_POST['email'];

$servername = "localhost";
$username = "root";
$password = "";
$db_name = "bcaRecordNew";

$connection = mysqli_connect($servername, $username, $password, $db_name);


if($connection){
$sql = "INSERT INTO students (first_name, last_name, email)
VALUES ('$fname', '$lname', '$email')";
if(mysqli_query($connection, $sql)){
echo "New data inserted successfully";
}
else{
echo "error".mysqli_error($connection);
}

}
else{
echo "Connection not established";
}
}
?>
<html>
<head>
<title>CV Form</title>
</head>
<body>
<h1>STUDENT FORM</h1>
<form method="POST" >
<input type="text" placeholder="Firstname" name="firstname"><br><br>
<input type="text" placeholder="Lastname" name="lastname"><br><br>
<input type="text" placeholder="Email" name="email"><br><br>
<button>Submit</button>
</form>

</body>
</html>
Select and display a data from database.
<?php
$server_name="localhost";
$username="root";
$password="";
$db_name="bcaRecordNew";
$connection = mysqli_connect($server_name, $username, $password, $db_name);
if($connection){
$sql="SELECT * FROM students";

$result=mysqli_query($connection, $sql);
if(mysqli_num_rows($result)){
while($row=mysqli_fetch_assoc($result)){
echo "id". $row["id"]. "-FirstName:" . $row["first_name"] . "<br>";
}
}
else{
echo mysqli_error($connection);
echo "Not our concern";
}
}
else{
echo "Connection not established";
}
?>
Update data in database.

<?php
$server_name="localhost";
$username="root";
$password="";
$db_name="bcarecordnew";
$connection = mysqli_connect($server_name, $username, $password, $db_name);
if($connection){
$sql="UPDATE `students` SET `first_name`='Abcd',`last_name`='Mnop' WHERE `id`=0";
if (mysqli_query($connection, $sql)) {
echo "Record updated successfully";
}
else{
echo "error";
}
}
else{
echo "Connection not established";
}
mysqli_close($connection);
?>
Update data in database.

<?php
$server_name="localhost";
$username="root";
$password="";
$db_name="bcarecordnew";
$connection = mysqli_connect($server_name, $username, $password, $db_name);
if($connection){
$sql="UPDATE `students` SET `first_name`='Abcd',`last_name`='Mnop' WHERE `id`=0";
if (mysqli_query($connection, $sql)) {
echo "Record updated successfully";
}
else{
echo "error";
}
}
else{
echo "Connection not established";
}
mysqli_close($connection);
?>
Delete data from database.

<?php
$server_name="localhost";
$username="root";
$password="";
$db_name="bcarecordnew";
$connection = mysqli_connect($server_name, $username, $password, $db_name);
if($connection){
$sql="DELETE FROM `students` WHERE `id`=0";
if (mysqli_query($connection, $sql)) {
echo "Record deleted successfully";
}
else{
echo "error";
}
}
else{
echo "Connection not established";
}
mysqli_close($connection);
?>

You might also like