Skip to main content

PHP CRUD Operations


Table Query
___________________________________________________________________

CREATE TABLE IF NOT EXISTS `emp` (
  `id` int(11) NOT NULL,
  `name` varchar(50) DEFAULT NULL,
  `salary` int(11) DEFAULT NULL,
  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;


config.php
________________________________________________________________________________

<?php
$server = "localhost";
$user = "root";
$password = "";
$dbname = "newdb";
$conn =  new mysqli($server,$user,$password,$dbname);
if($conn->connect_error)
{
die("connection failed:".$conn->connect_error);
} ?>

index.php
___________________________________________________________________

<?php include 'config.php'; ?>
<html>
<head>
</head>
<style>
table, th, td 
{
padding: 8px;
    border: 1px solid black;
    border-collapse: collapse;
}
</style>
<body>
<form action="query.php" method="post">
<table>
<caption>Add New Employee Info</caption>
<tr>
<td>Name: </td><td><input type="text" name="name"></td>
</tr>
<tr>
<td>Salary: </td><td><input type="text" name="salary"> <br></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="insert" value="Insert"></td>
</tr>
</table>
</form>
<!-- ======================================================== -->
<?php
$sql = "SELECT * FROM emp ";
$result = $conn->query($sql);
if($result->num_rows)
{ ?>
<table style="1">
<caption>View Employee Info</caption>
<tr >
<th>ID</th>
<th>Name</th>
<th>Salary</th>
<th colspan="2">Options</th>
</tr>
<?php
$sql = "SELECT * FROM emp ";
$result = $conn->query($sql);
while($row = $result->fetch_assoc())
{
$id= $row["id"];
echo "<tr>
<td>" . $row["id"] ." </td>
<td> ".$row['name'] ."   </td>
<td> ".$row['salary'] ."</td>
<td> <a href='index.php?id=$id' >edit</a></td>"."</td>
<td> <a href='query.php?id=$id' >delete</a></td>
</tr>";
} ?>
</table>
<?php
}
else
{
echo "0 results";
}
?>
<!-- ======================================================== -->
<?php
if(isset($_GET['id']))
{
$eid = $_GET['id'];
$sql = "SELECT * FROM emp WHERE id=$eid ";
$result = $conn->query($sql);
if($result->num_rows)

while($row = $result->fetch_assoc())
{
$name= $row['name'];
$salary =$row['salary']; ?>
<br>
<form action="query.php" method="post">
<table>
<caption>Update Employee Info</caption>
<tr>
<input type="hidden" name="eid" value="<?php echo $eid; ?>"> 
<td>Name: </td>
<td><input type='text' name='name' value='<?php echo $name; ?>'></td>
</tr>
<tr>
<td>Salary: </td>
<td><input type='text' name='salary' value='<?php echo $salary; ?>'></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="update" value="Update"></td>
</tr>
</table>
</form>
<?php 
}
}
}
$conn->close(); ?>
</body>

</html>

query.php
___________________________________________________________________

<?php
include 'config.php'; 
//=========insert query=======================
if(isset($_POST['insert']))
{
$name =$_POST['name'];
$salary =$_POST['salary'];
$sql ="INSERT INTO emp(name,salary) VALUES('$name','$salary')";
if($conn->query($sql)===TRUE)
{
echo "<script> window.location.href='index.php'; </script>";
}
else
{
echo "error:"."<br>".$conn->error;
}
}
//=========update query=======================
if(isset($_POST['update']))
{
$eid =$_POST['eid'];
$name =$_POST['name'];
$salary =$_POST['salary'];
$sql ="UPDATE emp SET name='$name', salary='$salary' WHERE id=$eid";
if($conn->query($sql)===TRUE)
{
echo "<script> window.location.href='index.php'; </script>";
}
else
{
echo "error:"."<br>" .$conn->error;
}
}
//=========delete query=======================
if(isset($_GET['id']))
{
$eid =$_GET['id'];
$sql ="DELETE FROM emp  WHERE id=$eid";
if($conn->query($sql)===TRUE)
{
echo "<script> window.location.href='index.php'; </script>";
}
else
{
echo "error:"."<br>" .$conn->error;
}
}
$conn->close();
?>
                              - by Pavan Khandenor 

Comments

Popular posts from this blog

transparent css

body {   font-family: 'Open Sans', sans-serif;   font-size: 14px;   line-height: 45px;   /*color: #666;*/   color: black;   background-color: #fff; } :hover {   text-decoration: none;   outline: none } .form-control {   background-color: transparent;   border-color: black;   height: 50px;   border-radius: 0;   box-shadow: none; } textarea.form-control {   min-height: 180px;   resize:none; } .form-group {   margin-bottom: 30px; } .contact-info {   padding-left:70px;   font-weight: 300; } ul.address {   margin-top: 30px;   list-style: none;   padding: 0;   margin: 0; } .btn-submit {   display: block;   padding: 12px;   width: 100%;   background:transparent;   color: #4985C6;   border:1px solid black;   margin-top: 40px; } .btn-submit:hover {   display: block;   padding: 12px; ...

Get Current Location & Address in PHP

index.php ____________________________________________________________________ <!DOCTYPE html> <head> <title>GEt Current Location</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> <script type="text/javascript"> jQuery(function($){ var lat = $('#lat'); var lng = $('#lng'); $('#submit').click(function(){ jQuery.ajax({ url: 'get_address.php', type: 'post', data: 'lat=' + lat.val() + '&lng='+lng.val(), success: function(results){ $("#ack").val(results) } }); }); }); </script> <script> if(!navigator.geolocation){ alert('Your Browser does not support HTML5 Geo Location. Please Use Newer Version Browsers'); } navigator.geolocation.getCurrentPosition(success, error); function success(position){ var latit...

Page loader

<html> <head> <style> .no-js #loader { display: none;  } .js #loader { display: block; position: absolute; left: 100px; top: 0; } .preload { position: fixed; left: 0px; top: 0px; width: 100%; height: 100%; z-index: 9999; background: url(load.gif) center no-repeat #fff; } </style> </head> <body> <div class="preload"></div> <script src="js/jquery.min.js"></script> <script type="text/javascript"> //paste this code under head tag or in a seperate js file. // Wait for window load $(window).load(function() { // Animate loader off screen $(". preload ").fadeOut("slow");; }); </script> </body> </html>