Crewow is a Blog contains Free Web Training's, Tutorials, Resources and much more. It will feature best Tutorials related Jquery, PHP, Ajax, CSS and other Web development languages. It will also tell you the sites from where you can get best web resources. Creatively Wow!
Thursday, 5 December 2013
PHP Simple Insert using PDO in Bootstrap Tutorial
PHP Simple Insert using PDO in Bootstrap is a tutorial in which we will learn how to insert data in database using PDO (PHP Data Objects) prepared statements while using Bootstrap CSS Framework. In Demo we will create database and then connect MySQL database using PDO (PHP Data Objects) and then use form to insert data in database and then show it using prepared select queries. Demo also contain UPDATE, DELETE and SELECT options which will be cover in next tutorials.
Modal will be open when Insert Button updates which will display below form for Addition of Fruit rates. Form code is in footer.php page and its a included in AdminIndex.php. This form contains fruit names in select option and rate field for adding fruit details. When we press Add Rate Button it will send values as POST method to FruitMy.php.
Before explaining FruitMy.php which covers prepared insert query we will create sample table in our database using sql.txt file which contains following table create queries and sample data for demo and test.
fruitsratelist table contains fields like Fruitname and its Rate.
CREATE TABLE IF NOT EXISTS `fruitsratelist` (
`Fruitname` varchar(15) NOT NULL,
`Rate` double NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
configPDO.php file use a PDO (PHP Data Objects) for connecting MySQL Database.
// mysql hostname
$hostname = 'localhost';
// mysql username
$username = 'root';
// mysql password
$password = '';
// Database Connection using PDO
$dbh = new PDO("mysql:host=$hostname;dbname=YourDatabaseName", $username, $password);
Now we will disucess our FruitMy.php which is used for getting values from from and then insert these in database using PDO query.
// We will include connection file first
// check if variable is set and Add Rate Button pressed.
// Define Variables
$Fruitname = $_POST[Fruitname]; //Fruitname
$a1 = $_POST[a1]; //Rate// We Will prepare SQL Query
$STM = $dbh->prepare("INSERT INTO fruitsratelist(Fruitname, Rate) VALUES (:Fruitname,:a1)");
// bind paramenters, Named parameters always start with colon(:)
// For Executing prepared statement we will use below function
// We use header here for redirecting it to other page where we will show success message.
Note: We can use 3 various methods to insert data into MySQL database using PDO Queries.
In Demo we used named parameters key with prepared statement. Binding parameter is must before execution using this method.
We used below tools and technologies for developing PHP Simple Insert using PDO in Bootstrap Tutorial.
PHP as Server Side Scripting Language
MySQL as a Database.
Bootstrap as CSS Framework.
JQuery as a service.
jqBootstrapValidation for validating sign-in form.
phpMyAdmin for creating database and tables.
Sublime Text 2+ as a code editor.
XAMPP as a package for Apache web server, PHP and MySQL.
Google Chrome as a browser for testing.
Thanks for reading. Enjoy and share with friends.
We hope you will find PHP Simple Insert using PDO in Bootstrap Tutorial very helpful and easy. PDO (PHP Data Objects) Prepared statements used in this tutorial along with Bootstrap CSS Framework for better user experience. For any query/suggestions please post a comment on our Facebook Page.