PHP Tutorial

Define PHP

PHP Installation

PHP INI File

PHP Comment

PHP Case Sensitivity

PHP Variable, DataType

PHP Echo & Print

PHP Operators

PHP Receiving Input

Decision Making - if...else

PHP Switch Case

PHP Loops

PHP Jumping Statement

PHP Image Gallery

PHP File Upload

PHP Arrays

PHP Date Functions

PHP String Functions

PHP Math Functions

PHP Functions

PHP Variable Scope

PHP Constant Variable

PHP Superglobals

PHP Form Validation

PHP Include Statement

PHP Filter

PHP File Handling

PHP Cookies

PHP Session

PHP Send Emails

PHP Captcha

PHP MySQL Select

PHP MySQL Insert

PHP MySQL Delete

PHP MySQL Update

PHP MySQL Injection

PHP Assignment

Page Stats

Visitor: 333

PHP MySql Insert

In PHP, we can execute a insert MySql statement to insert new record in the database table. First we need to create a MySql connection, write the sql insert query, execute the query to insert the record.

Example 1: Create a MySql connection, execute the query and insert new record in the MySql table.

<form action="" method="">
	Address <input type="text" name="address" ><br>
	Name <input type="text" name="name" ><br>
	<input name="submit" type="submit" value="insert">
</form>

<?php
	
   if(isset($_GET['submit']))
   {
      $uid = mysqli_real_escape_string($conn, $_GET['address']);
      
      $conn = mysqli_connect("localhost", "root" , "", "student");
      if($conn === false){
         die("ERROR: Could not connect to database.");
   }
   
   $sql = "insert into student.add (Address, Name) values('".$uid."', '".$uname."')";
   
   $result = mysqli_query($conn, $sql);
   if($result==1)
      echo "Record inserted";
   else
      echo "Unable to insert record";
   mysqli_close($conn);
}
?>
Advertisement