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

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.

Browse

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.

Building the First Page
The first page that you will build is the browse page, browse.php. The easiest way to do this is to use an HTML designer like FrontPage or DreamWeaver to design a template HTML page that has the layout and details of the page. Browse.htm, which can be found in the source code for this article, was designed like this, and can be seen in Figure 4. You can then take this HTML and turn it into a PHP page by changing the file extension to .php, and adding the code to drive this page from a database.

If you look under the hood of this page, you will see that it has four tables.

The first table is at the top of the page with Team 1, Team 2, and Team 3 as columns. For the PHP, you will want to write X number of columns where X is the total number of categories, and the contents of each column will be the text associated with that category. Finally, you will want that text to link back to this page, passing the ID of the category to the page so that the rest of the page will display the products in that category. Take a look at the PHP/HTML mixture that achieves this:

'>

As you can see, the code is quite straightforward. You first output the

in html, and then the (table row). You then kick into PHP and run a query of all categories. Iterating through the results of this query, you then output the
(table column), embedding the contents of each record in turn within this TD. You want to embed two things: first, the ID, so that the URL link will read browse.php?ID=X where X is the ID of the current record. Second is the text within the TD, which you should set to the description of the current record.

The other three tables on the template page are three examples of how the product details would be rendered. The real ones will be generated using an iteration through the products of that category within the database.

The code for this is in Listing 1.

Figure 4. The Basic Layout: the Browse Page: This shows the basic layout for the browse page.

This may look a little difficult to read, but once you understand the HTML that constructed the template table you will see that it is quite straightforward. It simply queries the DB for the full list of products within the specific category, and then, looping through each one outputs a table with the picture, description and price details. It also has some logic to calculate the shipping cost based on the weight, and if the product is currently out of stock, it doesn’t give a link to buy that product. You can see this screen in action in Figure 5. You may notice that the teams I have used in this example are fictitious, so no pictures of their jerseys are available, and the pictures in the sample come from other teams.

Searching
The following SQL shows an example of SQL that searches the DB for products that match the word ‘Green.’

SELECT *FROM `products`WHERE ( ( Upper(`description`) like '%GREEN%') OR  ( Upper(`detail`) like '%GREEN%')) 

Looking back at the home page of the Web site, you will see that the form submits a text field called T1K to the search page. Your search PHP will then take this as a $_POST parameter, and can build the query from it as shown in below:

if(isset($_POST['T1']))      $SearchParam = strtoupper($_POST['T1']);else      $SearchParam = "";  ..

From here, the rest proceeds exactly like the browse screen (Listing 1) to produce identical results (Figure 5).

Buying the Product with PayPal
In Figure 5, you saw 'Buy It Now' links on products that were in stock. Clicking these will take you to a PayPal payment page. You could have populated this screen with the PayPal buttons, and on a live site that would be a good idea to prevent another click. For simplicity of code, this example separates the PayPal button generation onto a separate PHP page called buyit.php.

PayPal uses a Forms submission mechanism to process payments. All you have to do is put a form on your Web site that links to their site to send the payment to you. An example of a PayPal form that sends $75.00 to a PayPal account called '[email protected]':

Figure 5. Product Browsing in Action: This shows how the code in Listing 1 will look.

Therefore, if you write a PHP page that generates this HTML, preloaded for the product ID, your PayPal e-mail address and relevant pricing, you can sell your goods online. If you remember back to the browse/search pages, they gave a hyperlink to buyit.php?id=X where X depends on which product you link. Using this information, you can query the DB for the details of X, and generate the correct button (see Listing 2).

It would be much more efficient and user friendly if this code was part of browse.php and search.php, embedding the PayPal button on the search results instead of having another click. It was separated here for readability only.

A user now clicking on this button will be taken through the workflow of PayPal to send you their hard earned money.

The next steps could be to use the Instant Payment Notification scheme on PayPal to detect that the payment has been received, and as the ID of the product is known, this could call your shipping service to note that the goods need to be shipped to the buyer as well as automatically removing the inventory levels for the purchased products.

This article hopefully gave you a good start into building your own online store, loading it with products and making those products browseable and searchable by your customers as well as offering a methodology whereby they can pay you for those products! Have fun, and happy selling!

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

Top 5 B2B SaaS Marketing Agencies for 2023

In recent years, the software-as-a-service (SaaS) sector has experienced exponential growth as more and more companies choose cloud-based solutions. Any SaaS company hoping to stay ahead of the curve in this quickly changing industry needs to invest in effective marketing. So selecting the best marketing agency can mean the difference

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

©2023 Copyright DevX - All Rights Reserved. Registration or use of this site constitutes acceptance of our Terms of Service and Privacy Policy.

Sitemap