How to Insert Data Into MySQL Database Table Using PHP?

Today, We want to share with you insert data in mysql using php form.In this post we will show you how to insert data in mysql using php?, hear for how we can update and delete data using Delete and Update statements. we will give you demo and example for implement.In this post, we will learn about How to Insert and Retrieve Data in Database? with an example.

how to insert multiple rows in mysql using php form?

For insert data in MySQL first i have to make a table in mysql data base.

Here we using 3 main useful file for insert data in MySQL:

  • database.php:For connecting data base in Mysql
  • do_insert_product.php:for getting the values from the user
  • save_products_data.php:A PHP file that all products save the request
Read Also:  PHP MySQL Dynamic SQL Insert Query

database Structure

CREATE TABLE `products` (
	`userid` int(8) NOT NULL,
	`product_code` varchar(55) NOT NULL,
	`product_name` varchar(55) NOT NULL,
	`product_desc` varchar(55) NOT NULL,
	`price` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

database.php

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

do_insert_product.php

<!DOCTYPE html>
<html>
  <body>
	<form method="post" action="save_products_data.php">
		Product Code:<br>
		<input type="text" name="product_code">
		<br>
		Product Name:<br>
		<input type="text" name="product_name">
		<br>
		Product Desc:<br>
		<input type="text" name="product_desc">
		<br>
		Product Price:<br>
		<input type="text" name="price">
		<br><br>
		<input type="submit" name="save" value="submit">
	</form>
  </body>
</html>

save_products_data.php

<?php
include_once 'database.php';
if(isset($_POST['save']))
{	 
	 $product_code = $_POST['product_code'];
	 $product_name = $_POST['product_name'];
	 $product_desc = $_POST['product_desc'];
	 $price = $_POST['price'];
	 $sql = "INSERT INTO products (product_code,product_name,product_desc,price)
	 VALUES ('$product_code','$product_name','$product_desc','$price')";
	 if (mysqli_query($conn, $sql)) {
		echo "New record created successfully !";
	 } else {
		echo "Error: " . $sql . "
" . mysqli_error($conn);
	 }
	 mysqli_close($conn);
}
?>

Alternative Code in (PDO)

<?php
$servername='localhost';
$username='root';
$password='DFDg^&*8979';
$dbname = "products_list";
try {
    $product_code = $_POST['product_code'];
    $product_name = $_POST['product_name'];
    $product_desc = $_POST['product_desc'];
    $price = $_POST['price'];
    date_default_timezone_set("Asia/Calcutta");
    $insertdate = date("Y-m-d H:i:s");
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
   $sql = "INSERT INTO products (product_code,product_name,product_desc,price,datetime)
    VALUES ('$product_code', '$product_name','$product_desc','$price','$insertdate')";
    $conn->exec($sql);
    echo "New record created successfully";
    }
catch(PDOException $e)
    {

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

$conn = null;
?>

I hope you get an idea about insert data in mysql using php api.
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.