Browse DevX
Sign up for e-mail newsletters from DevX


Build an Online Store Today with PayPal, PHP, and MySQL

The days of hiring high-priced specialists to build Web storefronts are gone. Build your own online Web store including inventory management using open source PHP and MySQL. Start selling now!




Building the Right Environment to Support AI, Machine Learning and Deep Learning

nce upon a time building a Web front to a store was an enormous task, involving expensive specialists. However, over time, technologies have developed that allow anybody to build their own store, and handle transaction fulfillment through the well-known, open access payment system of PayPal. In this article, you'll build a fictional store that stocks and sells sportswear. You'll need a PHP/MySQL-enabled Web server or site. If you don't have one, don't worry—all the details for building your own are in this article. If you don't know any PHP or MySQL, the same article is also a good place to start.

To build the store, you need a database that contains information about the products that you have for sale and their current stock levels.

To build the table for products, use this SQL code:

CREATE TABLE `products` ( `id` int(11) NOT NULL auto_increment, `description` varchar(100) NOT NULL default '', `picurl` varchar(100) NOT NULL default '', `price` decimal(10,0) NOT NULL default '0', `stockcount` int(11) NOT NULL default '0', `weight` int(11) NOT NULL default '0', `category` int(11) NOT NULL default '0', `detail` text NOT NULL, PRIMARY KEY (`id`) ) TYPE=MyISAM AUTO_INCREMENT=1 ;

This creates a table with the following fields:
  • ID: a unique ID for the product type
  • Description: a textual description of the product
  • PicURL: a URL to a picture of the product
  • Price: the cost of the product
  • Stockcount: the number of items of this product presently in stock
  • Weight: how heavy the product it (in pounds), used to calculate shipping
  • Category: the category of the product (see more on this later)
  • Detail: a detailed description of the product

Figure 1. The Products Table: This is data in the products table using PHPMY ADMIN.

Load this database with some dummy information (a sample is available in the download) as described. You don't need to set the ID field as it is an auto_increment type.

Next, you'll need a table of categories for your data. This will make it easier for your customers to browse related data. For example, if your store sells soccer merchandise (as this one does), the category could be team-based. So when customers enter your site, they can browse based on team, or search for a specific item. Assigning a category ID to the team enables this, and a separate table that associates that category ID with some friendly text makes it easier for your shoppers.

The code below shows the SQL used to create the Categories table. Figures 1 and 2 show data in these fields using PHPMYADMIN.

Author's Note: PHPMYADMIN is an open source PHP tool for managing online databases. If you want to use it, you can check it out in this article, or you can visit its homepage.

CREATE TABLE `categories` ( `id` int(11) NOT NULL auto_increment, `description` varchar(100) NOT NULL default '', PRIMARY KEY (`id`) ) TYPE=MyISAM AUTO_INCREMENT=1 ;

Figure 2. The Categories Table: This is data in the products table using PHPMY ADMIN.
Figure 3. The Storefront in Action: A simple PHP page introduces customers to your store and allows them to browse or search.

The next thing you will need is your storefront. This is a simple PHP page introducing people to your store and allowing them to browse or search your products. You can see some of the HTML code for this in the following code, and a screenshot of a sample one in Figure 3.

<a href="browse.php"> <img border="0" id="img3" src="images/buttonC.jpg" height="40" width="200" alt="Browse" .. fp-title="Browse"></a></p> <form method="POST" action="search.php" name="searchform"> <input type="text" name="T1" size="46"> <a href="javascript:document.searchform.submit()"> <img ... fp-title="Search"></a> </form>

This example is very simple and straightforward. It allows your users to access your products from the storefront in one of two ways.
  1. They can browse the products using browse.php. The storefront has hyperlinks that allow you to look at the merchandise for each of the teams with stocked goods. You will notice that even though there are 10 hyperlinks, they all link to the same PHP page! They are differentiated by the URL parameter that they pass to this page (browse.php?teamid=x). When PHP runs the page, it pulls in that parameter and generates the data accordingly. You will see this in more detail later on. What is also nice about this page is that these links and parameters are generated automatically based on the categories information in the database. Therefore, if a new team joins the league and has products, or a team folds or changes its name, you simply edit the database and the Web page will follow. You don't need to rewrite your page. The following page will show you how it's done.
  2. Your users can also search your database for the specific item that they want. For a small DB like this one, the advantage isn't obvious, but if you can imagine a larger store where they stock hundreds of different items for each team—from mugs to baby bibs—you may not want to page through hundreds of these to find that elusive calendar. Instead you would search for 'Seattle Calendar' or something like that, and get taken straight to a list of matching items. This functionality is achieved using an HTML form, that POSTs information to a PHP page (search.php) that runs the search against the data and renders the results. This form is shown in the next code block, and the code for the search results page is shown later.

Thanks for your registration, follow us on our social networks to keep up-to-date