PHPJavascript

Web Development Guide – My Personal Library of Tutorials and Scripts

(Modify View mysql data script) PHP Database Driven Websites – Tutorial 2c

September8

Download

Build content management system (CMS)  for our ecommerce website. Includes scripts for modifying and viewing mysql data.

Previously we had build scripts to add, delete mysql data and now we will develop scripts to modify and view products of our website.

Recap:

  • We are building a simple ecommerce dynamic website called games
  • So far we have build a database structure and CMS with login, adding and deleting mysql data script
  • Download file 72.rar. “start” folder consist of the work done previously and “final” folder consist of the complete work done in this tutorial

Extract the file and rename the folder “start” to “egames” and copy it to c:/wwwroot/inetpub. Use egames.sql for database structure.

Note:

  • The start folder comes with javascript validation built in with the php pages
  • If you face any problems please check the config.php file

Open internet explorer type http://localhost/egames/admin then type username: admin and password: password. You will be taken to the controlpanel page.
Note: I have already created add, delete, edit category script for you. The one that we will develop together will be similar so have a look at the category section to get an idea.

Lets start working ….. Open modifyproduct.php in dreamweaver or notepad.

Modify MYSQL Data Script

modify script is similar to add script. Like the adding script it consists of two pages modifyproduct.php and updateproduct.php. The main difference is in the modifyproduct.php the content is extracted from mysql table and shown on the html form unlike addproduct.php where the form field were blank. And in the updateproduct.php we use mysql “update” function while in the saveproduct.php we had used mysql “insert” function.

Here goes nuthing……

modifyproduct.php
Type <script language=”JavaScript” src=”../gen_validatorv1.js” type=”text/javascript”></script> in the <head></head> of the page

Find <!—- Listing ——> in the code and type the following:

updateproduct.php
Find <!—- Listing ——> in the code and type the following:

View MYSQL Data Script

This script is the simplest of all. From the product.php page when the user clicks on the product name, viewproduct.php is called page passing productid varable which is used to extract values from the mysql table ‘product’

viewproduct.php
Find <!—- Listing ——> in the code and type the following:

The Content Management System is done. You add/delete/modify the products and categories.

Now test all the scripts. All should be working fine if not then compare your code with the “final” folder.

Leave comments guys I need to know how you feel about the tutorial. Was it helpful or not?

Explanation

modifyproduct.php

Code Explanation
<script language="JavaScript" src="../gen_validatorv1.js" type="text/javascript"></script> Connecting javascript validation script which is placed in the main folder
<?
$productid=$_REQUEST["productid"];
$res1=mysql_query("select * from product where productid='$productid'")or die(mysql_error());
$row1=mysql_fetch_array($res1);

$rescat=mysql_query(”select * from productcategory where productid=’$productid’”)or die(mysql_error());
$rowcat=mysql_fetch_array($rescat); $productcat=$rowcat["productcat"];
?>

$productid is extracted previously from product.php page using URL passing method
Extract values from table “product” and “productcategory” to be used in html form later on where the productid match..
<form method=post name=myform action="updateproduct.php"> Used method post for passing variable
<input type="hidden" name="productid" value="<?echo $productid?>">
<input type="text" name="product" size="50">
html form elements which will used in the updateproduct.php page to update data in mysql table. Note $productid is passed as hidden form element
<select name="productcat" style=width:270px>
<option value="">Select</option>
<? $res=mysql_query("select * from category order by categoryid") or die("cannot select category");
while ($row=mysql_fetch_array($res)) {?><option value="<? echo $row["categoryid"] ?>"><? echo $row["category"];?>
</option>
<? } ?>
</select>
we are using a select box to display the product category. These categories are stored in a table “category” so here are getting the data from the table. The categoryid is compared with the productcat if it matches it is shown as selected.Note: We are displaying the category name and passing the categoryid to the next page

updateproduct.php

Code Explanation
$product=$_POST["product"];
$productcat=$_POST["productcat"];
$bdesc=$_POST["bdesc"];
$ddesc=$_POST["ddesc"];
Extracting values from addproduct.php;
Note: We are using $_POST as we used method=post in the modifyproduct.php
mysql_query("update product set product='$product',bdesc='$bdesc',ddesc='$ddesc' where productid='$productid'") or die(mysql_error());
updating the values in the table “product” where productid matches
mysql_query("update productcategory set productcat='$productcat' where productid='$productid'") or die(mysql_error());
updating the values in the table “productcategory” where productid matches

viewproduct.php

Code Explanation
$productid=$_REQUEST["productid"]; $productid value extracted from the URL passing method.
Note: check the url viewproduct.php?productid=1
$res1=mysql_query("select * from product where productid='$productid'")or die(mysql_error());
$row1=mysql_fetch_array($res1);
Seleting product values from the table ‘product’ where productid matches
Extracting the values row by row
$rescat=mysql_query("select * from productcategory where productid='$productid'") Selecting the category of the product selected from table ‘productcategory’
$res=mysql_query("select * from category where categoryid='$productcat'") The productcategory selected in the previous step is actually the “categoryid”, so we getting the name of the category in this step.
<?echo $row1["product"]?> <? echo $category;?> etc Displaying the mysql data on the webpage
posted under Development Guide

Email will not be published

Website example

Your Comment:

 

3,048 spam comments
blocked by
Akismet