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
Also Read This πŸ‘‰   PHP Google Multi factor authentication Tutorial With Example

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

For connecting data base


$password='[email protected]';
$dbname = "pakainfo_v1";
   die('Could not Connect My Sql:' .mysql_error());

for getting the values from the member


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

Free Live Chat for Any Issue

A PHP file that process the request


include_once 'db_config.php';
	 $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);

Alternative Code in (PDO)

$dbname = "pakainfo_v1";
try {
    $member_fname = $_POST['member_fname'];
    $member_lname = $_POST['member_lname'];
    $address = $_POST['address'];
    $email = $_POST['email'];
    $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 */
   $sql = "INSERT INTO member (member_fname,member_lname,address,email,datetime)
    VALUES ('$member_fname', '$member_lname','$address','$email','$insertdate')";
    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 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.