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

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...

AJAX POST IN PHP

AJAX POST SCRIPT ________________________________________________________ index.php ________________________________________________________ <!DOCTYPE html> <html> <head> <title>AJAX POST</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script type="text/javascript">    jQuery(function($){            var text1 = $('#text_id1');            var text2 = $('#text_id2');            $('#button_submit').click(function(){                    jQuery.ajax({                        ...

Auto refresh div using JQuery

<script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>         <script>         $(document).ready(function(){         setInterval(function(){         $("#set").load('refresh.php')         }, 1000);         });         </script>                  <div id="set"></div>