PHP Form CRUD With Code

Hello friends, in today’s post we will tell you about php crud, how it is done, along with its program, you can also see its output.


PHP CRUD


If we talk about crud in PHP, what is crud, then let us tell you that crud operation is also done in php or other programming languages. Full name of crud.
C – Create (Create) means through this we can create any data.
R – Read (Read) means through this we can read
U – update

<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous">
</head>
<body>
<div class="container-fluid">
<div class="row">
	<div class="col-sm-4"></div>
	<div class="col-sm-4 border mt-5 bg-warning">
		<form action="" method="POST" enctype="multipart/form-data">
			<h3 class="mt-2 text-center">CRUD OPERATION</h3>
			Name:<input type="text" name="name" class="form-control" required><br/>
			Email:<input type="email" name="email" class="form-control" required><br/>
			Image:<input type="file" name="image" class="form-control" required><br/>
			<input type="submit" class="btn btn-success" value="Submit" name="save">
		</form>
	</div>
	<div class="col-sm-4"></div>
</div>
</div>
</body>
</html>

<?php
include_once('conn.php');
if(isset($_POST['save'])){
	$name=$_POST['name'];
	$email=$_POST['email'];
	
	$files_arr=$_FILES['image'];
	$fname=$files_arr['name'];
	$type=$files_arr['type'];
	$size=$files_arr['size'];
	$error=$files_arr['error'];
	$tmp_path=$files_arr['tmp_name'];
	
	$fileinfo=pathinfo($fname);
	$extension=$fileinfo['extension'];
	$filename=$fileinfo['filename'];
	
	$image=$filename."_".time().".".$extension;
	$new_location=__DIR__.'/uploads/'.$filename."_".time().".".$extension;

	$date = date('Y-m-d');
				
	$sql="INSERT INTO neotbl(name,email,image,date) VALUES('{$name}','{$email}','{$image}',{$date});";
	$query=mysqli_query($conn,$sql);
	if($query){
		if($error==0){

			if(move_uploaded_file($tmp_path,$new_location)){
				echo "file uploaded";
				header("location:show.php");
			}else{
				echo "file not uploaded";
				header("location:show.php");
			}
		}else{
			echo "error found";
		}
	}else{
		echo "data not inserted";
	}
	
}

?>

<html>
<head>

</head>
<body>
<h1 style="text-align:center;color:red;">SHOW DATA</h1>
<table border="2" width="100%" cellspacing="0">
	<tr>
	<th>SR.</th>
	<th>ID</th>
	<th>NAME</th>
	<th>EMAIL</th>
	<th>IMAGE</th>
	<th>DELETE</th>
	<th>UPDATE</th>
	</tr>
	
	<?php
		include_once'conn.php';
		
		$sql="SELECT * FROM neotbl;";
		$query=mysqli_query($conn,$sql);
		$i=1;
		while($row=mysqli_fetch_assoc($query)):
		
		
		?>
		<tr>
		<td><?php echo $i; ?></td>
		<td><?php echo $row['id']; ?></td>
		<td><?php echo $row['name']; ?></td>
		<td><?php echo $row['email']; ?></td>
		<td><img height="40px" width="50px" src="<?php echo "http://localhost/php/neotest/uploads/".$row['image'];?>" alt=""></td>
		<td><a onclick="return confirm('are you sure want to delete item')" href="delete.php?id=<?php echo $row['id']; ?>">Delete</a></td>
		<td><a href="update.php?id=<?php echo $row['id']; ?>">Edit</a></td>
		
		<?php
		
		$i=$i+1;
		endwhile;
		?>
		</tr>
		
</table>
		
</body>
</html>

<?php
$id=$_GET['id'];
include_once'conn.php';

$sql="DELETE FROM neotbl WHERE id='{$id}';";
if(mysqli_query($conn,$sql)){
	echo "Record deleted successfullt";
				header("location:show.php");
}else{
	echo "Record not deleted";
	header("location:show.php");

}

?>

<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous">
</head>
<body>
<div class="container-fluid">
<div class="row">
	<div class="col-sm-4"></div>
	<div class="col-sm-4 border mt-5 bg-warning">
		<form action="" method="POST" enctype="multipart/form-data">
			<h3 class="mt-2">UPDATE RECORD</h3>
			Name:<input type="text" name="name" class="form-control"
			placeholder="<?php echo $row['name']; ?>"><br/>
			Email:<input type="email" name="email" class="form-control"
			placeholder="<?php echo $row['email']; ?>"><br/>
			Image:<input type="file" name="image" class="form-control"><br/>
			<input type="submit" class="btn btn-success" value="Submit" name="save">
		</form>
	</div>
	<div class="col-sm-4"></div>
