Insert Data using PHP MySQLi
![]() |
PHP MySQLi Insert Examples |
Hi Guys in This Tutorial We will Learn How to Insert Data into MySQLi Database using PHP.
Folder and Database Structure
Database name: tutorialTable name: test;
1 2 3 4 5 6 | CREATE TABLE IF NOT EXISTS `test` ( `id` int (11) NOT NULL AUTO_INCREMENT, ` name ` varchar (150) NOT NULL , `email` varchar (150) NOT NULL , PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1; |
create 4 files under htdocs folder.
1.db_config.php
2.index.html
3.insert.php
4.style.css
Codes for db_config.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php $host = "localhost" ; //Host name $username = "root" ; //MySQL Username .'root' is default $password = "" ; //MySQL Password $db_name = "tutorial" ; //Database Name // Create connection passing host,username,password and db_name $conn = new mysqli( $host , $username , $password , $db_name ); // Checking connection if ( $conn ->connect_error) { die ( "Connection failed: " . $conn ->connect_error); } ?> |
Codes for index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | < html > < head > < title >Inserting Data Using PHP MySQLi Beginners</ title > < link rel = "stylesheet" href = "style.css" type = "text/css" /> </ head > < body > < form method = "POST" action = "insert.php" > < span >Name:</ span > < input type = "text" name = "name" placeholder = "Your Name Here" /> < span >Email:</ span > < input type = "text" name = "email" placeholder = "Your Email Here" /> < input type = "submit" name = "submit" value = "Insert" /> </ form > </ form > </ body > </ html > |
Codes for insert.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php include ( 'db_config.php' ); $name = $_POST [ 'name' ]; $email = $_POST [ 'email' ]; $sql = "INSERT INTO test (name,email) VALUES ( '$name' , '$email' )"; if ( $conn ->query( $sql ) === TRUE) { echo "New record successfully Inserted ||<a href=index.html>Insert New One</a>" ; } else { echo "Error: " . $sql . " " . $conn ->error; } $conn ->close(); ?> |
Codes for style.css
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | input[type=text] { width : 30% ; padding : 8px ; margin : 4px 0 ; box-sizing: border-box; border : 1px solid #808080 ; -webkit-transition: 0.5 s; transition: 0.5 s; font-size : 20px ; } span{ font-size : 20px ; color : red ; } input[type=text]:focus { border : 3px solid green ; } input[type=submit]{ background-color : #4CAF50 ; border : none ; color : white ; padding : 16px 32px ; cursor : pointer ; } |
Keep Visiting for More Codes.Thank you.
No comments:
Post a Comment
Thank You for Your Comment
Note: Only a member of this blog may post a comment.