Press "Enter" to skip to content

PHP MySQL INSERT Data

Html template.

First create a folder named phpCrud then inside the folder create 3 files config.php, Database.php and create.php. Inside the create.php file pest the html code then open config.php file and pest the code then last open the Database.php file and pest the code i have given.

If you can not create the mysql database you can see video from youtube how mysql database create then you give the database name in config file.

For Inserting the data i use this template.

<!doctype html>
<html>
<head>
 <title>PHP OOP CRUD</title>
 <style>
		 body{font-family:verdana}
	    .phpcoding{width:900px; margin: 0 auto;
		 background:#ddd}
	    .headeroption, .footeroption{background:#444;
		color:#fff;text-align:center;padding:20px;}
	    .headeroption h2, .footeroption h2{margin:0;font-size:24px}
	    .maincontent{min-height:400px;padding:20px;font-size:18px}
		 p{margin:0}
		 input[type="text"]{width:238px;padding:5px;}
		 select{font-size:18px;padding:2px 5px;width:250px;}
		 .tblone{width:100%;border:1px solid #fff;margin:20px 0}
		 .tblone td{padding:5px 10px;text-align:center;}
		 table.tblone tr:nth-child(2n+1){background:#fff;height:30px;}
		 table.tblone tr:nth-child(2n){background:#f1f1f1;height:30px;}
		 #myform{width:400px;border:1px solid #fff;padding:10px;}
 </style>
</head>
<body>

<div class="phpcoding">
 <section class="headeroption">
  <h2>CRUD Using OOP PHP and MYSQLi</h2>
 </section>
  <section class="maincontent">
  
<form action="" method="post">
<table>
 <tr>
  <td>Name</td>
  <td><input type="text" name="name" placeholder="Please enter
   name"/></td>
 </tr>
 <tr>
  <td>Email</td>
  <td><input type="text" name="email" placeholder="Please enter
   email"/></td>
 </tr>

 <tr>
  <td>Skill</td>
  <td><input type="text" name="skill" placeholder="Please enter
  Skill"/></td>
 </tr>
 <tr>
  <td></td>
  <td>
  <input type="submit" name="submit" value="Submit"/>
  <input type="reset" Value="Cancel" />
  </td>
 </tr>

</table>
</form>

</section>
<section class="footeroption">
<p>&copy 2020 I Am a PHP Learner</p>
<h2>www.programsolve.com</h2>
</section>
</div>
</body>
</html>

Inserting Data into a MySQL Database Table.

Now that you’ve understood how to create database and tables in MySQL. In this tutorial you will learn how to execute SQL query to insert records into a table.

The INSERT INTO statement is used to insert new rows in a database table.

Let’s make a SQL query using the INSERT INTO statement with appropriate values, after that we will execute this insert query through passing it to the PHP mysqli_query() function to insert data in table. Here’s an example, which insert a new row to the tbl_user table by specifying values for the name, email and skill fields.

Config file.

First we create php file and the name of the file will be config.php, in config file we write this line of code…

<?php
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASS", "");
define("DB_NAME", "db_crud");

Database file creation.

Here we follow full php oop method so, we use data insert file and the file name will be Database.php. This file will include the config.php file.

<?php
class Database{
	public $host   = DB_HOST;
	public $user   = DB_USER;
	public $pass   = DB_PASS;
	public $dbname = DB_NAME;
	
	public $link;
	public $error;
	
	public function __construct(){
		$this->connectdb();
	}
	
	private function connectdb(){
		$this->link = new mysqli($this->host, $this->user, $this->pass, $this->dbname);
		if(!$this->link){
			$this->error = "connection fail".$this->link->connect_error;
			return false;
		}
	}
	
        
        
        	// Insert Data
	public function insert($data) {
        $name = mysqli_real_escape_string($this->link, $data['name']);
        $email = mysqli_real_escape_string($this->link, $data['email']);
        $skill = mysqli_real_escape_string($this->link, $data['skill']);
        if ($name == '' || $email == '' || $skill == '') {
            return $error = "Field must not be Empty !!";
        } else {
            $query = "INSERT INTO tbl_user(name, email, skill) values('$name', '$email', '$skill') ";
            $insert_row = mysqli_query($this->link ,$query) or die($this->link->error . __LINE__);
            if ($insert_row) {
                header("Location: index.php?msg=" . urlencode('Data Inserted Successfully.'));
                exit();
            } else {
                die("Error : (" . $this->link->errno . ")" . $this->link->error);
            }
        }
    }

Finally i use this line of code inside the html file which name i gave create.php. This line of code need to pest inside body tag.

<?php
include "config.php";
include "Database.php";
?>

<?php
 $db = new Database();
if(isset($_POST['submit'])){
  $error = $db->insert($_POST);
}
?>

<?php
if(isset($error)){
 echo "<span style='color:red'>".$error."</span>";
}
?>

At last i give some picture of file and after the code.

Files folder picture.

Second after run the code.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *