PHP – MySql Connection Example

Right now, will figure out how to interface with MySql utilizing PHP. We will embed a record in MySql database utilizing PHP.

Presently you have your database prepared! It’s a great opportunity to make an HTML structure to get the values from the client. Make a record in your PHP envelope named “product.php” and compose the accompanying code in it as it is simple HTML you would have the option to comprehend the code effectively.

PHP - MySql Connection Example

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 structure is prepared we should attempt what it looks like. Run your item record in your program and you should see this way:

PHP - MySql Connection Example

Presently how about we code to embed this information into the database. To do this make a record named “productsubmit.php” and include the accompanying 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 clarifications

  • $dsn → this is a variable where we characterize our host for the present its localhost and dbname holds the database name.
  • PDO → In request to interface with the database we will utilize PDO, PDO represents PHP information object which is used to get to the database, it takes three parameter name of the database, userid of database and secret phrase.
  • $nq → this variable holds the addition question that will be processed.

This inquiry will be arranged and executed by utilizing the inbuilt functions plan() and execute(). $result will contain the outcome sent by the database. On the off chance that record will be embedded effectively, $result will be genuine else it would be bogus and we will show the message in like manner.

How about we present the record and see the outcome:

PHP - MySql Connection Example

Great work! Item is submitted to table item you can check it in the database. Investigate this cause changes right now le it to carry on as you need it to be.

Leave a Comment

error: Alert: Content is protected!!