Here your form is ready let’s try how it looks. Run your product file in your browser and you should see like this:
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:
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.
HTML code
Here your form is ready let’s try how it looks. Run your product file in your browser and you should see like this:
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
Code/Variable explanations
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:
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