PHP MYSQL
INSERT UPDATE DELETE SEARCH
WEB FORM
save file_name.php
<html>
<head>
<title>Form</title>
</head>
<body>
<form action="file_name2.php" method="POST">
ID <input type="text" name="id" ><br><br>
Name <input type="text" name="name" ><br><br>
Email<input type="text" name="mail" ><br><br><br>
<input type="submit" name="submit" value="Insert">
<input type="submit" name="update" value="Update">
<input type="submit" name="delete" value="Delete">
<input type="submit" name="search" value="Search">
</form>
</body>
</html>
SQL CONNECT CODE
save file_name2.php
<html>
<head>
<title>CIS</title>
<style>
table,th,td {
border:1px solid black;
background-color: #FF9900;
}
</style>
</head>
<body>
<?php
$host="localhost";
$uname="root";
$pw="";
$dbname="uduwanage";
$conn=mysqli_connect($host,$uname,$pw,$dbname);
if($conn){
echo "successfully connected";
echo "<br>";
}
else{
echo "error";
}
if(!empty($_POST['submit'])){
$s="INSERT INTO user(id,name,mail)
values('$_POST[id]','$_POST[name]','$_POST[mail]')";
mysqli_query($conn,$s);
}
if (isset($_POST['update'])){
$id=$_POST['id'];
$name=$_POST['name'];
$mail=$_POST['mail'];
$u=("update user set name='$name',mail='$mail' where id='$id'");
mysqli_query($conn,$u);
}
$sel="select * from user";
$search=$conn->query($sel);
if($search->num_rows>0){
echo "<table><tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>";
while($row=$search->fetch_assoc()){
echo "<tr><td>".$row["id"]."</td>
<td>".$row["name"]."</td>
<td>".$row["mail"]."</td></tr>";
}
echo "</table>";
}
if (isset($_POST['delete'])){
$id=$_POST['id'];
$d=("delete from user where id='$id'");
mysqli_query($conn,$d);
}
?>
</body>
</html>
No comments:
Post a Comment