Insert Data into MySQL Database – onlinecode
In this post we will give you information about Insert Data into MySQL Database – onlinecode. Hear we will give you detail about Insert Data into MySQL Database – onlinecodeAnd how to use it also give you demo for it if it is necessary.
Insert Data into MySQL Database
SQL INSERT INTO statement are used to insert record in MySQL tables.
Let’s write a MySQL query to insert data by using insert into statement, after this we will execute this insert query through passing it to mysqli_query() method to store data in MySQL table.
See the example which store the record in MySQL table.
Insert Record Into a Database From an HTML Form
Try out this example by putting this code in insert_employee.php, A html form is displayed which hold the values from your side and then it will store the data in MySQL table by clicking submit button.
- <title>Insert Data into MySQLDatabase</<!--title>
- <?php
- if(isset($_POST['insert'])){
- $servername ="localhost";
- $username ="username";
- $password ="password";
- $dbname ="myDB";
- $conn =mysqli_connect($servername, $username, $password, $dbname);
- if(! $conn ){
- die('Could not connect: '.mysqli_connect_error());
- }
- if(!get_magic_quotes_gpc()){
- $name =addslashes($_POST['name']);
- $address =addslashes($_POST['address']);
- }else{
- $name = $_POST['name'];
- $address = $_POST['address'];
- }
- $salary = $_POST['salary'];
- $sql ="INSERT INTO employee (name,address, salary,
- join_date) VALUES('$name','$address',$salary, NOW())";
- if(mysqli_query($conn, $sql)){
- echo "New record created successfully";
- }else{
- echo "Error: ". $sql ."".mysqli_error($conn);
- }
- mysqli_close($conn);
- }else{
- ?>
- <formmethod="post" action="">
- <tablewidth="400" border="0" cellspacing="1" cellpadding="2">
- <tbody>
- <tr>
- <tdwidth="100">Name</<!--td>
- <td><inputname="name" type="text" id="name"></<!--<!--td>
- </<!--tr>
- <tr>
- <tdwidth="100">Address</<!--<!--td>
- <td><inputname="address" type="text" id="address"></<!--<!--td>
- <!--<!--tr>
- <tr>
- <tdwidth="100">Salary<!--<!--td>
- <td><inputname="salary" type="text" id="salary"></<!--<!--td>
- </tr>
- <tr>
- <tdwidth="100"> </<!--<!--td>
- <td> </<!--<!--td>
- </<!--<!--tr>
- <tr>
- <tdwidth="100"> </<!--<!--td>
- <td><inputname="insert" type="submit" id="insert" value="Add Data"></<!--<!--td>
- </<!--tr>
- </tbody>
- </<!--table>
- </<!--form>
- <?<!--?php }?>
title>Insert Data into MySQL Database <!--?php if(isset($_POST['insert'])) { $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; $conn =mysqli_connect($servername, $username, $password, $dbname); if(! $conn ) { die('Could not connect: ' . mysqli_connect_error()); } if(! get_magic_quotes_gpc() ) { $name = addslashes ($_POST['name']); $address = addslashes ($_POST['address']); }else { $name = $_POST['name']; $address = $_POST['address']; } $salary = $_POST['salary']; $sql = "INSERT INTO employee (name,address, salary, join_date) VALUES('$name','$address',$salary, NOW())"; if (mysqli_query($conn, $sql)) { echo "New record created successfully"; } else { echo "Error: " . $sql . "" . mysqli_error($conn); } mysqli_close($conn); }else { ?>
Hope this code and post will helped you for implement Insert Data into MySQL Database – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs
For More Info See :: laravel And github