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;
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
<?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
<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><br/> <input type="text" name="name" placeholder="Your Name Here"/><br/> <span>Email:</span><br/> <input type="text" name="email" placeholder="Your Email Here"/><br/> <input type="submit" name="submit" value="Insert"/> </form> </form> </body> </html>
Codes for insert.php
<?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 . "<br>" . $conn->error; } $conn->close(); ?>
Codes for style.css
input[type=text] { width: 30%; padding: 8px; margin: 4px 0; box-sizing: border-box; border: 1px solid #808080; -webkit-transition: 0.5s; transition: 0.5s; 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; }If you Like This article Please Like and Share With Your Friends.
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.