Home » PHP

PHP - MySql Connection Example

In this article, we will learn to connect to MySql using PHP. We will insert a record in MySql datatbase using PHP.
Submitted by Jyoti Singh, on January 31, 2018

First, you need to create a schema named "Product". In this schema create a table named "product" with following fields.

product schema for PHP example

Now you have your database ready! It’s time to make an HTML form to get the values from the user. Make a file in your PHP folder named "product.php" and write the following code in it as it is just html you would be able to understand the code easily.

HTML code

<html>
	<form action="productsubmit.php">
	<center>
	<table>
		<caption>
			<b><font color="Green" size="5">Product Interface</font></b>
		</caption>
		<br><br>
		<tr>
		<td><b><i>Product Id</i></b></td>
		<td><input type="text" name="pid"></td>
		</tr>

		<tr>
		<td><b><i>Product Name</i></b></td>
		<td><input type="text" name="pn"></td>
		</tr>

		<tr>
		<td><b><i>Product Rate</i></b></td>
		<td><input type="text" name="pr"></td>
		</tr>

		<tr>
		<td><b><i>Offer Rate</i></b></td>
		<td><input type="text" name="por"></td>
		</tr>

		<tr>
		<td><b><i>Picture</i></b></td>
		<td><input type="file" name="pic"></td>
		</tr>

		<tr>
		<td><b><i>Purchase Date:</i></b></td>
		<td><input type="date" name="pd"></td>
		</tr>
		<tr>
		<td><input type="submit"></td>
		<td><input type="reset"></td>
		</tr>
	</table>
	</center>
</form>

Here your form is ready let’s try how it looks. Run your product file in your browser and you should see like this:

PHP - MySql Connection HTML form

Now let’s code to insert this data into the database. To do this make a file named "productsubmit.php" and add the following code in it.

PHP code

<?php

	$dsn="mysql:host=localhost;dbname=product";
	$cn=new PDO($dsn,"root","123");
	
	//In order to connect to the database we use PDO, 
	//PDO stands for php data object which is used to 
	//accessthe database,u need to give three parameters in it 
	//first one is database and host name second 
	//is The user id of database and password.
	$pid=$_GET['pid'];
	
	//request to get the data 
	$pn=$_GET['pn'];
	$pr=$_GET['pr'];
	$por=$_GET['por'];
	$pic=$_GET['pic'];
	$pd=$_GET['pd'];
	
	$nq="insert into products values($pid,'$pn',$pr,$por,'$pic','$pd')";

	$smt=$cn->prepare($nq);
	$result=$smt->execute();
	if($result){
		echo "Record Submitted";
	}else{
		echo "Fail to submit record";
	}
?>

Code/Variable explanations

  • $dsn → this is a variable in which we define our host for now it’s localhost and dbname holds the database name.
  • PDO → In order to connect to the database we will use PDO, PDO stands for php data object which is use to access the database , it takes three parameter name of the database , userid of database and password.
  • $nq → this variable holds the insert query that is to be process.

This query will be prepared and executed by using inbuilt functions prepare() and execute(). $result will contain the result sent by the database. If record will be insert successfully $result will be true else it would be false and we will show the message accordingly.

Let’s submit the record and see the result:

PHP - MySql Connection output

Good work! Product is submitted to table product you can check it in database. Explore this make changes in this code and le it behave as you want it to be.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.