Here is the PHP code to insert new record in MYSQL database! 

Img1

Example

 

<form action="insert.php" method="post">

<?php
$r1="";
$r2="";
$n1="";
$c1="";
$s1="";$s2="";$s3="";
$apperr="";
$flag=0;
if(isset($_POST["save"]))
{
    if(empty($_POST["roll"]))
	{
		$r1="Roll No. Missing";
		$flag=0;
		}
		
		else if(is_numeric(trim($_POST["roll"]))==false)
		{
			$r2="Accept Numeric value only";
			$flag=0;
		}
		
		else{
		
		$s1=$_POST["roll"];
		$flag=1;
		
		}
		
		 if(empty($_POST["name"]))
		{
		$n1="Name Missing";
		$flag=0;
		}
		else
		{
			
		$s2=$_POST["name"];
		$flag=1;
		}
		
		  if(empty($_POST["course"]))
		{
		$c1="Course Missing";
		$flag=0;
		}
		else
		{
			
		$s3=$_POST["course"];
		$flag=1;
		}
}
if($flag!=0)
{
	$server='localhost';
	$username='root';
	$password='';
	$cnt=0;
	try
	{
		$db=new PDO("mysql: host=$server; dbname=softaid",$username, $password);
		$count=$db->exec("Insert into  student ( rollno, sname,scourse)values('$s1','$s2', '$s3')");
		if($count>0)
		{
			$apperr="<span style='color:green'>Insert Successfully</span>";
		}
		else
		{
		$apperr="<span style='color:red'>Roll No. is already exiest</span>";
		}
		$db=null;
		
	}
	catch(PDOException $e)
	{
		
		echo $e->getMessage();
	}
}
		
?>


<div style="width:600px; padding:13px 0px; border-radius:30px 30px 0 0;
 background-color:#00A7CA; color:#FFF; margin:auto; 
font-size:20px;font-family:Tahoma, Geneva, sans-serif; text-align:center;">Insert Student Details</div>
<table  cellspacing="10" style="width:600px; margin:auto; font-family:Tahoma, Geneva, sans-serif; color:#666; background-color:#FF9">
<tr>
<td>Enter Roll No.:</td>
<td><input type="text" name="roll" class="txt"  placeholder="<?php echo $r1; echo $r2; ?>"/></td>
<td><span class="error"> 
</span>
  </td>
</tr>
<tr>
<td>Enter Name:</td>
<td><input type="text" name="name" class="txt" placeholder="<?php echo $n1; ?>" /></td>
<td><span class="error"> 
</span>
  </td>
</tr>
<tr>
<td>Enter Course:</td>
<td><input type="text" name="course" class="txt" placeholder="<?php echo $c1; ?>" /></td>
<td><span class="error"> 
</span>
  </td>
</tr>

<tr>
<td colspan="2" align="right"> 
<?php echo $apperr; ?> &nbsp <input  type="submit" name="save" value="Insert" style=" padding:6px 20px; font-size:18px; border-radius:5px; "/></td>
</tr>
</table>
<div style="width:600px; padding:20px 0px; border-radius:0px 0px 20px 20px; background-color:#00A7CA; color:#FFF; margin:auto; font-size:18px; text-align:center;"></div>
</form>

Output is:

load the page! 

Capture

 

Press the insert button above seen.

ins

now check your database!

sodat