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