You are currently viewing How to submit data to SQL in PHP

How to submit data to SQL in PHP

In this article, we will see how can we post form data to MYSQL in PHP using OOPS concept. We will write a separate function for form submission and then will include that in index.php

In the example below, we will take input from the user in a form and then we will submit that in the MYSQL database.

Index.php

<?php
include_once("function.php");
$insertdata=new DB_connection(); 
if(isset($_POST['submit']))
{
$name=$_POST['name'];
$email=$_POST['email'];
$contact=$_POST['contact'];
$gender=$_POST['gender'];
$education=$_POST['education'];
$sql=$insertdata->insertdData($name,$email,$contact,$gender,$education);
if($sql)
{
echo "<script>alert('Data inserted');</script>";
}
else
{
echo "<script>alert('Data not inserted');</script>";
}
}
 ?>
<!DOCTYPE html>
<html>
	<body>
   <h3>Insert Data In Databse</h3>
		<form action="" method="post">
       <table width="100%"  border="0">
  <tr>
    <th width="26%" height="60" scope="row">Name :</th>
    <td width="74%"><input type="text" name="name" value="" required /></td>
  </tr>
  <tr>
    <th height="60" scope="row">Email :</th>
    <td><input type="email" name="email" value=""  required /></td>
  </tr>
  <tr>
    <th height="60" scope="row">Contact no. :</th>
    <td><input type="text" name="contact"  required /></td>
  </tr>
  <tr>
    <th height="60" scope="row">Gender :</th>
    <td><input type="radio" name="gender" value="Male" required /> Male   
	<input type="radio" name="gender" value="Female" required /> Female</td>
  </tr>
   <tr>
    <th height="60" scope="row">Education :</th>
    <td><select name="education" >
	<option value="">Select</option>
	<option value="Primary">Primary</option>
	<option value="secondary">secondary</option>
	<option value="Graduate">Graduate</option>
	<option value="Post Graduate">Post Graduate</option>
	</select> </td>
  </tr>
  <tr>
    <th height="60" scope="row"> </th>
    <td><input type="submit" value="Submit" name="submit"  /></td>
  </tr>
</table>
     </form> 
	</body>
</html>

function.php: In this file, we have details of database connection and we will fire a query to submit data to MySQL Database. Please replace the details of the database with your MYSQL server details.

<?php
session_start();
define('DB_SERVER','localhost');
define('DB_USER','root');
define('DB_PASS' ,'');
define('DB_NAME', 'myproject');
class DB_connection
{
	function __construct()
	{
$connection = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
$this->database=$connection;

	}
	public function insertdData($fname,$email,$contact,$gender,$education)
	{
	$result=mysqli_query($this->database,"insert into student(name,email,phone,gender,education) values('$fname','$email','$contact','$gender','$education')");
	return $result;
	}
} 
?>

Now create the Database with name myproject and just copy and paste the code in MySQL Database. You can change the database name as the requirement. The code below will generate the required tables and insert some dummy records. Feel free to change the table name and remove demo records.

-- phpMyAdmin SQL Dump
-- version 4.8.5
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Aug 19, 2019 at 05:21 AM
-- Server version: 10.1.38-MariaDB
-- PHP Version: 5.6.40

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `myproject`
--

-- --------------------------------------------------------

--
-- Table structure for table `demp_project`
--

CREATE TABLE `demp_project` (
  `id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `phone` varchar(255) NOT NULL,
  `link` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `demp_project`
--

INSERT INTO `demp_project` (`id`, `name`, `email`, `phone`, `link`) VALUES
(2, 'Ethan', '[email protected]', '9999999999', 'https://www.ingenioustechies.com/'),
(3, 'Amelia', '[email protected]', '9999999999', 'https://www.ingenioustechies.com/'),
(4, 'Mia', '[email protected]', '9999999999', 'https://www.ingenioustechies.com/'),
(5, 'Lucas', '[email protected]', '9999999999', 'https://www.ingenioustechies.com/'),
(6, 'Chloe', '[email protected]', '9999999999', 'https://www.ingenioustechies.com/'),
(7, 'Noah', '[email protected]', '9999999999', 'https://www.ingenioustechies.com/'),
(8, 'Enzo', '[email protected]', '9999999999', 'https://www.ingenioustechies.com/'),
(9, 'Mathias', '[email protected]', '9999999999', 'https://www.ingenioustechies.com/'),
(10, 'Jade', '[email protected]', '9999999999', 'https://www.ingenioustechies.com/'),
(11, 'Manon', '[email protected]', '9999999999', 'https://www.ingenioustechies.com/'),
(12, 'Felix', '[email protected]', '9999999999', 'https://www.ingenioustechies.com/'),
(13, 'Leon', '[email protected]', '9999999999', 'https://www.ingenioustechies.com/'),
(14, 'Leonie', '[email protected]', '9999999999', 'https://www.ingenioustechies.com/'),
(15, 'Julia', '[email protected]', '9999999999', 'https://www.ingenioustechies.com/'),
(16, 'Maximilian', '[email protected]', '9999999999', 'https://www.ingenioustechies.com/'),
(17, 'Vicente', '[email protected]', '9999999999', 'https://www.ingenioustechies.com/'),
(18, 'Valentina', '[email protected]', '9999999999', 'https://www.ingenioustechies.com/'),
(19, 'Diego', '[email protected]', '9999999999', 'https://www.ingenioustechies.com/');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `demp_project`
--
ALTER TABLE `demp_project`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `demp_project`
--
ALTER TABLE `demp_project`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=20;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

Now you are all set. Run the PHP application, fill the form and submit your data. Now go to MYSQL database and check the table demp_project to see the inserted records.

That’s it Folks on this topic. Let us know your comments on this article in the comment box below. Kindly like our facebook page, follows us on twitter and subscribe to our YouTube channel for latest updates.

Leave a Reply