insert data into mysql table using php form


In this tutorial you'll learn how to Insert Data in MySQL table using PHP.


SQL tutorial



Click Here to Create SQL Table

Insert Data into mysql table


I’m going to create 2 files
File 1: add-record-form.htm

		  
< html> < head> < Title>Add Record Form< /Title> < head> < Form>

We also need to make sure that the form method attribute is “post”
so as to access the data being entered in a reliable way in the next page being directed “insert-data.php”
so that the data being entered in the textboxes can then be saved to the database in the “insert-data.php” page.

< form action="insert-data-php" method="Post">
< input type=“text” name=“ProductID” id=“ProductID”>

The < input> element is the most important form element. defines a one-line input field for text input:

< input type=“text” name=“ProductName” id=“ProductName”>

< input type=“text” name=“Price” id=“Price”>

< input type=“text” name=“Price” id=“Price”>

< input type="submit" value="Submit">

Note: The form itself is not visible you can test it now. But I’m going to add some description

< form action="insert-data-php" method="Post">
Product_ID: 
< input type=“text” name=“ProductID” id=“ProductID”>
Product_Name:
< input type=“text” name=“ProductName” id=“ProductName”>
Product_Price:
< input type=“text” name=“Price” id=“Price”>
Product_Description:
< input type=“text” name=“Price” id=“Price”>


< input type="submit" value="Submit"> < /form>

Execute the file 1: add-record-form.htm

html-form-using-php

This is how the HTML code above will be displayed in a browser.
Our HTML Form file is complete, now I want to complete PHP file

< /form>		  
< /body>

< /html>		

Creating File 2:
Insert-data.php

< ?php>
$ProductID = $_POST['ProductID']; 
? >

we are geting ProductID from HTML FORM using php script.

get-productID-using-php

< ?php>
$ProductID = $_POST['ProductID']; 
$ProductName = $_POST['ProductName'];
$Price = $_POST['Price']; 
$ProductDescription = $_POST['ProductDescription']; 
? >

//Connect to the MySql database


   $cn = mysql_connect('localhost', ‘client', ‘Client123') or
        die('Unable to connect to server');
		
   mysql_select_db("shaigle_test", $cn) or	
      die(mysql_error($cn));

Execute the following statement to insert Form data into the Table

    $sql = 'INSERT INTO Product (ProductID, ProductName, Price, ProductDescription) 

  VALUES

 ("' . $ProductID . '", "' . $ProductName . '", "' . $Price . '", "' . $ProductDescription . '")'; 

Fill the form

fill-form

Click Submit Button
after clicking the Submit button data processing like this
When the user fills out the form above and clicks the submit button, the form data is sent for processing to a PHP file named "insert-data.php". The form data is sent with the HTTP POST method.
After Executing the following statement to insert Form data into the Table Variable $ProductID gets value 111, then it puts into the table "Product" using SQL query "INSER INTO Product"

data-process-html-form-to-php

finally check data intered successfully or not


if ($result = mysql_query($sql, $cn)) {   
 echo "Records added successfully.";
} else {    
echo "ERROR: Could not able to execute $sql. "; }

Records added successfully.

insert-form-data-into-table

Congratulation its Working 100%



Put All code Togather

 < ?php>
   $ProductID = $_POST['ProductID']; 
   $ProductName = $_POST['ProductName'];
   $Price = $_POST['Price']; 
   $ProductDescription = $_POST['ProductDescription']; 

  $cn = mysql_connect('localhost', ‘client', ‘Client123') or
        die('Unable to connect to server');
		
   mysql_select_db("shaigle_test", $cn) or	
      die(mysql_error($cn));
      
  $sql = 'INSERT INTO Product (ProductID, ProductName, Price, ProductDescription) 

  VALUES

 ("' . $ProductID . '", "' . $ProductName . '", "' . $Price . '", "' . $ProductDescription . '")'; 
 
 if ($result = mysql_query($sql, $cn)) {   
 echo "Records added successfully.";
 } 
  else {    
 echo "ERROR: Could not able to execute $sql. "; }

 ? >



message-and-ideas

Message and Feedback


: (11:33pm:10/05/18)


: (10:50pm:24/04/18)


Julia Toeskina: (4:18pm:23/04/18) Hello, Shaikof! I am working the translation agency and we are specializing mostly in EN>RU>EN and DE>EN, but we have all capacities to deliver other languages also, so maybe you have some connections with companies or people who need good qualified translations, we can do a lot of words fast. Have a good day!


khan: (6:06am:15/03/18) Dear all: first singn in then enter your message then click to send button to send your message thanks


Ejaz Baloch: (9:11am:02/11/17) Are you teaching computer? We are ready to join you.


Khan: (3:36pm:31/10/17) Shaigle site is very informative, i like this site and kindly update news every day


shaigle: (2:45pm:31/10/17) welcome to our Website, write your message and send your feed back