SQL & PHP Tutorial

Update mysql database using php


In this tutorial you'll learn how to update the records in a SQL table using PHP.


SQL tutorial



Click here to insert data using Forms

Update mysql table tutorial


I’m going to create 2 files
File 1: update-form.htm
Let's create an HTML form that can be used to update records into the Product table

		  
< html> < head> < Title>Update Form< /Title> < head> < body> < 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 “update-sql-data.php”
so that the data being entered in the textboxes can then be update and saved to the database in the “update-sql-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="Update">

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="Update"> < /form>

Execute the file 1: Update-form.htm

update-form

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:
update-sql-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 Query statement to Update data into the Table I’m going to Update “ Price ”

update-price-data-into-table
   $sql = "UPDATE Product SET Price = '$Price'  WHERE ProductID = '$ProductID' ";

UPDATE Product SET Price = '2580$' WHERE ProductID = '111'

Fill the form

update-form-filled

Click Update Button
after clicking the update button data processing start like this
When the user fills out the form above and clicks the Update 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 Update Data Variable $ProductID gets value 111, Variable $Price gets value 2580$, and then search the table "Product" using SQL query
" $sql = "UPDATE Product SET Price = '$Price', ProductDescription = '$ProductDescription' WHERE ProductID = '$ProductID'";
if Record found then Update Product Table column Price = 2580$;

update-form-process

After updating Price Value in the Table look like this

update-price-2580$

finally check data updated successfully or not


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

Records Updated successfully.

updated-table

Congratulation its Working 100%

How data process works

how-data-process-works

If you want update multiple record then add comma between columns
Execute the following Query statement to Update Multiple column into the Table
I’m going to Update “ Price ” and " ProductDescription ".

$sql = "UPDATE Product SET Price = '$Price', ProductDescription = '$ProductDescription' WHERE ProductID = '$ProductID'";
update-multiple-records

after Executing the following Query statement, updated two columns, “ Price ” and " ProductDescription

record-updated-successfully.png

Writing all php code together

  < ?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 = "UPDATE Product SET Price = '$Price', ProductDescription = '$ProductDescription' WHERE ProductID = '$ProductID'";  
	
	 if ($result = mysql_query($sql, $cn)){ 
   echo "Records updated successfully.";
   }
   else
       { echo "ERROR: Could not able to execute $sql. "; }
	   
	mysql_close($cn);
	   
 ? >