Insert record into MySQL
In this tutorial I will help you how to insert record into database using MySQl and MySqli in PHP. Insert data from your form page to MySLQ table in PHP programming language. At first you need to connect database in your page then start insert record work. There are two type insert record I will give you with an example and source code.
Connect database or call the database connection page in the insert page, where you write your insert query. That’s like
<?php require_once('db_connect.php'); ?>
OR
<?php include "db_connect.php"; ?>
Insert Record Using Mysql
First Create database and create a MySQL table where you insert record and give some field name which match your form text field. Then store form data in a variable, that’s like
$_REQUIRE[‘name’];. If you want to set method in your form, then you can use method GET and POST method, then you store data in variable using $_GET[‘name’] and $_POST[‘name’]. But you use the POST method for insert record into database. Follow the insert query code:
Example:
<?php if(isset($_POST['submit'])){ $name=$_POST['name']; $email=$_POST['email']; $pwd=$_POST['password']; $phone=$_POST['phone']; mysql_query("INSERT INTO `Table name`(`name`,`email`,`password`,`phone`)VALUES('$name','$email','$pwd','$phone')"); } ?>
Insert Record Using Mysqli
It is the easy way to insert record into database in PHP using MySQLi. The MySQL connection was used before PHP 5 but now later Mysqli useing for database connection.
Example1:
<?php if(isset($_POST['submit'])){ $name=$_POST['name']; $email=$_POST['email']; $pwd=$_POST['password']; $phone=$_POST['phone']; mysqli_query($mysqli,"INSERT INTO `table name`(`name`,`email`,`password`,`phone`)VALUES('$name','$email','$pwd','$phone')"); } ?>
Example2:
<?php if(isset($_POST['submit'])){ $sql=mysqli_query($mysqli,"INSERT INTO `table name`(`name`,`email`,`password`,`phone`)VALUES('".$_POST['name']."','".$_POST['email']."','".$_POST['password']."','".$_POST['phone']."')"); if($sql){ echo "success"; }else{ echo "error"; } } ?>