The following code example shows Inserting a Record in a Database Table Using MySQLi in PHP.
<html>
<head>
<title>Inserting Data</title>
</head>
<body>
<form name="f1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table border="0">
<caption>Insert a Course Record</caption>
<tr>
<td>Enter Course Code: </td>
<td><input type="text" name="c1" /></td>
</tr>
<tr>
<td>Enter Course Name: </td>
<td><input type="text" name="c2" /></td>
</tr>
<tr>
<td>Enter Duration: </td>
<td><input type="text" name="c3" /></td>
</tr>
<tr>
<td>Enter Number of Credits: </td>
<td><input type="text" name="c4" /></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Insert"/></td>
</tr>
</table>
</form>
</body>
</html>
</html>
<?php
if(isset($_POST['submit'])){
$a=$_POST['c1'];
$b=$_POST['c2'];
$c=$_POST['c3'];
$d=$_POST['c4'];
$server_host="localhost";
$database_user="root";
$password="";
// Connect with MySQL Server
$con1=new mysqli($server_host, $database_user, $password, "institute");
// Create a query for database creation
$query="insert into course(course_id, course_name, duration, credits)values('".$a."', '".$b."', '".$c."', '".$d."')";
// Execute the query
$b=$con1->query($query);
if($b)
{
echo 'Record Inserted Successfully!';
}
else
{
echo 'Record Not Inserted: <br>',$con1->error;
}
// close the connection
$con1->close();
}
?>
Output
Further Reading
Examples of Array Functions in PHP