Posted inphp / Mysql / Mysqli

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

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


do_insert_product.php



  
	
Product Code:

Product Name:

Product Desc:

Product Price:


save_products_data.php


Alternative Code in (PDO)

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 . "
" . $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.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

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

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype