Lab No 12
Lab No 12
LAB NO# 12
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.
OBJECTIVE #2:
To insert record in MySQL database using PHP
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
BROWSER OUTPUT:
Running it on the browser with the link
Ramsha Aftab 2021F-BCE-013
http://localhost/ramsha068/
Ramsha Aftab 2021F-BCE-013
Objective # 3:
To delete record from MySQL database.
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";
?>