Q:

PHP - MySql Connection Example

belongs to collection: PHP Database Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

<< PHP Code to Insert Data in MySql...