PHP PDO insert into

PHP PDO Mysql insert into statement

user_table.sql
CREATE TABLE IF NOT EXISTS `demo_user` (
  `user_id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `email` varchar(100) NOT NULL,
  PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

INSERT INTO `demo_user` (`user_id`, `name`, `email`) VALUES
(1, 'Lucy', 'lucy@fakemail.com'),
(2, 'Brooke', 'brooke@fakemail.com'),
(3, 'Anna', 'anna@fakemail.com'),
(4, 'Mariya', 'mariya@fakemail.com'),
(5, 'Jack', 'jack@fakemail.com'),
(6, 'Jack', 'jack123@fakemail.com'),
(7, 'Anna', 'anna123@fakemail.com');  
db_connection.php
<?php

	define('DBHOST','localhost');
	define('DBUSER','root'); //DB username
	define('DBPASS',''); //DB password
	define('DBNAME','demo_table'); //DB table name

	try {
		$db_conn = new PDO("mysql:host=".DBHOST.";charset=utf8mb4;dbname=".DBNAME, DBUSER, DBPASS);
		$db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

		//connection success

	} catch(PDOException $e) {

		//connection error
		die("Error connection ".$e->getMessage());
	} 
?>
insert.php
<?php 

if (isset($_POST['user_name']) && isset($_POST['user_email'])) {

	require 'db_connection.php';

	$name = $_POST['user_name'];
	$email = $_POST['user_email'];

	$stmt = $db_conn->prepare('SELECT `email` FROM `demo_user` WHERE `email` = :email');
	$stmt->bindValue(':email', $email, PDO::PARAM_STR);
	$stmt->execute();
	$row = $stmt->fetch(PDO::FETCH_ASSOC);

	//check if same email row exist
	if ($row) {

		echo "This email alredy in use";
		exit();

	} else {

		//insert statement
		$sql = 'INSERT INTO `demo_user` (`name`, `email`) VALUES (:name, :email)';
		$insert_stmt = $db_conn->prepare($sql);
		$insert_stmt->bindValue(':name', $name, PDO::PARAM_STR);
		$insert_stmt->bindValue(':email', $email, PDO::PARAM_STR);
		$insert_stmt->execute();

		// success
		// redirect main page 
		header('Location: index.php');
	}

	//close connection
    $db_conn = null;

}
?>  
index.php
 <!DOCTYPE html>
 <html>
 <head>
 	<title>Users Table</title>
 	<style>
	  #myTable{
	     font-family: 'helvetica neue',helvetica,arial,'lucida grande',sans-serif;
	     border-collapse: collapse;
	  }
	  #myTable td{
	     border: solid 1px #C9C9C9;
	     padding: 10px;
	     font-size: 12px;
	     text-align: left;
	  }
	  #myTable tr{
	     color: #000000;
	  }
	  #myTable th{
	     border: solid 1px #BFBFBF;
	     padding: 10px;
	     color: #000000;
	     background-color: #f5f5f5;
	     text-align: center;
	     font-size: 12px;
	  }
	</style>
 </head>
 <body>

 	<form action="insert.php" method="post">
 		<div>Add New user</div><br>
 		<div>Name <input type="text" name="user_name" required=""></div><br>
 		<div>Email <input type="email" name="user_email" required=""></div><br>
 		<div><input type="submit" name="submit" value="submit"></div><br>
 	</form>

 	<?php 
 		require 'db_connection.php';

 		// select all from user table
		$stmt = $db_conn->prepare('SELECT * FROM `demo_user`');
		$stmt->execute();
		$row = $stmt->fetchAll(PDO::FETCH_ASSOC);

		echo '<table id="myTable" width="100%">
			<thead>
			<tr>
			<th>User ID</th>
			<th>Name</th>
			<th>Email</th>
			</tr>
			</thead>';

		foreach ($row as $value) {
			echo  '<tbody>
			<tr>
			<td>'.$value['user_id'].'</td>
			<td>'.$value['name'].'</td>
			<td>'.$value['email'].'</td>
			</tr>';
		}

		echo ' </tbody>
		</table>';

		//close connection
		$db_conn = null;
 ?>

 <script>
 	//prevent resubmission post
 	if ( window.history.replaceState ) {
 		window.history.replaceState( null, null, window.location.href );
 	}
 </script>

 </body>
 </html>
Folder Name :
  • pdo-insert
    • index.php
    • insert.php
    • db_connection.php
    • user_table.sql
hostinger