You are on page 1of 11

Database Queries

Database:

Code:
<table cellpadding="5" cellspacing="10" align="center">

<center>

<h3>

Students Registration

</h3>

</center>

<form method="post" action="#">

<tr>

<td>

Reg_no:

<input type="text" name="reg_no"><br><br>


Name:

<input type="text" name="name"><br/><br/>

Semester:

<input type="text" name="semester">

</td>

</tr>

<tr>

<td colspan="2" align="center">

<input type="submit" name="submit" value="Insert">

</td>

</tr>

</form>

</table>

<?php

$connection=mysqli_connect("localhost", "root", "");

$db=mysqli_select_db($connection, 'registration');

if (!$db){

echo mysqli_error($connection);

if($_POST){

$re=$_POST['reg_no'];

$na=$_POST['name'];

$em=$_POST['semester'];

$insert="insert into students (reg_no ,name, semester) values('$re','$na', '$em')";

$query=mysqli_query($connection,$insert);

if (!$query){
echo mysqli_error($connection);

}else{

echo "<script>alert('Data entered successfully')</script>";

?>

<table cellpadding="5" cellspacing="10" align="center">

<center>

<h3>

Update Data

</h3>

</center>

<form method="post" action="">

<tr>

<td>

Reg_no

<input type="text" name="reg_no">

</td>

</tr>

<tr>

<td>

Name:

<input type="text" name="name" id="name">

</td>

</tr>

<tr>

<td>
Semester:

<input type="text" name="semester" id="semester">

</td>

</tr>

<tr>

<td colspan="2" align="center">

<input type="submit" name="submit" value="update">

</td>

</tr>

</form>

</table>

<?php

$connection=mysqli_connect("localhost", "root", "");

$db=mysqli_select_db($connection, 'registration');

if(!$db)

echo mysqli_error($connection);

if($_POST)

$reg_no=$_POST['reg_no'];

$name=$_POST['name'];

$semester=$_POST['semester'];

$update="UPDATE students SET reg_no='$reg_no', semester='$semester' WHERE name='$name'";

$update=mysqli_query($connection, $update);

if(!$update)

echo mysqli_error($connection);

}
else

echo "<script>alert('Data Updated Successfully')</script>";

?>

<table cellpadding="5" cellspacing="10" align="center">

<center>

<h3>Delete Data</h3>

</center>

<form method="post" action="">

<tr>

<td>

Name :

<input type="text" name="name" id="name">

</td>

</tr>

<tr>

<td colspan="2" align="center">

<input type="submit" name="submit" value="delete">

</td>

</tr>

</form>

</table>

<?php

$connection=mysqli_connect("localhost","root","");
$db=mysqli_select_db($connection,'registration');

if(!$db)

echo mysqli_error($connection);

if ($_POST)

$name=$_POST['name'];

$delete="DELETE FROM students WHERE name='$name'";

$delete=mysqli_query($connection,$delete);

if(!$db)

echo mysqli_error($connection,$delete);

else

echo "<script>alert('Data deleted succesfully')</script>";

}
Insert Query:
Update Query:

Delete Query:
Select Query:
<head>

<title>Select</title>

</head>

<style type="text/css">

th

text-align: center;

border: 2px solid black;

padding-left: 10px;

padding-right: 10px;

padding-top: 5px;

padding-bottom: 5px;

td

text-align: center;

border: 2px solid black;

padding-right: 50px;

padding-left: 50px;

padding-top: 10px;

padding-bottom: 10px;

</style>

<?php

$connection=mysqli_connect("localhost", "root", "");

$db=mysqli_select_db($connection, 'registration');

if(!$db)

{
echo mysqli_error($connection);

$query="Select * FROM students";

if($query2=mysqli_query($connection, $query))

if(mysqli_num_rows($query2)>0)

echo "<table>";

echo "<tr>";

echo "<th>Reg</th>";

echo "<th>Name</th>";

echo "<th>Semester</th>";

echo "</tr>";

while($row=mysqli_fetch_array($query2))

echo "<tr>";

echo "<td>".$row['reg_no']."</td>";

echo "<td>".$row['name']."</td>";

echo "<td>".$row['semester']."</td>";

echo "</tr>";

echo "</table>";

else

echo "Records not Found!";

mysqli_close($connection);
?>

You might also like