How to insert data in Database using PHP and MySQLi | Part 1
In this tutorial,I will show you how to insert data in database using PHP. It is not so difficult, So as you know that data can be of any type text,audio,video and images .But in this tutorial, I will only insert text data and others types of data are inserted in some different way.
So data is inserted using MySQL insert query and save my data into into MySQL database.
Steps to Insert data
- Create crud folder in www folder.Inside it we create two files insert.php and config.php.
- First create database `student` and table `data` in wamp software. Table `data` have four column -
id | name | class | marks |
Make sure column id should be primary and auto increment.
- config.php
<?php $con= mysqli_connect('localhost','root','','student'); ?>
- insert.php
<?php include('config.php'); ?>
<html>
<title>
insert data in database
</title>
<body>
<form method="post" action="">
Name<input type="text" name="name">
class<input type="text" name="class">
marks<input type="text" name="marks">
<input type="submit" name="submit">
</form>
</body>
</html>
<?php
if(isset($_POST['submit']))
{
$name=$_POST['name'];
$class=$_POST['class'];
$marks=$_POST['marks'];
$query1="INSERT INTO `data`(name,class,marks) VALUES('$name','$class','$marks')";
if(mysqli_query($con,$query1))
{
echo "data is inserted";
}
else
{
echo "data is not inserted";
}
{[['
']]}
No comments