PHP MySQL Insert Data – How to insert data dynamically in MySQL using PHP? | php my sql insert

php my sql insert – Insert Data Into MySQL Using MySQLi and PDO – Inserting Data into a MySQL Database Table. This article displays you how to use the MySQL INSERT INTO SELECT statement to insert data into a table with example.

php my sql insert – PHP MySQL Insert Data

php my sql insert – PHP MySQL INSERT Query For insert data in MySQL first i have to make a table in data base. The INSERT INTO statement is used to insert new rows in a database table.

Here i using 3 main file for insert data in MySQL:

  • db_config.php:For connecting data base
  • insert.php:for getting the values from the member
  • do_submit.php:A PHP file that process the request
Read Also:  PHP Check if Multiple Array contains all Array

Insert Data Into MySQL Using PHP

CREATE TABLE `member` (
	`memberid` int(8) NOT NULL,
	`member_fname` varchar(55) NOT NULL,
	`member_lname` varchar(55) NOT NULL,
	`address` varchar(55) NOT NULL,
	`email` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

For connecting data base

db_config.php

<?php
$servername='localhost';
$username='root';
$password='[email protected]';
$dbname = "pakainfo_v1";
$link=mysqli_connect($servername,$username,$password,"$dbname");
if(!$link){
   die('Could not Connect My Sql:' .mysql_error());
}
?>

for getting the values from the member

insert.php

<!DOCTYPE html>
<html>
  <body>
	<form method="post" action="do_submit.php">
		First name:<br>
		<input type="text" name="member_fname">
		<br>
		Last name:<br>
		<input type="text" name="member_lname">
		<br>
		Address:<br>
		<input type="text" name="address">
		<br>
		Email Id:<br>
		<input type="email" name="email">
		<br><br>
		<input type="submit" name="save" value="submit">
	</form>
  </body>
</html>

A PHP file that process the request

do_submit.php

<?php
include_once 'db_config.php';
if(isset($_POST['save']))
{	 
	 $member_fname = $_POST['member_fname'];
	 $member_lname = $_POST['member_lname'];
	 $address = $_POST['address'];
	 $email = $_POST['email'];
	 $sql = "INSERT INTO member (member_fname,member_lname,address,email)
	 VALUES ('$member_fname','$member_lname','$address','$email')";
	 if (mysqli_query($link, $sql)) {
		echo "New member created successfully !";
	 } else {
		echo "Error: " . $sql . "
" . mysqli_error($link);
	 }
	 mysqli_close($link);
}
?>

Alternative Code in (PDO)

<?php
$servername='localhost';
$username='root';
$password='';
$dbname = "pakainfo_v1";
try {
    $member_fname = $_POST['member_fname'];
    $member_lname = $_POST['member_lname'];
    $address = $_POST['address'];
    $email = $_POST['email'];
    date_default_timezone_set("Asia/Calcutta");
    $insertdate = date("Y-m-d H:i:s");
    $link = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    /* set the PDO error mode to exception */
    $link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
   $sql = "INSERT INTO member (member_fname,member_lname,address,email,datetime)
    VALUES ('$member_fname', '$member_lname','$address','$email','$insertdate')";
    $link->exec($sql);
    echo "New member created successfully";
    }
catch(PDOException $e)
    {

    	echo $sql . "<br>" . $e->getMessage();
    }

$link = null;
?>

Also Read: CRUD in PHP

I hope you get an idea about php my sql insert.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.