php mysql pdo crud

PHP, MySQL CRUD with PDO Driver – Minimal HTML & CSS


A quick PHP, MySQL CRUD operation with PDO mysql driver with minimul HTML and almost no CSS. I am going to share the code for each file and operation separately.

I am using XAMPP server with MariaDB, PHP 8.1, and Apache 2.4 web server on my Windows 10 Pro.

I assume that you are already familiar with XAMPP htdocs and phpmyadmin. I have created a database using phpmyadmin and rest everything in php files.

Create Database and MySQL Connection with PDO

/db.php

<?php 
$connect = new PDO('mysql:host=localhost;dbname=phpecom', 'root', 'password');

$connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 ?>

Create Profile page

This is where is the create or insert HTML form to insert row data into the database table

/index.php

<?php include_once "db.php"; ?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<link rel="stylesheet" href="style.css">
</head>
<body>

	<h3>Create a New Profile....</h3>

	<form action="crud.php" method="post">
		<input type="text" name="name" id="" placeholder="Full Name">
		<input type="email" name="email" id="" placeholder="[email protected]">
		<input type="text" name="username" id="" placeholder="useranme">
		<input type="password" name="password" id="" placeholder="**********">
		<input type="submit" name="submit" value="Create Profile">
	</form>

<br>
<br>
<br>
 <a href="crud.php">Show all</a>
	
</body>
</html>

The CREATE OR INSERT into table code is in a file called crud.php but you can also keep whatever file you want to in.

Read table rows

/crud.php

<h3>All Profiles...</h3>

<?php 
include_once "db.php";

$profiles = "CREATE TABLE IF NOT EXISTS profiles (
	u_id int not null auto_increment,
	u_name varchar(120) not null, 
	u_email varchar(120) not null, 
	u_username varchar(120) not null, 
	u_password varchar(120) not null, 
	primary key(u_id))";
	

$stmt = $connect->query($profiles);


if (isset($_POST['submit'])) {

$createProfile = "insert into profiles values()";

	$name = $_POST['name'];
	$email = $_POST['email'];
	$username = $_POST['username'];
	$password = $_POST['password'];

	$profileRow = "INSERT INTO profiles(u_name, u_email, u_username, u_password) VALUES('$name', '$email', '$username', '$password')";

	$row = $connect->query($profileRow);

}

$showProfiles = $connect->query("select * from profiles");


$rows = $showProfiles->fetchAll();

foreach ($rows as $row) {
	echo "<br><b>ID:</b> ", $row['u_id'], ", <b>Name:</b>", $row['u_name'], " <b>Email:</b> ", $row['u_email'], " <b>Password:</b> ", $row['u_password'], "  ",  "<a href=edit.php?id=". $row['u_id']. "> Edit</a>", "  ",  "<a href=view.php?id=". $row['u_id'] . "> View</a>", " ", "<a href=delete.php?id=". $row['u_id']. "> Delete</a>", "<br>";
}
 
?>



<br>
<br>
<br>
 <a href="index.php">Crate Profile</a>

Edit the Row data

/edit.php

<?php include_once "db.php"; ?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<link rel="stylesheet" href="style.css">
</head>
<body>

<h3>Edit Profile...</h3>
	<?php 
		$id = $_GET['id'];

		$profile = "select * from profiles where u_id = '$id' ";

		$editRow = $connect->query($profile);

		$updateRows = $editRow->fetchAll();

		foreach ($updateRows as $updateRow) {
		
	 ?>

	<form  method="post">
		<input type="text" name="name" value="<?php echo $updateRow['u_name'] ?>" placeholder="Full Name">
		<input type="hidden" name="id"  value="<?php echo $updateRow['u_id'] ?>" >
		<input type="email" name="email" value="<?php echo $updateRow['u_email'] ?>" placeholder="[email protected]">
		<input type="text" name="username" value="<?php echo $updateRow['u_username'] ?>" placeholder="useranme">
		<input type="password" name="password" value="<?php echo $updateRow['u_password'] ?>" placeholder="**********">
		<input type="submit" name="updateProfile" value="Update Profile">
	</form>

<?php } ?>


 <!-- update query on update button click -->
<?php 
if (isset($_POST['updateProfile'])) {
	$uid = $_POST['id'];
	$name = $_POST['name'];
	$email = $_POST['email'];
	$username = $_POST['username'];
	$password = $_POST['password'];

	$updateEdit = "UPDATE profiles SET u_name='$name', u_email='$email', u_username='$username', u_password='$password' where u_id = '$uid' ";

	$row = $connect->query($updateEdit);



	if ($row) {
		header('location:crud.php');
	}

}

 ?>
	
</body>
</html>

Single View Page

/view.php

<?php 
include_once('db.php');
$id = $_GET['id'];

$dataquery = $connect->query("select * from profiles where u_id = '$id'");

$viewdata = $dataquery->fetchAll();

foreach ($viewdata as $value) {

	echo "<br><b>ID:</b> ", $value['u_id'], ", <b>Name:</b>", $value['u_name'], " <b>Email:</b> ", $value['u_email'], " <b>Password:</b> ", $value['u_password'];
    
}

 ?>

Delete Page

/delete.php

<?php 

include_once "db.php";

$id = $_GET['id'];

$connect->query("DELETE FROM profiles WHERE u_id = '$id'");


header('Location: crud.php');


?>