0% found this document useful (0 votes)
3 views

Lab 4

Uploaded by

Dawit Sebhat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lab 4

Uploaded by

Dawit Sebhat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Lab 4

1. Connecting database with server-side code making operations


i. Connecting database:
➢ Write the following PHP code in the same directory and save
connection.php
<?php
$conn = mysqli_connect("localhost","root","","file_storage");
if(!$conn){
die("Connection error: " . mysqli_connect_error());
}
?>
ii. Creating HTML Forms
form.html
<html><head><title>Registration Forms</title></head><body>
<form action =”process.php” method =”post”>
Name: <input type=”text” name=”fname” required=”true” /><br>
Address: <input type=”text” name=”address” required=”true” /><br>
Phno: <input type=”number” name=”phno” required=”true” /><br>
Email: <input type=”email” name=”email” required=”true” /><br>
<input type=”submit” value=”Save” name=”submit”/>
<input type=”reset” value=”Clear” name=”Clear”/>
</form></body></html>
iii. Inserting value to database:
process.php
<?php
require_once("connection.php");
if (isset($_POST['submit'])) {
$name = mysqli_real_escape_string($conn, $_POST['fname']);
$address = mysqli_real_escape_string($conn, $_POST['address']);
$phno = mysqli_real_escape_string($conn, $_POST['phno']);
$email = mysqli_real_escape_string($conn, $_POST['email']);

$conn->query ("INSERT INTO student (id,name,address, phno, email)


VALUES('','$name','$address’,'$phno','$email')") or die(mysqli_error($conn));?>

iv. Displaying Data


display.php

<table><tr>

<th>Name</th><th>Adress</th><th>Phno</th><th>Email</th><th>Action </th></tr>

<?php

require_once("connection.php");

$sql = "SELECT * FROM student";

$result = mysqli_query($conn, $sql) or die(mysqli_errno($conn));

while ($row = mysqli_fetch_array($result)) {


$id = $row['id'];

$fname = $row['fname'];

$address = $row[‘address’];

$phno = $row[‘phno’];

$email = $row[‘email’];

?>

<tr><td><?php echo $fname; ?></td><td><?php echo $address; ?></td><td><?php echo $phno;


?></td><td><?php echo $email; ?></td></tr>

<td><a href =”delete.php?stu_id=<?php echo $id; >” class =”btn btn-red”>Delete


Student</a></td></tr></table>

<?php } ?>

V. Deleting Data

<?php

require_once("connection.php");

if(isset($_GET[‘id’])){

$id = mysqli_real_escape_string($conn, $_GET[‘stu_id’];

Mysqli_query($con, “DELETE FROM STUDENT WHERE id =’$id’”) or die(mysqli_error($con));

Echo ‘<div id =”div1” style=”color: red;”>Sucessfully Deleted!</div>’;

You might also like