Outline
- Introduction
- The Situation
- The Data – CSV to MySQL
- HTML PayPal Button
- MySQL Data into HTML forms using PHP
Introduction
Although this may seem very basic to most experienced Programmers, I thought this might help some others who were in the same situation I was.
What You’ll Need
- Know how to create a MySQL table
- Know how to perferm MySQL Queries
- Have a solid understanding of HTML
- Understand basic PHP
The Situation
I recently had to add an ecommerce store to a clients existing website. No problem, I have done that before. The difference was, I had either used Open Source Shopping Cart software, or used PayPal’s button creator. The client had over 100 items to sell with different sizes and colors for some. That ruled out PayPal’s button creator as manually creating 100+ buttons would take far too long. Also, the look of the site was to stay the same, so instead of implementing Shopping Cart software, I decided to try and figure out how to dynamically generate PayPal buttons with PHP.
The Data – CSV to MySQL
The client had provided me with an Excel Spreadsheet with Products, Prices, etc. The first step was to convert this spreadsheet to a .csv (Comma Separated Values) file. The .csv had a result like so: (Although there were more columns, I’ll keep it simple for the example)
productid,category,price,desc 001,cat1,50.95,example product one 002,cat1,40.95,example product two 003,cat2,10.00,example product three
I needed to import these products into a MySQL table that I had previously created. I found a very helpful tool that converts CSV to SQL Inserts.
INSERT INTO products (`productid`,`category`,`price`,`desc`) VALUES ('001','cat1','50.95','example product one');
INSERT INTO products (`productid`,`category`,`price`,`desc`) VALUES ('002','cat1','40.95','example product two');
INSERT INTO products (`productid`,`category`,`price`,`desc`) VALUES ('003','cat2','10.00','example product three');
HTML PayPal Button
Now I have my data stored and organized. Using PayPal’s button creator, I made a button so I could get the HTML I would need. The button code is as follows:
<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post" />
<input type="hidden" name="cmd" value="_cart" />
<input type="hidden" name="business" value="youraccount@paypal.com" />
<input type="hidden" name="lc" value="US">
<input type="hidden" name="item_name" value="example product one" />
<input type="hidden" name="item_number" value="001" />
<input type="hidden" name="amount" value="50.95" />
<input type="hidden" name="currency_code" value="USD" />
<input type="hidden" name="button_subtype" value="products" />
<input type="hidden" name="shipping" value="0" />
<input type="hidden" name="handling_cart" value="4.95" />
<input type="hidden" name="add" value="1" />
<input type="hidden" name="bn" value="PP-ShopCartBF:btn_cart_LG.gif:NonHostedGuest" />
<input type="image" src="images/addcart.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!" />
These are the lines that will be focused on:
name=”business” value="youraccount@paypal.com" //This is just to link the button to your PayPal Account
name=”item_name” value=”example product one” //This will be the name of the item, this will be generated by the “desc” column
name=”item_number” value=”001″ //This is the productid, each item has a unique ID number
name=”amount” value=”50.95″ //This will need to be generated by the price column
name=”shipping” value=”0″ //I have shipping set to $0, as to keep it simple we will just charge a flat shipping rate in our example
name=”handling_cart” value=”4.95″ //This is the flat shipping rate for all orders
MySQL data into HTML forms using PHP
Now, you need to put the two together. You’ll need to:
- Connect to the MySQL Database
- Retrieve desired data
- Have the data populate the correct fields
- Loop the process until all products are listed
Connecting to the Database
The following is used to connect to the database:
<?php
$con = mysql_connect("mysql.yourdomain.com","login","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}mysql_select_db("databasename", $con);
Replace ("mysql.yourdomain.com","login","password"); with your login information. Next you need to connect to the table that your products are in.
$result = mysql_query("SELECT * FROM products WHERE category=''cat1");
This will allow you to select which data you wish to display. For this example, we just want to display products in the first category.
Displaying your Data from your MySQL table
As much as I hate HTML tables, I will be using them here to hold our dynamic data. You can however, use whatever suits your needs. For this example, add the following to your PHP file.
echo "<table border='0' width='600'>
<tr>
<th>Description</th>
<th>Picture</th>
<th>Price</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr class=\"itemname\">";
echo "<th colspan=\"3\">" . $row['desc'] . "</th></tr>";
echo "<tr class=\"imgrow\"><td class=\"imgcell\">" . "<img src=\"images/products/" . $row['id'] . ".jpg\" alt=\"Description if needed\" />" . "</td>";
echo "<td class=\"pricecell\">" . "$" . $row['price'] . "<br />plus Shipping" . "</td>";
Now we have our basic structure laid out. The while command will cycle through our data and display all the items that have a category value of cat1. Also note, all of the " need to be "add slashed," as the quotes are a delimiter in PHP. This means, wherever you want a " to appear, you have to add a / before it, even when you are echoing HTML.
We're More Than Halfway!
Check back within the next week to read how to Dynamically Generate PayPal Add to Cart Buttons! Or you can follow me on Twitter ( @MetacomCreative ) for updates, or subscribe to my RSS feed.


Hallo!
I`m connecting to my database as You have shown, but sometimes connection to the database is lost during loaging of the page… any idea what can I do?
.-= Adzi´s last blog ..Dzisiaj naprawiłem całkiem ładny kawał laptopa =-.
Hi Adzi,
Is this happening on a site that is live? Or a site that is on your local computer? If it is on your local computer, you want to try connecting to “localhost”.
If the site is live and you are losing the connection, that might be an issue with your host. You can try contacting their tech support and ask if they have had any server outages.
If you can elaborate more on your problem with more details, I would be glad to offer any help that I can.
It’s happening on a “live” site, yes. I suppose that everything is fine with Your code, and hosting is just bad – it is free one You know. When I refresh site it connect just fine.
On another page I found the same problem I the reason was hosting as You suggested.
Thanks for reply.
.-= Adzi´s last blog ..Zmiany w wyglądzie google ciąg dalszy =-.
Your host might be the problem. There are a lot of very affordable hosting plans out there that offer MySQL databases, they might be worth looking into.
I hope I helped, or at least provided you with some info! Feel free to ask again if you have any other problems!
Great idea even for experienced guys !
Thanks Dave! I should probably revisit this post and touch it up a bit. I’m glad people found it helpful!
I have been visiting your weblog for a while now and I usually discover a gem in your new posts. Thanks for sharing.
Aw, this was a really nice post. In concept I wish to put in writing like this moreover – taking time and precise effort to make an excellent article… but what can I say… I procrastinate alot and on no account appear to get something done.
Hi Ryan,
Can you send me the link of your next post of dynamically creating a add to cart button?
Thanks,
Kala
Hi Kala,
I apologize, I should have included a link in this article. Anyways, you can view the second part of the post at How to Dynamically Generate Paypal Add to Cart Buttons with PHP and MySQL.
This tutorial is a little old, and one thing to keep in mind for projects is the availability of all the Open Source shopping cart software out there.
I hope this helps! And if you have any questions, please feel free to ask! Good luck!