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
Post a Comment