Ramsha Aftab 2021F-BCE-013
LAB NO# 12
Objective: Use Dreamweaver Web Development tool to Insert/Delete Record.
1) To understand the concept of inserting in and deleting records from database.
2) To insert record in MySQL database using PHP.
3) To delete record from MySQL database.
THEORY:
This lab is mainly concerned with the learning of the concept of Server Side Scripting in PHP and to
get aware of the concept of INSERTING & DELETING RECORDS in PHP. In this lab we are
actually implementing the functionality of insertion and deletion of records using PHP.
OBJECTIVE #1:
To understand the concept of inserting in and deleting records from database.
INSERTING RECORDS IN DATABASE:
The INSERT INTO statement is used to insert new records in a table. It is possible to write the
INSERT INTO statement in two forms. The first form does not specify the column names where the
data will be inserted, only their values. The second form specifies both the column names and the
values to be inserted.
DELETING RECORDS FROM DATABASE:
The DELETE statement is used to delete rows in a table. It is possible to delete all rows in a table
without deleting the table. This means that the table structure, attributes, and indexes will be intact.
OBJECTIVE #2:
To insert record in MySQL database using PHP
PHP code for inserting record is as follows:
Insert.php
<?php
if(!empty($_POST))
{$uname = $_POST['uname'];
$pwd = $_POST['pswd'];
$db_user = 'root';
$db_pass = "";
$db = 'tests';//test
$con = mysqli_connect('localhost',$db_user,$db_pass) or die('Unable to connect database');
mysqli_select_db($con,$db)or die("cannot connect database practice");
$sql="insert into lab10(uname,pwd) values('".$uname."','".$pwd."')";
$result=mysqli_query($con, $sql);
if($result)
Ramsha Aftab 2021F-BCE-013
{echo "You have successfully created new account";}
else{echo "Operation Failure please re-attempt";
}}
?>
<form id="insert_form" name="insert_form" method="post" action="">
<table width="285" border="1">
<tr>
<th colspan="3" scope="col"><em><strong>Insert User Record</strong></em></th>
</tr>
<tr>
<td width="78"><em><strong>Username</strong></em></td>
<td width="144"><em><strong>
<input type="text" name="uname" id="uname" />
</strong></em></td>
<td width="22"> </td>
</tr>
<tr>
<td><em><strong>Password</strong></em></td>
<td><em><strong>
<input type="password" name="pswd" id="pswd" />
</strong></em></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td><em><strong></strong></em></td>
<td><input type="submit" name="submit" id="submit" value="Submit" /></td>
</tr>
</table>
</form>
BROWSER OUTPUT:
Running it on the browser with the link
Ramsha Aftab 2021F-BCE-013
http://localhost/ramsha068/
Ramsha Aftab 2021F-BCE-013
PASSWORD HASH FUNCTION
Hashing password is a technique of converting a single password into another string called
hashed password. The hashed password is generally one-way, i.e. we can’t go to the
original password from the hashed password.
Objective # 3:
To delete record from MySQL database.
PHP code for deleting record is as follows:
Delete.php
<?php
session_start();
$uname = $_SESSION['usern'] ;
$db_user = 'root';
$db_pass = "";
$db = 'tests';//test
$conn = mysqli_connect('localhost',$db_user,$db_pass) or die('Unable to connect database');
mysqli_select_db($conn,$db)or die("cannot connect database practice");
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
$sql="Delete from lab10 where uname = '".$uname."' ";
$result=mysqli_query($conn,$sql);
if($result)
echo "You have successfully deleted your account";
else
echo "Operation Failure please re-attempt";
?>
BROWSER OUTPUT:
Running it on the browser with the link
http://localhost/ramsha068/
Ramsha Aftab 2021F-BCE-013
Exercise:
Design a login form using Dreamweaver, database using phpMyAdmin and add 2 more records
and delete first 3 records and show the resultant table.
The table is from: Add, Edit and Delete Record using PHP/MySQL | Free Source Code, Projects
& Tutorials (sourcecodester.com)
Ramsha Aftab 2021F-BCE-013
Ramsha Aftab 2021F-BCE-013
<?php
if(!empty($_POST)){
$Title = $_POST['Title'];
$Author = $_POST['Author'];
$Publisher_name = $_POST['Publisher_name'];
$Copyright_year = $_POST['Copyright_year'];
$db_user = 'root';
$db_pass = "";
$db = 'tests';//test
$con = mysqli_connect('localhost',$db_user,$db_pass) or die('Unable to connect database');
mysqli_select_db($con,$db)or die("cannot connect database practice");
$sql="insert into lab12(Title,Author,Publisher_name,Copyright_year) values('".
$Title."','".$Author."','".$Publisher_name."','".$Copyright_year."')";
$result=mysqli_query($con, $sql);
if($result)
{ echo "You have successfully created new record";
}else{
echo "Operation Failure please re-attempt";}}?>
<form id="insert_form" name="insert_form" method="post" action="">
<table width="285" border="1"><tr>
<th colspan="3" scope="col"> </th>
Ramsha Aftab 2021F-BCE-013
</tr><tr>
<td width="78"><em><strong>Title</strong></em></td>
<td colspan="2"><em><strong>
<input type="text" name="uname" id="uname" />
</strong></em></td></tr> <tr>
<td><em><strong>Author</strong></em></td>
<td colspan="2"><label for="author"></label>
<input type="text" name="author" id="author"></td>
</tr><tr>
<td><strong><em>Publisher_name</em></strong></td>
<td colspan="2"><label for="publisher_name"></label>
<input type="text" name="publisher_name" id="publisher_name"></td>
</tr> <tr>
<td><em><strong>Copyright_year</strong></em></td>
<td colspan="2"><em><strong>
<input type="Copyright_year" name="Cpyright_year" id="text" />
</strong></em></td>
</tr><tr>
<td colspan="3"><em><strong></strong></em> <input type="submit" name="submit"
id="submit" value="Submit" /></td>
</tr</table>
</form>
DELETION:
<?php
session_start();
$uname = $_SESSION['usern'] ;
$db_user = 'root';
Ramsha Aftab 2021F-BCE-013
$db_pass = "";
$db = 'tests';//test
$conn = mysqli_connect('localhost',$db_user,$db_pass) or die('Unable to connect database');
mysqli_select_db($conn,$db)or die("cannot connect database practice");
if(! $conn ) {
die('Could not connect: ' . mysql_error());}
$sql="Delete from lab12 where uname = '".$uname."' ";
$result=mysqli_query($conn,$sql);
if($result)
echo "You have successfully deleted your account";else
echo "Operation Failure please re-attempt";
?>