Insert Data in MySQL DB using PHP

This is a simple post in php. In this post you will learn how to add data to the MySQL Database. Basically in this post, a form is used which accepts input from the user. A class DBClass is used for interactions with the database.

Screenshot


The Code
The Config.php File
<?php
define("DB_NAME","sample");//Database Name
define("DB_USER","root");//Database User
define("DB_PASS","");//Database Password
define("DB_HOST","localhost");//Database Host
?>
The DBClass.php file
<?php
require_once 'config.php';
class DBClass
{
 public function addData($table,$name,$phone,$email)
 {
  $con=mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);
  $res=mysqli_query($con,"insert into $table values ('$name','$phone','$email')");
  if($res)
  {
   echo '<script>alert("Data Succesfully inserted !")</script>';
  }
  else
  {
   echo '<script>alert("Data Not Succesfully inserted !")</script>';
  }
 }
 public function showData()
 {
  $res=mysqli_query($con,'select * from $table');
  if($res)
   return mysqli_fetch_array($retval, MYSQL_ASSOC);
  else
   die('Could not connect: ' . mysql_error()); 
 }  
}
?>
The index.php File
<?php 
if(isset($_POST['submit']))
{
 require_once 'db_class.php';
 $db = new DBClass();
 $db->addData('sample',$_POST['name'],$_POST['phone'],$_POST['email']);
}
?>
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Varela+Round">
<style>
@charset "utf-8";
@import url(http://weloveiconfonts.com/api/?family=fontawesome);
@import url(http://meyerweb.com/eric/tools/css/reset/reset.css);
[class*="fontawesome-"]:before {
  font-family: 'FontAwesome', sans-serif;
}
body {
 background-color: #C0C0C0;
 color: #000;
 font-family: "Varela Round", Arial, Helvetica, sans-serif;
 font-size: 16px;
 line-height: 1.5em;
}

input {
 border: none;
 font-family: inherit;
 font-size: inherit;
 font-weight: inherit;
 line-height: inherit;
 -webkit-appearance: none;
}

/* ---------- LOGIN ---------- */

#login {
 margin: 50px auto;
 width: 400px;
}

#login h2 {
 background-color: #f95252;
 -webkit-border-radius: 20px 20px 0 0;
 -moz-border-radius: 20px 20px 0 0;
 border-radius: 20px 20px 0 0;
 color: #fff;
 font-size: 28px;
 padding: 20px 26px;
}

#login h2 span[class*="fontawesome-"] {
 margin-right: 14px;
}

#login fieldset {
 background-color: #fff;
 -webkit-border-radius: 0 0 20px 20px;
 -moz-border-radius: 0 0 20px 20px;
 border-radius: 0 0 20px 20px;
 padding: 20px 26px;
}

#login fieldset p {
 color: #777;
 margin-bottom: 14px;
}

#login fieldset p:last-child {
 margin-bottom: 0;
}

#login fieldset input {
 -webkit-border-radius: 3px;
 -moz-border-radius: 3px;
 border-radius: 3px;
}

#login fieldset input[type="text"], #login fieldset input[type="email"] {
 background-color: #eee;
 color: #777;
 padding: 4px 10px;
 width: 328px;
}

#login fieldset input[type="submit"] {
 background-color: #33cc77;
 color: #fff;
 display: block;
 margin: 0 auto;
 padding: 4px 0;
 width: 100px;
}

#login fieldset input[type="submit"]:hover {
 background-color: #28ad63;
}
</style>
<div id="login">
<h2><span class="fontawesome-lock"></span>Enter The Data</h2>
<form method="post" action="<?php $_PHP_SELF ?>" name="sample">
<fieldset>
<input type="text" placeholder="Enter the Name" name="name"/></br>
<input type="email" placeholder="Enter the Email" name="email"/></br>
<input type="text" placeholder="Enter the mobile number" name="phone"/><br/><br/>
<input type="submit" name="submit"/>
</fieldset>
</form>
</div>
That's all for this code. Got any problems, comment them !

Comments

Popular posts from this blog

Non Restoring Division Algorithm Implementation in C

Bit Stuffing Code Implementation in Java

Hackerrank Modified Kaprekar Numbers Solution