</div>
</div>
</body>
</html>

<?php
 $id=$_GET['id'];
include_once'conn.php';
$id=$_GET['id'];
$sql="select * from neotbl where id='{$id}';";
$query=mysqli_query($conn,$sql);
while($row=mysqli_fetch_assoc($query)):

?>
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous">
</head>
<body>
<div class="container-fluid">
<div class="row">
	<div class="col-sm-4"></div>
	<div class="col-sm-4 border mt-5 bg-warning">
		<form action="" method="POST" enctype="multipart/form-data">
			<h3 class="mt-2">UPDATE RECORD</h3>
			Name:<input type="text" name="name" class="form-control"
			placeholder="<?php echo $row['name']; ?>"><br/>
			Email:<input type="email" name="email" class="form-control"
			placeholder="<?php echo $row['email']; ?>"><br/>
			Image:<input type="file" name="image" class="form-control"><br/>
			<input type="submit" class="btn btn-success" value="Submit" name="save">
		</form>
	</div>
	<div class="col-sm-4"></div>
</div>
</div>
</body>
</html>
<?php
endwhile;
?>
<?php
include_once('conn.php');
if(isset($_POST['save'])){
	$id=$_GET['id'];
	$name=$_POST['name'];
	$email=$_POST['email'];
	
	if(!empty($_FILES['image'])){
		$files_arr=$_FILES['image'];
		$fname=$files_arr['name'];
		$type=$files_arr['type'];
		$size=$files_arr['size'];
		$error=$files_arr['error'];
		$tmp_path=$files_arr['tmp_name'];
		
		$fileinfo=pathinfo($fname);
		$extension=$fileinfo['extension'];
		$filename=$fileinfo['filename'];
		$image=$filename."_".time().".".$extension;
		$new_location=__DIR__.'/uploads/'.$filename."_".time().".".$extension;
	}else{
		$sql = "SELECT * FROM neotbl WHERE id = '{$id}'";
		echo $sql;die;
	}
	die;
	

	$c_date = date('Y-m-d');
				
	$sql="update neotbl set name='{$name}',email='{$email}',image='{$image}',date='{$c_date}' where id='{$id}';";
	$query=mysqli_query($conn,$sql);
	if($query){
		if($error==0){
			if(move_uploaded_file($tmp_path,$new_location)){
				echo "file updated";
				header("location:show.php");

			}else{
				echo "file not updated";
			header("location:show.php");

			}
		}else{
			echo "error found";
		}
	}else{
		echo "data not updated";
	}
	
}

?>
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous">
</head>
<body>
<div class="container-fluid">
<div class="row">
	<div class="col-sm-4"></div>
	<div class="col-sm-4 border mt-5 bg-warning">
		<form action="" method="POST" enctype="multipart/form-data">
			<h3 class="mt-2 text-center">CRUD OPERATION</h3>
			Name:<input type="text" name="name" class="form-control" required><br/>
			Email:<input type="email" name="email" class="form-control" required><br/>
			Image:<input type="file" name="image" class="form-control" required><br/>
			<input type="submit" class="btn btn-success" value="Submit" name="save">
		</form>
	</div>
	<div class="col-sm-4"></div>
</div>
</div>
</body>
</html>
<?php
include_once('conn.php');
if(isset($_POST['save'])){
	$name=$_POST['name'];
	$email=$_POST['email'];
	
	$files_arr=$_FILES['image'];
	$fname=$files_arr['name'];
	$type=$files_arr['type'];
	$size=$files_arr['size'];
	$error=$files_arr['error'];
	$tmp_path=$files_arr['tmp_name'];
	
	$fileinfo=pathinfo($fname);
	$extension=$fileinfo['extension'];
	$filename=$fileinfo['filename'];
	
	$image=$filename."_".time().".".$extension;
	$new_location=__DIR__.'/uploads/'.$filename."_".time().".".$extension;

	$date = date('Y-m-d');
				
	$sql="INSERT INTO neotbl(name,email,image,date) VALUES('{$name}','{$email}','{$image}',{$date});";
	$query=mysqli_query($conn,$sql);
	if($query){
		if($error==0){

			if(move_uploaded_file($tmp_path,$new_location)){
				echo "file uploaded";
				header("location:show.php");
			}else{
				echo "file not uploaded";
				header("location:show.php");
			}
		}else{
			echo "error found";
		}
	}else{
		echo "data not inserted";
	}
	
}

?>

Leave a Comment