In this post, we are going to explain how to insert, delete, update tables on a database using php.

DataBase And Table:
CREATE DATABASE demo;
CREATE TABLE reg (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) NOT NULL,
password VARCHAR(30) NOT NULL,
address VARCHAR(30) NOT NULL,
email VARCHAR(50),
fathername VARCHAR(30) NOT NULL,
mothername VARCHAR(30) NOT NULL,
country VARCHAR(50),
state VARCHAR(30) NOT NULL,
city VARCHAR(50),
photo VARCHAR(50))
INSERT
$query=mysql_connect("localhost","root","");
mysql_select_db("demo",$query);
if(isset($_POST['send']))
{
$name=$_POST['name'];
$password=$_POST['password'];
$address=$_POST['address'];
$email=$_POST['email'];
$fathername=$_POST['fathername'];
$mothername=$_POST['mothername'];
$country=$_POST['country'];
$state=$_POST['state'];
$city=$_POST['city'];
$photo=$_FILES["file"]["name"];
$tempphoto=$_FILES["file"]["tmp_name"];
$mode = 0777; 
 $target_dir = "upload/";
 if(!is_dir($target_dir))
 {
 mkdir($target_dir,$mode, true); 
 }
 move_uploaded_file($_FILES['file']['tmp_name'],$upload);
$in=mysql_query("insert into reg values('', '$name', '$password', '$address', '$email', '$fathername', '$mothername', '$country', '$state', '$city','$photo')");
}
<!DOCTYPE html>
<html>
<head>
 <title>Basic insert, delete,  update database  & Image upload using php</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<table width="400" border="0" cellspacing="0" cellpadding="0">
 <tr>
 <td>name</td>
 <td>:</td>
 <td><input type="text" name="name" /></td>
 </tr> 
 <tr>
 <td>password</td>
 <td>:</td>
 <td><input type="password" name="password" /></td>
 </tr>
 <tr>
 <td>address</td>
 <td>:</td>
 <td><input type="text" name="address" /></td>
 </tr>
 <tr>
 <td>email</td>
 <td>:</td>
 <td><input type="text" name="email" /></td>
 </tr>
 <tr>
 <td>fathername</td>
 <td>:</td>
 <td><input type="text" name="fathername" /></td>
 </tr>
 <tr>
 <td>mothername</td>
 <td>:</td>
 <td><input type="text" name="mothername" /></td>
 </tr>
 <tr>
 <td>country</td>
 <td>:</td>
 <td><input type="text" name="country" /></td>
 
 </tr>
 <tr>
 <td>state</td>
 <td>:</td>
 <td><input type="text" name="state" /></td>
 </tr>
 <tr>
 <td>city</td>
 <td>:</td>
 <td><input type="text" name="city" /></td>
 
 </tr>
 <tr>
 <td>
 <input type="file" name="file"/>
 </td>
 </tr>
 <tr>
 <td>
 <input type="submit" name="send" value="submit" />
 </td>
 </tr>
</table>

</form>
</body>
</html>

VIEW 

<?php

$query=mysql_connect("localhost","root","");

mysql_select_db("demo",$query);

?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<?php
$s=mysql_query("SELECT * FROM reg");
echo"<table border='1' align='center'>";
echo"<th>Name</th>";
echo"<th>ID</th>";
echo"<th>address</th>";
echo"<th>email</th>";
echo"<th>fathername</th>";
echo"<th>mothername</th>";
echo"<th>country</th>";
echo"<th>state</th>";
echo"<th>city</th>";
echo"<th>Edit</th>";
echo"<th>Delete</th>"; 
while($query2=mysql_fetch_array($s)) 
{ 
echo "<tr><td>".$query2['name']."</td>"; 
echo "<td>".$query2['id']."</td>"; 
echo "<td>".$query2['address']."</td>"; 
echo "<td>".$query2['email']."</td>"; 
echo "<td>".$query2['fathername']."</td>"; 
echo "<td>".$query2['mothername']."</td>"; 
echo "<td>".$query2['country']."</td>"; 
echo "<td>".$query2['state']."</td>"; 
echo "<td>".$query2['city']."</td>"; 
echo "<td><a href='edit.php?id=".$query2['id']."'>Edit</a></td>"; 
echo "<td><a href='delete.php?id=".$query2['id']."'>Delete</a></td></tr>"; 
} 
echo "</table>"; 
?> 
</body> </html>

EDIT

 <?php
$query=mysql_connect("localhost","root","");
mysql_select_db("demo",$query);
$ID=$_GET['id'];
if(isset($_POST['submit']))
{
$name=$_POST['name'];
$address=$_POST['address'];
$email=$_POST['email'];
$fathername=$_POST['fathername'];
$mothername=$_POST['mothername'];
$country=$_POST['country'];
$state=$_POST['state'];
$city=$_POST['city'];
$query3=mysql_query("update reg set name='$name',
address='$address',
email='$email',
fathername='$fathername',
mothername='$mothername',
country='$country',
state='$state',
city='$city' where id='$ID'"); 
}
?>
<!DOCTYPE html>
<html>
<head>
 <title>Basic insert, delete, update database & Image upload using php</title>
</head>
<body>
</body>
<?php
$query1=mysql_query("select * from reg where id='$ID'");
$a=mysql_fetch_array($query1);
?>
<form method="post" action="">
 <table width="400" border="0" cellspacing="30" cellpadding="0" >
 <tr>
 <td>Name</td>
 <td>:</td>
 <td><input type="text" name="name" value="<?php echo $a['name'];?>"></td>
 </tr>
 <tr>
 <td>address;</td>
 <td>:</td>
 <td><input type="text" name="address" value="<?php echo $a['address'];?>"></td>
 </tr>
 <tr>
 <td>email</td>
 <td>:</td>
 <td><input type="text" name="email" value="<?php echo $a['email'];?>"></td>
 </tr>
 <tr>
 <td>fathername</td>
 <td>:</td>
 <td><input type="text" name="fathername" value="<?php echo $a['fathername'];?>"></td>
 </tr>
 <tr>
 <td>mothername</td>
 <td>:</td>
 <td><input type="text" name="mothername" value="<?php echo $a['mothername'];?>"></td>
 </tr>
 <tr>
 <td>country</td>
 <td>:</td>
 <td><input type="text" name="country" value="<?php echo $a['country'];?>"></td>
 </tr>
 <tr>
 <td>state</td>
 <td>:</td>
 <td><input type="text" name="state" value="<?php echo $a['state'];?>"></td>
 </tr>
 <tr>
 <td>city</td>
 <td>:</td>
 <td><input type="text" name="city" value="<?php echo $a['city'];?>"></td>
 </tr>
 </table>
</form>
</body>
</html>

DELETE

 <?php
$query=mysql_connect("localhost","root","");
mysql_select_db("demo",$query);
$ID=$_GET['id'];
if(isset($_GET['id']))
{
$id=$_GET['id'];
$query1=mysql_query("delete from reg where id='$id'");
}
?>

 

Leave a Reply