Insert Data into MySQL Database – onlinecode

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.

  1. <title>Insert Data into <span>MySQL</span><span></span>Database</<span><!--title<span>></span></span>
  2. <?php
  3. if(isset($_POST['insert'])){
  4. $servername ="localhost";
  5. $username ="username";
  6. $password ="password";
  7. $dbname ="myDB";
  8. $conn =mysqli_connect($servername, $username, $password, $dbname);
  9. if(! $conn ){
  10. die('Could not connect: '.mysqli_connect_error());
  11. }
  12. if(!get_magic_quotes_gpc()){
  13. $name =addslashes($_POST['name']);
  14. $address =addslashes($_POST['address']);
  15. }else{
  16. $name = $_POST['name'];
  17. $address = $_POST['address'];
  18. }
  19. $salary = $_POST['salary'];
  20. $sql ="INSERT INTO employee (name,address, salary,
  21. join_date) VALUES('$name','$address',$salary, NOW())";
  22. if(mysqli_query($conn, $sql)){
  23. echo "New record created successfully";
  24. }else{
  25. echo "Error: ". $sql ."".mysqli_error($conn);
  26. }
  27. mysqli_close($conn);
  28. }else{
  29. ?>
  30. <formmethod="post" action="">
  31. <tablewidth="400" border="0" cellspacing="1" cellpadding="2">
  32. <tbody>
  33. <tr>
  34.    <tdwidth="100">Name</<!--td>
  35.    <td><inputname="name" type="text" id="name"></<!--<!--td>
  36. </<!--tr>
  37. <tr>
  38.   <tdwidth="100">Address</<!--<!--td>
  39. <td><inputname="address" type="text" id="address"></<!--<!--td>
  40. <!--<!--tr>
  41. <tr>
  42.   <tdwidth="100">Salary<!--<!--td>
  43. <td><inputname="salary" type="text" id="salary"></<!--<!--td>
  44. </tr>
  45. <tr>
  46. <tdwidth="100"> </<!--<!--td>
  47. <td> </<!--<!--td>
  48. </<!--<!--tr>
  49. <tr>
  50. <tdwidth="100"> </<!--<!--td>
  51. <td><inputname="insert" type="submit" id="insert" value="Add Data"></<!--<!--td>
  52. </<!--tr>
  53.     </tbody>
  54. </<!--table>
  55.     </<!--form>
  56. <?<!--?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 {            ?>                           
Name
Address
Salary

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

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  45  =  47

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US