PHP - MySql Connection Example

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 [Last updated : March 13, 2023]

Create MySQL schema and a table

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.

PHP Database Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.