How to insert and select data in php and mysql beginner guide

Read this tutorial for how to insert and select data in php and mysql beginner guide. This article is for beginner. After reading this article you will be able to insert the data in database and select the inserted data from the database. First create a  database in mysql name hrm and create a table name tasks 

CREATE TABLE IF NOT EXISTS `tasks` (
 `id` int(10) NOT NULL AUTO INCREMENT,
 `name` varchar(50) NOT NULL,
 `email` varchar(100) NOT NULL,
 `contact` int(12) NOT NULL,
 `address` varchar(100) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Now go to localhost and create a folder name PHP. Now open the folder and create a file name index.php . Now copy and paste the below code in the index file. We will create a form to insert the data in the database and will show the inserted data in the table below the form. Create connection with database in the file. You can create a separate file for connection and include in the top of PHP file.

<div>
<form method = "POST" action = "insert.php">
	Name:<br><input type="text" name = "name" ><br>
	Email:<br><input type="text" name = "email" ><br>
	Contact:<br><input type="text" name = "contact" ><br>
	Address:<br><input type="text" name = "address" ><br><br>
	<input type="submit" name="submit" value="submit">
</form>
</div>
 
<!-- here we will select the data inserted in database -->
 
<?php
 
	$servername = "localhost";
	$username = "root";
	$password = "";
	$database = "hrm";
 
// Create connection
	$conn = new mysqli($servername, $username, $password, $database);
 
	if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
	} 
 
	$sql = "SELECT * FROM tasks";
	$result = $conn->query($sql);
	if ($result->num_rows > 0) { ?>
	<div >
	<table style="text-align:centre">
		<th>Name</th><th>Email</th><th>Contact</th><th>Address</th>
 
	<?php
    // output data of each row
    while($row = $result->fetch_assoc()) { ?>
        <tr>
			<td><?php echo $row['name'];?></td>
			<td><?php echo $row['email'];?></td>
			<td><?php echo $row['contact'];?></td>
			<td><?php echo $row['address'];?></td>
		</tr>
  <?php  } ?>
  </table>
  </div>
  <?php
} else {
    echo "No record inserted yet";
}
?>

After adding this code save the file and run the URL http://localhost/php/
Your page will look like this

php insert and select data

Now create a another PHP file inside the PHP folder insert.php. We will add the code to insert the data in the database in this file. we will use $_POST to get the data from the form. Copy and paste the below code and put in your insert.php file.

<?php
 
	$servername = "localhost";
	$username = "root";
	$password = "";
	$database = "hrm";
 
// Create connection
	$conn = new mysqli($servername, $username, $password, $database);
 
	if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
	} 
 
 
		$name = $_POST['name'];
		$email = $_POST['email'];
		$contact = $_POST['contact'];
		$address = $_POST['address'];
 
 
	$sql = "INSERT INTO tasks (name, email, contact, address)
			VALUES ('".$name."', '".$email."', '".$contact."', '".$address."')";
 
	if ($conn->query($sql) === TRUE)
	{
		echo "New record created successfully";
	} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
	}
	$conn->close();
 
?>

Now add some data in the form and click on the submit button. After adding data you will see the inserted data in the database below in the table. After adding data your web-browser will look like this.

insert and select data in mysql and php

Hope this small tutorial how to insert and select data in php and mysql beginner guide will help you.