Insert Data Into Database Using PHP and MySQLi

0
265

This time, I will teach you how to Insert Data Into Database Using PHP and MySQLi. In this tutorial it will demonstrate to you on how to Insert Data Into Database with the use of insert query statement in PHP and MySQLi.

Below are following step by step guide on how to Insert Data Into Database Using PHP and MySQLi.

Lets Begin:

First Step: Create a MySQL Database and name it “userdb“.

Second Step: Do the following code for creating a table in the database that you have created.

  1. CREATE TABLE `userdb`.`tbluser` (
  2. `Name` VARCHAR( 30 ) NOT NULL ,
  3. `Username` VARCHAR( 30 ) NOT NULL ,
  4. `Pass` VARCHAR( 90 ) NOT NULL

Third Step: Create a .php file and name it “index.php“.

Fourth Step: Do the following code for the html view of a form.

  1. <html>
  2. <title>Insert Data Into Database Using PHP and MySQLi</title>
  3. <head>
  4. <h1 align="center">Add New User</h1>
  5. </head>
  6. <body>
  7. <div id="box">
  8. <div class="column"></div>
  9. <div class="column"><form id="main" class="form" action="index.php " method="POST"><label class="first-column ">Name:</label>
  10. <div class="second-column"></div>
  11. <div class="clear"></div>
  12. <label class="first-column ">Username:</label>
  13. <div class="second-column"></div>
  14. <div class="clear"></div>
  15. <label class="first-column ">Password:</label>
  16. <div class="second-column"></div>
  17. <div class="clear"></div>
  18. <div class="first-column "></div>
  19. <div class="second-column"><button class="btn" name="save" type="submit">Save</button></div>
  20.  
  21. </form>
  22. </div>
  23. <div class="column"></div>
  24. </div>
  25. </body>
  26. </html>

Fifth Step: Create a CSS Style for the layout of the form.

  1. #box{
  2. width: 100%;
  3. }
  4. .column {
  5. width: 33.33%;
  6. display: inline-block;
  7. }
  8. .form{
  9. padding: 2px;
  10. width: 100%;
  11. display: inline-block;
  12. }
  13. .first-column {
  14. display: inline-block;
  15. width: 100px;
  16. height: 2px;
  17. margin: 2px;
  18. position: inherit;
  19. }
  20. .second-column{
  21. display: inline-block;
  22. width: 150px;
  23. height: 25px;
  24. margin: 1px;
  25. padding: 0px;
  26. position: inherit;
  27. }
  28. .btn{
  29. width: 50px;
  30. height: 40px;
  31. margin: 2px;
  32. float: left;
  33. }
  34. .c{
  35. c: left;
  36. height: 15px;
  37. }
Sixth Step: Create a script for inserting data into database.

  1. <?php
  2. $server="localhost";
  3. $userid ="root";
  4. $Password = "";
  5. $myDB = "userdb";
  6. $con = mysqli_connect($server,$userid,$Password,$myDB);
  7. # code...
  8. echo "Failed to connect to MySQL: " . mysqli_connect_error();
  9. }
  10. if (isset($_POST['save'])) {
  11. # code...
  12. $name = $_POST['name'];
  13. $username = $_POST['username'];
  14. $pass = $_POST['pass'];
  15. $sqli = "INSERT INTO `tbluser` (`UserID`, `Name`, `Username`, `Pass`) VALUES (null,'{$name}','{$username}','{$pass}')";
  16. $res = mysqli_query($con,$sqli);
  17. if ($res) {
  18. # code...
  19. echo "New Data has been save in the database.";
  20. }
  21.  
  22. }
  23. ?>

If you have any questions or suggestion about how to Insert Data Into Database Using PHP and MySQLi, please feel free to contact us at our contact page.You can subscribe this site to see more of my tutorials.

You can also view these tutorials below:

Facebook Comments
(Visited 242 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here