<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Web Design &#38; Development Talk &#187; MySQL</title>
	<atom:link href="http://www.web-design-talk.co.uk/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.web-design-talk.co.uk</link>
	<description>Web Design &#38; Development Blog</description>
	<lastBuildDate>Thu, 29 Dec 2011 15:34:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Enchaning an Invoicing System for Reporting</title>
		<link>http://www.web-design-talk.co.uk/383/enchaning-a-invoicing-system-for-reporting/</link>
		<comments>http://www.web-design-talk.co.uk/383/enchaning-a-invoicing-system-for-reporting/#comments</comments>
		<pubDate>Tue, 20 Sep 2011 19:44:04 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://www.web-design-talk.co.uk/?p=383</guid>
		<description><![CDATA[At the start of the year I wrote a custom invoicing system for a client, using PHP and Smarty (hope to convert to use Twig at some point). The client has since come back to me to built in extra functionality &#8211; namely a reporting section. I thought I&#8217;d share my solution to the issue [...]]]></description>
			<content:encoded><![CDATA[<p>At the start of the year I wrote a custom invoicing system for a client, using PHP and Smarty (hope to convert to use Twig at some point). The client has since come back to me to built in extra functionality &#8211; namely a reporting section. I thought I&#8217;d share my solution to the issue I was faced with.</p>
<p>The client required some fairly involved reporting facilities, here is a sample of some of the requirements from the client:</p>
<ul>
<li>Aggregated report, split between two dates of our choosing, with totals split by either month, day or week (we want to choose)</li>
<li>A list of invoices for a day of our choosing with aggregated totals of for the day selected</li>
<li>Annual invoicing report &#8211; we want to show a summary of the total for the items sold, the total vat charged and summaries for average invoice values</li>
<li>Invoices totals grouped by customer</li>
</ul>
<p>There were several more, but they were all similar to the above.</p>
<p>Firstly, I&#8217;ll explain the database setup, for the invoicing, of the current system I built. It was basically two tables &#8211; one for the invoivce header and one table for the invoices items &#8211; nothing un typical there at all.</p>
<p><span id="more-383"></span></p>
<p>My first point of call to integrate the reporting was to use what I had already. This proved qwuite complicated as it involved fecthing large sets of data to calculate the totals required by the reports &#8211; this was also pretty slow when running the more intensive reports over a longer date range. It would get even more complicated and database intensive when I wanted totals for the other reports.</p>
<p>After some consideration, my solution to this was simple. I decided to add a few new columns to my invoice header table &#8211; &#8220;invoice_total&#8221;, &#8220;invoice_sub_total&#8221;, &#8220;invoice_vat&#8221; and &#8220;total_invoice_items&#8221;. For the reporting side of things, this made the whole process much easier. For example:</p>
<p>To get an aggreate total figure invoiced between two dates, I required a single sql statement, that required no joins, or loops in PHP:</p>
<pre class="brush: sql; title: ; notranslate">
SELECT SUM(`invoice_total`) As total_invoiced FROM `invoice_header` WHERE (`invoice_issue_date` &gt;= :myDate AND `invoice_issue_date`
</pre>
<p>Equally as simple, to get a monthly total between two dates, grouped by month, the sql statement was just as simple:</p>
<pre class="brush: sql; title: ; notranslate"> SELECT SUM(`invoice_total`) As total_invoiced FROM `invoice_header` WHERE (`invoice_issue_date` &gt;= :myDate AND `invoice_issue_date`
</pre>
<p>Getting average invoice sub total between two dates:</p>
<pre class="brush: sql; title: ; notranslate"> SELECT AVG(`invoice_sub_total`) As average_invoice_subtotal FROM `invoice_header` WHERE (`invoice_issue_date` &gt;= :myDate AND `invoice_issue_date` </pre>
<p>The latter made the actual PHP code required to produce the required reports very straightforward &#8211; before I would have required joins to caluclate the totals and multiple sql queries &#8211; that could have got quite messy. I also gained a few added benefits of storing the totals within my header table:</p>
<ul>
<li>On the print invoice screen, there was need to calculate anything, as I simply read the totals from the datbase</li>
<li>On the invoice listing screens, I could now display an invoice total in a seperate column, as this was again, simply a database value &#8211; before I would have needed to calculate this total via the invoice items table</li>
</ul>
<p>However, this simplicity in one area, came at a cost &#8230; unfortunately. As all report data was now based from totals in the invoice header table, this figure needed to be correct at all times. I needed to amend the add and edit invoice functions. When adding an invoice, I needed another loop to calculate the aggregate totals to insert into the database. When editing the invoice, I needed to recalculate these totals each time the invoice was edited (in case a row was deleted for example). Whilst the latter added a little more work when adding/editing invoices, I feel that this is well worth it due to simplicity when dealing with the reporting side.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.web-design-talk.co.uk/383/enchaning-a-invoicing-system-for-reporting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Storing Ecommerce Order Information Correctly</title>
		<link>http://www.web-design-talk.co.uk/323/storing-ecommerce-order-details/</link>
		<comments>http://www.web-design-talk.co.uk/323/storing-ecommerce-order-details/#comments</comments>
		<pubDate>Mon, 04 Jul 2011 19:23:28 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Paypal]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://www.web-design-talk.co.uk/?p=323</guid>
		<description><![CDATA[Yesterday I was doing a tiny bit of web realted freelancing for a friend &#8211; simply involving doing a bulk, conditional update of his entire stock for his ecommerce website. The site itself was simple in design and used PayPal with a sprinkle of IPN. So, after a bit of MySQL magic, I updated all [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday I was doing a tiny bit of web realted freelancing for a friend &#8211; simply involving doing a bulk, conditional update of his entire stock for his ecommerce website. The site itself was simple in design and used PayPal with a sprinkle of IPN. So, after a bit of MySQL magic, I updated all of his prices, for all ~700 products in one fell swoop &#8211; all good so far, easy money.</p>
<p>However, after an hour or so of emailing the store owner I got a very worried phone call. In short, the bulk price update to had caused all archived order invoices to become incorrect. Why &#8211; due to bad database design by the original developer, let me explain.</p>
<p><span id="more-323"></span>The <strong>web developer</strong> had stored the order as you would expect &#8211; an order header table, coupled with a order lines table (one record for each order item). All good so far. Where the previous web developer had fallen short is not storing the price at the time of the order, instead simply storing the product id within the order lines table and looking order details via the way of an <a title="Inner Join on Wikipedia" href="http://en.wikipedia.org/wiki/Join_%28SQL%29#Inner_join" target="_blank">INNER JOIN</a> &#8211; bad move unfortunately. Obviously, in any sort of product price update (as above) all of his historical order information would become null and void. The latter was especially bad in this case, as the store owner pretty much used the list of store orders as base for financial and year end reporting, as the business was pretty small (no Sage then). Explaining the latter to a none technical client is even harder, believe me <img src='http://www.web-design-talk.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>To avoid such nightmares <em>never</em>, <em>ever </em>store such information in this manner &#8211; always store product details at the time of ordering. Storing products details at this stage will ensure that an order made in 2008 will be correct when viewed even 10 years later when prices will have been updated. So, in the order lines table, as an absolute minimum store the product name, price and any attributes that will affect price &#8211; meaning an INNER JOIN with the order header table is not required.</p>
<p>Depending upon your business rules and frequency by which your product information changes, you could also store the product at time of ording, within a product archive table &#8211; that stores basic product info. This way, there is the benefit of saving the description the customer saw at time of ordering.</p>
<p>What is descriobed above is basically bog standard when making any ecommerce solution, or even using something bigger like Opencart, but when it is not present, the amount of issues it causes is huge.</p>
<p>Luckily for me (and the store owner) I created a quick backup of the entire store&#8217;s database schema and data before making the bulk update, so was able to restore this for the time being. All I have to do now is amened the database design accordingly and alter the way the site stores order information.</p>
<p>So, never ever rely upon relational data, to view and store data that needs to be achived.</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.web-design-talk.co.uk/323/storing-ecommerce-order-details/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Best Practices With PEAR Cache Lite</title>
		<link>http://www.web-design-talk.co.uk/286/best-practices-with-pear-cache-lite/</link>
		<comments>http://www.web-design-talk.co.uk/286/best-practices-with-pear-cache-lite/#comments</comments>
		<pubDate>Sun, 13 Mar 2011 20:58:14 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://www.web-design-talk.co.uk/?p=286</guid>
		<description><![CDATA[Caching a website using PEAR Cache Lite has many benefits &#8211; it will speed up your site and is very simple to implement. However, there are several common pitfalls they could cause potential issues on a website. Basically, you can&#8217;t approach caching from a single angle, as the type of caching used is always project/problem [...]]]></description>
			<content:encoded><![CDATA[<p>Caching a website using <a href="http://www.web-design-talk.co.uk/226/php-caching-pear-cache-lite/">PEAR Cache Lite</a> has many benefits &#8211; it will speed up your site and is very simple to implement. However, there are several common pitfalls they could cause potential issues on a website. Basically, you can&#8217;t approach caching from a single angle, as the type of caching used is always project/problem specific.</p>
<h2>Full File Level Caching</h2>
<p>Full file caching is the simplist method and essentially takes an image of the page at a particular time. This method is very fast to implement but does have it&#8217;s downsides. As you are storing a static file of the page you&#8217;ll instantly notice the following issues (I&#8217;ve included some typical examples you&#8217;ll run into):</p>
<ul>
<li>Any elements of your page that require session data will fail to work (you&#8217;ve essentially cached the session id too!)</li>
<li>If the page needs to be served using a particular header you&#8217;ll be out of luck, as a statiuc file will always be served using a plain text header (E.g. a cached rss feed won&#8217;t validate as the header has changed)</li>
<li>Clearing the cache becomes very clumsey as the whole cache requires cleaning when even a small change is made (E.g. say a new article is added by the admin)</li>
<li>User logins that make use of session data</li>
<li>E-Commerce sites and a shopping basket summary</li>
<li>Search results pages</li>
</ul>
<p>With full file caching dynamic content will always be a issue. However, full file caching CAN help high traffic sites, even when a short cache lifetime is set &#8211; use with caution!<br />
<span id="more-286"></span></p>
<h2>Block Level or Partial File Caching</h2>
<p>Block level caching is likely to be a much longer term caching solution. It is more likely that you&#8217;ll have certain sections of a site that do not require an up to date minute. For example, say we have an ecommerce store with dropdown menu that list our full category structure from a database &#8211; this is likely to be highly database intensive and may use recursion. This output can be cached to a flat file as follows:</p>
<pre class="brush: php; title: ; notranslate">
$options = array(
    'cacheDir' =&gt; '/cached/',
    'lifeTime' =&gt; 1000
);

$cache = new Cache_Lite_Output($options);

if (!$cache-&gt;start('left_category_structure')) {
   $store-&gt;list_full_category_structure();
   $cache-&gt;end();
}
</pre>
<p>This is much better than full file caching for several reasons:</p>
<ul>
<li>The page in question can remain dynamic dynamic elements such as the user&#8217;s login status on another part of the page</li>
<li>If a change is made to the category structure I can delete that particular section of the cache only (the rest of the cache does not require regenerating!)</li>
<li>The cache lifetime can be set on a per block basis &#8211; useful for different types of content</li>
<li>Even if the category structure doesn&#8217;t change often, the block could still be cache forever (set cacheLifeTime to null in the options array) and cleaned when a change is made</li>
</ul>
<h2>Taking Partial Caching Further</h2>
<p>PEAR Cache_LIte is also fairly flexible and you can make it fit your needs. For example, say we wanted to cache 10 random articles on a website &#8211; which consists of a database query that fetches 10 random articles from a database. The idea here is to still cache the content in a block as above. However, for this our system would create a number of cache blocks and then service those cached blocks to the user. The solution is a simple one, building on the example from above:</p>
<pre class="brush: php; title: ; notranslate">
$options = array(
    'cacheDir' =&gt; '/cached/',
    'lifeTime' =&gt; 1000
);

$cache = new Cache_Lite_Output($options);

if (!$cache-&gt;start( 'left_category_structure . rand(1,5))) {
   $store-&gt;list_full_category_structure();
   $cache-&gt;end();
}
</pre>
<p>When the user requests a page, 5 different cache blocks will be checked &#8211; we are essentially creating 5 different versions of the random cached content.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.web-design-talk.co.uk/286/best-practices-with-pear-cache-lite/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Associate Products with Multiple Categories Using MySQL</title>
		<link>http://www.web-design-talk.co.uk/254/products-multiple-categories-mysql-database/</link>
		<comments>http://www.web-design-talk.co.uk/254/products-multiple-categories-mysql-database/#comments</comments>
		<pubDate>Sun, 21 Nov 2010 16:33:52 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[phpmyadmin]]></category>

		<guid isPermaLink="false">http://www.web-design-talk.co.uk/?p=254</guid>
		<description><![CDATA[Having a database structure that allows a product to be associated with more than one category is a very common scenario in any eCommerce website. However, after working on a couple of truely awful bespoke solutions from other developers recently, whose methods to store and retreive such data were so convoluted, have inspired me to [...]]]></description>
			<content:encoded><![CDATA[<p>Having a database structure that allows a product to be associated with more than one category is a very common scenario in any eCommerce website. However, after working on a couple of truely awful bespoke solutions from other developers recently, whose methods to store and retreive such data were so convoluted, have inspired me to write this article.</p>
<p>Story such data need to not be overly complicated. The following, simple, table structure is required (in a real ecommerce system you would definately have additional fields &#8211; these have been omitted here for thae sake of the example):</p>
<p><span style="text-decoration: underline;"><strong>Product</strong></span><strong><br />
</strong>product_id (PK)<strong><br />
</strong>name</p>
<p><span style="text-decoration: underline;"><strong>Category</strong></span><br />
category_id (PK)<br />
name</p>
<p><span style="text-decoration: underline;"><strong>Products_Category</strong></span><br />
product_id (PK)<strong><br />
</strong>category_id (PK)</p>
<p>The products_category table is a simple linking table that allows a <a href="http://dev.mysql.com/tech-resources/articles/intro-to-normalization.html">many-to-many</a> relationship between the product and category table. It contains to two primary keys to ensure every combination of product and category is unique. for example, this table will contain many unique number pairs and a row may be 1,4 or product_id 1 and category_id 4. The files to create and populate this table structure with sample data can be found <a href="http://www.web-design-talk.co.uk/examples/7/sql.txt">here</a>.</p>
<p>Now it is simplay a case of running a series of MySQL statements (I&#8217;d advise converting them to stored procedures for more security and better application seperation) to retreieve the appropriate data. For example:</p>
<p><strong>Products Within a Certain Category (E.g. category_id 1):</strong></p>
<pre class="brush: sql; title: ; notranslate">
SELECT p.product_id, p.name FROM product p
INNER JOIN product_category pc
ON p.product_id = pc.product_id
WHERE pc.category_id = '1';
</pre>
<p><strong>Count Products Within a Certain Category (E.g. category_id 1):</strong></p>
<pre class="brush: sql; title: ; notranslate">
SELECT COUNT(p.product_id) As myCount FROM product p
INNER JOIN product_category pc
ON p.product_id = pc.product_id
WHERE pc.category_id = '1';
</pre>
<p>&#8230;and that&#8217;s it. Extremely simple, can be expanded to any eCommerce system and not convoluted at all <img src='http://www.web-design-talk.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.web-design-talk.co.uk/254/products-multiple-categories-mysql-database/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Displaying a Breadcrumb Navigation for Multiple Sub Categories via PHP</title>
		<link>http://www.web-design-talk.co.uk/188/displaying-a-breadcrumb-navigation-for-multiple-sub-categories-via-php-2/</link>
		<comments>http://www.web-design-talk.co.uk/188/displaying-a-breadcrumb-navigation-for-multiple-sub-categories-via-php-2/#comments</comments>
		<pubDate>Mon, 22 Mar 2010 10:57:43 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[phpmyadmin]]></category>
		<category><![CDATA[multiple sub categories]]></category>
		<category><![CDATA[php categories]]></category>
		<category><![CDATA[recursive functions]]></category>

		<guid isPermaLink="false">http://www.web-design-talk.co.uk/?p=188</guid>
		<description><![CDATA[While making an ecommerce store I ran into the issue of displaying a category breadcrumb. Usually this is easy as I always recommend keeping the numbers of categories and sub categories to a maximum of 1 level deep. E.g. Tshirts &#62; Red tshirts. For this store, due to the sheer number of products the owner [...]]]></description>
			<content:encoded><![CDATA[<p>While making an ecommerce store I ran into the issue of displaying a category breadcrumb. Usually this is easy as I always recommend keeping the numbers of categories and sub categories to a maximum of 1 level deep. E.g. Tshirts &gt; Red tshirts. For this store, due to the sheer number of products the owner wanted to add up to seven category levels. While there are many tutorials on this floating about, they all seem tocus on displaying the whole tree &#8211; very useful for a sitemap page, but not a breadcrumb navigation. While it would have been possible to hard code several if statements into the category listing page, this seemed a bit messey and would cause problems if an eighth level was added.</p>
<p>For the breadcrumb naviagtion I needed a function to displaying the path to a given category ID, sometimes refered to as a single branch or node. I was using a simple parent child database table structure:</p>
<p><a href="http://www.web-design-talk.co.uk/wp-content/uploads/2010/03/php-multiple-sub-categories.jpg"><img class="size-full wp-image-189 alignnone" style="border: 1px solid #cccccc;" title="php-multiple-sub-categories" src="http://www.web-design-talk.co.uk/wp-content/uploads/2010/03/php-multiple-sub-categories.jpg" alt="php multiple=" /></a></p>
<p>While the latter may seem fairly trival I really couldn&#8217;t get my head around the problem. After a bit of Googling, I found a function that was a good starting point so adapted it to fit my problem (the function is part of a categoru class):</p>
<pre class="brush: php; title: ; notranslate">
function getCategoryTreeIDs($catID) {
		$row = DB::getInstance()-&gt;query(&quot;SELECT parent FROM categories WHERE ID = '$catID'&quot;)-&gt;fetch();
		$path = array();
		if (!$row['parent'] == '') {
			$path[] = $row['parent'];
			$path = array_merge($this-&gt;getCategoryTreeIDs($row['parent']), $path);
		}
		return $path;
	}
</pre>
<p>The function simply returns an array of category IDs. E.g 20, 28. So from the array I&#8217;d know that the tree would go as Home &gt; Cat ID 20 &gt; Cat ID 28.</p>
<h2 style="font-size:12px;border:0;padding-left:0;">Displaying the Breadcrumb Navigation</h2>
<p>To display the actual breadcrumb I simply added the following method, that loops through the array of ID we just generated. The getNameLink method simply generates an SEO feindly website URL for the category, inside the &lt;a&gt; tag.</p>
<pre class="brush: php; title: ; notranslate">
function showCatBreadCrumb($catID) {

		$array = $this-&gt;getCategoryTreeIDs($catID);

		$numItems = count($array);
		for ($i = 0; $i&lt;=$numItems-1; $i++) {
			echo $this-&gt;getNameLink($array[$i]) . ' &amp;raquo; ';
		}
	}
</pre>
<p>The result is a nicely formatted breadcrumb (to use our tshiorts example again):</p>
<p>Home &amp;rquao; Clothes &amp;rquao; tshirts &amp;rquao;  Mens &amp;rquao; Red tshirts &amp;rquao; Offensive &amp;rquao;</p>
<h2 style="font-size:12px;border:0;padding-left:0;">A recursive function inside a loop, are you insane?</h2>
<p>Some of you may have noticed that the function used to generate the category IDs is called recursively. This generally considered bad practice, for large data sets due to performance issues. However, for the current use this isn&#8217;t an issue. I know for a fact that client won&#8217;t be adding categories more than several levels deep, so performance really isn&#8217;t an issue in my eyes here. Maybe if we had hundreds of categories, but for several it&#8217;s really a non issue in my opinion.</p>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 185px; width: 1px; height: 1px; overflow: hidden;">
<pre>getCategoryTreeIDs</pre>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.web-design-talk.co.uk/188/displaying-a-breadcrumb-navigation-for-multiple-sub-categories-via-php-2/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>MySQL Cheatsheet &#8211; Useful MySQL Queries</title>
		<link>http://www.web-design-talk.co.uk/128/mysql-cheatsheet-useful-mysql-queries/</link>
		<comments>http://www.web-design-talk.co.uk/128/mysql-cheatsheet-useful-mysql-queries/#comments</comments>
		<pubDate>Thu, 05 Nov 2009 19:06:50 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[mysql query]]></category>
		<category><![CDATA[sample mysql queries]]></category>

		<guid isPermaLink="false">http://www.web-design-talk.co.uk/?p=128</guid>
		<description><![CDATA[Page rank 5 Page rank 5 Page rank 5 Page rank 5 What follows is a list of some very useful MySQL queries for use in projects, enjoy. I won&#8217;t go into detail how and why they work, I&#8217;ll leave that up to you]]></description>
			<content:encoded><![CDATA[<div id="rank_jit_com_main" style="border: 0px none; margin: 0px; padding: 0px; position: absolute; top: 126px; left: 519px; line-height: normal; background-color: transparent; z-index: 99999; width: 70px; height: 46px; visibility: hidden;">
<div style="visibility: inherit;">
<table style="margin: 0pt; padding: 0px; width: 70px; height: 36px; background-color: transparent; font-family: Lucida Grande,sans-serif; font-size: 9px; z-index: 200000; border-collapse: separate; visibility: inherit; border: 0px 1px 1px 0px none solid solid none -moz-use-text-color #e0e0e0 #e0e0e0 -moz-use-text-color;" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr style="background-color: transparent;">
<td style="margin: 0px; padding: 0px; height: 16px; background-color: transparent; text-align: center; vertical-align: middle; visibility: inherit; border: 0px 0px 1px none none solid -moz-use-text-color -moz-use-text-color #e0e0e0;">
<div style="border: 0pt none; margin: 0pt; padding: 0pt; background-color: transparent;"></div>
</td>
<td style="margin: 0px; padding: 0px; width: 20px; height: 16px; text-align: center; vertical-align: middle; background-color: white; visibility: inherit; border: 1px 0px 1px 1px solid none solid solid #e0e0e0 -moz-use-text-color #e0e0e0 #e0e0e0;" colspan="2" align="center"><img id="rank_jit_pin" style="border: 0px none; margin: 0px; padding: 0px; display: inline; cursor: pointer; width: 16px; height: 16px; vertical-align: bottom; visibility: inherit; background-color: transparent;" src="http://www.rankjit.com/extension/img/pin.gif" alt="" /></td>
</tr>
<tr style="margin: 0px; padding: 0px; background-color: white; visibility: inherit; border: 1px 0px 0px solid none none #e0e0e0 -moz-use-text-color -moz-use-text-color;">
<td style="margin: 0px; padding: 0px; font-family: Lucida Grande,sans-serif; color: #000000; font-size: 9px; font-weight: normal; text-align: center; vertical-align: middle; height: 10px; background-color: white; visibility: inherit; border: 0px 0px 0px 1px none none none solid -moz-use-text-color -moz-use-text-color -moz-use-text-color #e0e0e0;" align="center">Page rank</td>
<td style="margin: 0px; padding: 0px; font-family: Lucida Grande,sans-serif; text-align: center; vertical-align: middle; background-color: white; border-spacing: 1px; font-size: 14px; width: 20px; height: 20px; color: white; visibility: inherit; border: 0px 0px 0px 1px none none none solid -moz-use-text-color -moz-use-text-color -moz-use-text-color #e0e0e0;" rowspan="2" align="center" valign="middle">
<div style="border: 0px none; margin: 1px; padding: 0px; width: 20px; height: 20px; background-color: #848484; visibility: inherit;">
<div id="rank_jit_resultContainer" style="border: 0px none; margin: 0px; padding: 0px; background-color: transparent; width: 20px; height: 20px; line-height: 20px; vertical-align: middle; color: white; visibility: inherit; text-align: center;">5</div>
</div>
</td>
</tr>
<tr>
<td style="margin: 0px; padding: 0px; height: 10px; vertical-align: middle; background-color: white; visibility: inherit; border: 0px 0px 0px 1px none none none solid -moz-use-text-color -moz-use-text-color -moz-use-text-color #e0e0e0;" align="center">
<div style="border: 1px solid #e0e0e0; margin: 0px 0px 1px 1px; padding: 0px; width: 38px; height: 4px; visibility: inherit;"></div>
</td>
</tr>
</tbody>
</table>
</div>
<div style="border: 0pt none; margin: 0pt; padding: 0pt; overflow: hidden; z-index: 9999; font-style: normal; font-weight: normal; font-family: trebuchet ms,arial,helvetica,sans-serif; float: none; position: absolute; left: auto; top: 39.5px; line-height: normal; background-color: transparent; visibility: inherit; width: 10px; height: 10px; right: 50px;"><img style="border: 0pt none; margin: 0pt; padding: 0pt; font-style: normal; font-weight: normal; font-family: trebuchet ms,arial,helvetica,sans-serif; float: none; position: absolute; left: 0px; top: 0px; line-height: normal; background-color: transparent; visibility: inherit; z-index: 99999;" src="http://www.rankjit.com/extension/img/pointer.gif" alt="" /></div>
</div>
<div id="rank_jit_com_main" style="border: 0px none; margin: 0px; padding: 0px; position: absolute; top: 126px; left: 519px; line-height: normal; background-color: transparent; z-index: 99999; width: 70px; height: 46px; visibility: hidden;">
<div style="visibility: inherit;">
<table style="margin: 0pt; padding: 0px; width: 70px; height: 36px; background-color: transparent; font-family: Lucida Grande,sans-serif; font-size: 9px; z-index: 200000; border-collapse: separate; visibility: inherit;" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr style="background-color: transparent;">
<td style="margin: 0px; padding: 0px; height: 16px; background-color: transparent; text-align: center; vertical-align: middle; visibility: inherit;"></td>
<td style="margin: 0px; padding: 0px; width: 20px; height: 16px; text-align: center; vertical-align: middle; background-color: white; visibility: inherit;" colspan="2" align="center"><img id="rank_jit_pin" style="border: 0px none; margin: 0px; padding: 0px; display: inline; cursor: pointer; width: 16px; height: 16px; vertical-align: bottom; visibility: inherit; background-color: transparent;" src="http://www.rankjit.com/extension/img/pin.gif" alt="" /></td>
</tr>
<tr style="margin: 0px; padding: 0px; background-color: white; visibility: inherit; border: 1px 0px 0px solid none none #e0e0e0 -moz-use-text-color -moz-use-text-color;">
<td style="margin: 0px; padding: 0px; font-family: Lucida Grande,sans-serif; color: #000000; font-size: 9px; font-weight: normal; text-align: center; vertical-align: middle; height: 10px; background-color: white; visibility: inherit;" align="center">Page rank</td>
<td style="margin: 0px; padding: 0px; font-family: Lucida Grande,sans-serif; text-align: center; vertical-align: middle; background-color: white; border-spacing: 1px; font-size: 14px; width: 20px; height: 20px; color: white; visibility: inherit;" rowspan="2" align="center" valign="middle">
<div style="border: 0px none; margin: 1px; padding: 0px; width: 20px; height: 20px; background-color: #848484; visibility: inherit;">
<div id="rank_jit_resultContainer" style="border: 0px none; margin: 0px; padding: 0px; background-color: transparent; width: 20px; height: 20px; line-height: 20px; vertical-align: middle; color: white; visibility: inherit; text-align: center;">5</div>
</div>
</td>
</tr>
<tr>
<td style="margin: 0px; padding: 0px; height: 10px; vertical-align: middle; background-color: white; visibility: inherit;" align="center"></td>
</tr>
</tbody>
</table>
</div>
<div style="border: 0pt none; margin: 0pt; padding: 0pt; overflow: hidden; z-index: 9999; font-style: normal; font-weight: normal; font-family: trebuchet ms,arial,helvetica,sans-serif; float: none; position: absolute; left: auto; top: 39.5px; line-height: normal; background-color: transparent; visibility: inherit; width: 10px; height: 10px; right: 50px;"><img style="border: 0pt none; margin: 0pt; padding: 0pt; font-style: normal; font-weight: normal; font-family: trebuchet ms,arial,helvetica,sans-serif; float: none; position: absolute; left: 0px; top: 0px; line-height: normal; background-color: transparent; visibility: inherit; z-index: 99999;" src="http://www.rankjit.com/extension/img/pointer.gif" alt="" /></div>
</div>
<div id="rank_jit_com_main" style="border: 0px none; margin: 0px; padding: 0px; position: absolute; top: 126px; left: 519px; line-height: normal; background-color: transparent; z-index: 99999; width: 70px; height: 46px; visibility: hidden;">
<div style="visibility: inherit;">
<table style="margin: 0pt; padding: 0px; width: 70px; height: 36px; background-color: transparent; font-family: Lucida Grande,sans-serif; font-size: 9px; z-index: 200000; border-collapse: separate; visibility: inherit;" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr style="background-color: transparent;">
<td style="margin: 0px; padding: 0px; height: 16px; background-color: transparent; text-align: center; vertical-align: middle; visibility: inherit;"></td>
<td style="margin: 0px; padding: 0px; width: 20px; height: 16px; text-align: center; vertical-align: middle; background-color: white; visibility: inherit;" colspan="2" align="center"><img id="rank_jit_pin" style="border: 0px none; margin: 0px; padding: 0px; display: inline; cursor: pointer; width: 16px; height: 16px; vertical-align: bottom; visibility: inherit; background-color: transparent;" src="http://www.rankjit.com/extension/img/pin.gif" alt="" /></td>
</tr>
<tr style="margin: 0px; padding: 0px; background-color: white; visibility: inherit; border: 1px 0px 0px solid none none #e0e0e0 -moz-use-text-color -moz-use-text-color;">
<td style="margin: 0px; padding: 0px; font-family: Lucida Grande,sans-serif; color: #000000; font-size: 9px; font-weight: normal; text-align: center; vertical-align: middle; height: 10px; background-color: white; visibility: inherit;" align="center">Page rank</td>
<td style="margin: 0px; padding: 0px; font-family: Lucida Grande,sans-serif; text-align: center; vertical-align: middle; background-color: white; border-spacing: 1px; font-size: 14px; width: 20px; height: 20px; color: white; visibility: inherit;" rowspan="2" align="center" valign="middle">
<div style="border: 0px none; margin: 1px; padding: 0px; width: 20px; height: 20px; background-color: #848484; visibility: inherit;">
<div id="rank_jit_resultContainer" style="border: 0px none; margin: 0px; padding: 0px; background-color: transparent; width: 20px; height: 20px; line-height: 20px; vertical-align: middle; color: white; visibility: inherit; text-align: center;">5</div>
</div>
</td>
</tr>
<tr>
<td style="margin: 0px; padding: 0px; height: 10px; vertical-align: middle; background-color: white; visibility: inherit;" align="center"></td>
</tr>
</tbody>
</table>
</div>
<div style="border: 0pt none; margin: 0pt; padding: 0pt; overflow: hidden; z-index: 9999; font-style: normal; font-weight: normal; font-family: trebuchet ms,arial,helvetica,sans-serif; float: none; position: absolute; left: auto; top: 39.5px; line-height: normal; background-color: transparent; visibility: inherit; width: 10px; height: 10px; right: 50px;"><img style="border: 0pt none; margin: 0pt; padding: 0pt; font-style: normal; font-weight: normal; font-family: trebuchet ms,arial,helvetica,sans-serif; float: none; position: absolute; left: 0px; top: 0px; line-height: normal; background-color: transparent; visibility: inherit; z-index: 99999;" src="http://www.rankjit.com/extension/img/pointer.gif" alt="" /></div>
</div>
<div id="rank_jit_com_main" style="border: 0px none; margin: 0px; padding: 0px; position: absolute; top: 126px; left: 519px; line-height: normal; background-color: transparent; z-index: 99999; width: 70px; height: 46px; visibility: hidden;">
<div style="visibility: inherit;">
<table style="margin: 0pt; padding: 0px; width: 70px; height: 36px; background-color: transparent; font-family: Lucida Grande,sans-serif; font-size: 9px; z-index: 200000; border-collapse: separate; visibility: inherit;" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr style="background-color: transparent;">
<td style="margin: 0px; padding: 0px; height: 16px; background-color: transparent; text-align: center; vertical-align: middle; visibility: inherit;"></td>
<td style="margin: 0px; padding: 0px; width: 20px; height: 16px; text-align: center; vertical-align: middle; background-color: white; visibility: inherit;" colspan="2" align="center"><img id="rank_jit_pin" style="border: 0px none; margin: 0px; padding: 0px; display: inline; cursor: pointer; width: 16px; height: 16px; vertical-align: bottom; visibility: inherit; background-color: transparent;" src="http://www.rankjit.com/extension/img/pin.gif" alt="" /></td>
</tr>
<tr style="margin: 0px; padding: 0px; background-color: white; visibility: inherit; border: 1px 0px 0px solid none none #e0e0e0 -moz-use-text-color -moz-use-text-color;">
<td style="margin: 0px; padding: 0px; font-family: Lucida Grande,sans-serif; color: #000000; font-size: 9px; font-weight: normal; text-align: center; vertical-align: middle; height: 10px; background-color: white; visibility: inherit;" align="center">Page rank</td>
<td style="margin: 0px; padding: 0px; font-family: Lucida Grande,sans-serif; text-align: center; vertical-align: middle; background-color: white; border-spacing: 1px; font-size: 14px; width: 20px; height: 20px; color: white; visibility: inherit;" rowspan="2" align="center" valign="middle">
<div style="border: 0px none; margin: 1px; padding: 0px; width: 20px; height: 20px; background-color: #848484; visibility: inherit;">
<div id="rank_jit_resultContainer" style="border: 0px none; margin: 0px; padding: 0px; background-color: transparent; width: 20px; height: 20px; line-height: 20px; vertical-align: middle; color: white; visibility: inherit; text-align: center;">5</div>
</div>
</td>
</tr>
<tr>
<td style="margin: 0px; padding: 0px; height: 10px; vertical-align: middle; background-color: white; visibility: inherit;" align="center"></td>
</tr>
</tbody>
</table>
</div>
<div style="border: 0pt none; margin: 0pt; padding: 0pt; overflow: hidden; z-index: 9999; font-style: normal; font-weight: normal; font-family: trebuchet ms,arial,helvetica,sans-serif; float: none; position: absolute; left: auto; top: 39.5px; line-height: normal; background-color: transparent; visibility: inherit; width: 10px; height: 10px; right: 50px;"><img style="border: 0pt none; margin: 0pt; padding: 0pt; font-style: normal; font-weight: normal; font-family: trebuchet ms,arial,helvetica,sans-serif; float: none; position: absolute; left: 0px; top: 0px; line-height: normal; background-color: transparent; visibility: inherit; z-index: 99999;" src="http://www.rankjit.com/extension/img/pointer.gif" alt="" /></div>
</div>
<p>What follows is a list of some very useful MySQL queries for use in projects, enjoy. I won&#8217;t go into detail how and why they work, I&#8217;ll leave that up to you <img src='http://www.web-design-talk.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<pre class="brush: sql; title: ; notranslate">
/*** select records from the previous day ***/
SELECT * FROM users WHERE TO_DAYS(last_login) = ( TO_DAYS(NOW()) - 1 )

/*** select records from last 90 minutes ***/
SELECT* FROM users DATE_SUB(NOW(),INTERVAL 90 MINUTE);

/*** select records from last 1hr 5 mins ***/
SELECT DATE_ADD('1970-01-01 12:00:00', INTERVAL CAST(6/4 AS DECIMAL(3,1)) HOUR_MINUTE);

/*** select records from last hour ***/
select DATE_SUB(NOW(), INTERVAL 1 HOUR);

/*** using the SIGN function to mark a number as positive, negative or null ***/
SELECT backlist, SIGN(backlist) AS user_to_backlist
FROM users
WHERE user_banned IS NOT NULL;

/*** select records from last week ***/
select DATE_SUB(NOW(), INTERVAL 1 WEEK);

/*** get the last day of next month ***/
SELECT LAST_DAY('2006-03-06' + INTERVAL 1 MONTH) AS last_day;

/*** select unique records only ***/
SELECT user_name FROM users GROUP BY users HAVING ( COUNT(user_name) = 1 );

/*** select records from one table that are in another table i.e. all the customers that have placed an order ***/
SELECT DISTINCT cust.customer_id, cust.customer_name
FROM cust INNER JOIN orders ON cust.customer_id = orders.customer_id;

/*** insert data from one table into another ***/
INSERT INTO customers(customer_id, customer_name)
SELECT cus_key, cus_name
FROM customers_2 WHERE customer_name LIKE 'W%';

/*** update information based upon a seperate table ***/
UPDATE cust SET status = '1'
FROM orders WHERE orderdate &gt; '2009-01-01' and orders.customer_id = cus.customer_id;

/*** classic self join example - who is an emoployees manager ***/
SELECT emp.empID, emp.Name, emp.Salary, managers.Name AS manager_name
FROM emp
LEFT JOIN emp AS manager_name
ON emp.ManagerID = Manager.EmployeeID
WHERE (emp.empID= '123456');

/*** using UNION to combine results from multiple queries into a single table ***/
SELECT users.name
FROM users WHERE (users.name BETWEEN 'A%' AND 'M%')
UNION
SELECT banned_users.name FROM banned_users
WHERE (banned_users.name BETWEEN 'A%' AND 'M%');

/*** concatenate column data into a single column ***/
SELECT CONCAT(emp.firstname, '-', emp.lastname) AS emp_full_name FROM emp;

/*** select count of records for each hour ***/
SELECT HOUR(last_login) AS last_login_hour, COUNT(*) AS the_count FROM users GROUP BY HOUR(last_login);

/*** import a csv file ***/
LOAD DATA INFILE '/path/xxx.csv' INTO users_table csv_test_table FIELDS TERMINATED BY ',' LINES TERMINATED BY &quot;\n&quot; (user_name, access_level , user_email);

/*** display current mysql user ***/
SELECT USER();
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.web-design-talk.co.uk/128/mysql-cheatsheet-useful-mysql-queries/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adding Unlimited Form Fields With JQuery and Saving to a Database</title>
		<link>http://www.web-design-talk.co.uk/58/adding-unlimited-form-fields-with-jquery-mysql/</link>
		<comments>http://www.web-design-talk.co.uk/58/adding-unlimited-form-fields-with-jquery-mysql/#comments</comments>
		<pubDate>Sat, 25 Jul 2009 20:36:49 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[JQuery]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[associative array]]></category>
		<category><![CDATA[dynamic form fields]]></category>
		<category><![CDATA[pgp]]></category>
		<category><![CDATA[sprintf]]></category>

		<guid isPermaLink="false">http://www.web-design-talk.co.uk/?p=58</guid>
		<description><![CDATA[In this article I'll discuss how to add an unlimited number of additional form elements to a form and then save to a database. The latter part is the key here as a variety of tutorials exist on adding form elements, but I have yet to see anywhere that actually explains how to manipulate these added form fields. For example, how to get values to store them in a MySQL datbase. In the example we'll have a simple user signup form where the user can add multiple fields to describe their favourite websites.  The basic Form HTML is as follows (nothing amazing, just a simple html form):]]></description>
			<content:encoded><![CDATA[<p>In this article I&#8217;ll discuss how to add an unlimited number of additional form elements to a form and then save to a database. The latter part is the key here as a variety of tutorials exist on adding form elements, but I have yet to see anywhere that actually explains how to manipulate these added form fields. For example, how to get values to store them in a MySQL datbase. In the example we&#8217;ll have a simple user signup form where the user can add multiple fields to describe their favourite websites.  The basic Form HTML is as follows (nothing amazing, just a simple html form):</p>
<pre class="brush: xml; highlight: [10,11,12]; title: ; notranslate">
&lt;script src=&quot;js/jquery.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
&lt;h1&gt;New User Signup&lt;/h1&gt;
&lt;form action=&quot;index.php&quot; method=&quot;post&quot;&gt;

  &lt;label for=&quot;name&quot;&gt;Username:&lt;/label&gt;
  &lt;input id=&quot;name&quot; name=&quot;name&quot; type=&quot;text&quot; /&gt;
  &lt;label for=&quot;name&quot;&gt;Password:&lt;/label&gt;
  &lt;input id=&quot;password&quot; name=&quot;password&quot; type=&quot;text&quot; /&gt;

   &lt;div id=&quot;container&quot;&gt;
      &lt;a href=&quot;#&quot;&gt;&lt;span&gt;» Add your favourite links.....&lt;/span&gt;&lt;/a&gt;
   &lt;/div&gt;

   &lt;input id=&quot;go&quot; class=&quot;btn&quot; name=&quot;btnSubmit&quot; type=&quot;submit&quot; value=&quot;Signup&quot; /&gt;
&lt;/form&gt;
</pre>
<p>The only part that isn&#8217;t standard is highlighted above. This is simply the link users click to add additional form fields on the fly. To make that happen we&#8217;ll need some <a href="http://www.JQuery" target="_blank">JQuery</a>:</p>
<pre class="brush: jscript; title: ; notranslate">
var count = 0;
$(function(){
	$('p#add_field').click(function(){
		count += 1;
		$('#container').append('&lt;strong&gt;Link #' + count + '&lt;/strong&gt;'+ '&lt;input id=&quot;field_' + count + '&quot; name=&quot;fields[]' + '&quot; type=&quot;text&quot; /&gt;' );
	});
});</pre>
<p><span id="more-58"></span></p>
<p>Jquery makes it very easy to add for elements using the <a href="http://docs.jquery.com/Manipulation/append" target="_blank">append </a>method. As we need to assign a unique name to each additional input (as we are allowing the user to add an indefinite amount of inputs) I&#8217;ll use a variable to keep track of the number of inputs added. Whenever an input is added, the counter variable increments by one. Another thing to notice is how the fields are named using &#8216;txt[]&#8216;. This creates an array of field names. So now when we click the &#8216;add_field&#8217; link, a new input field is appended to the contianer div &#8211; <a href="http://www.web-design-talk.co.uk/examples/2/1/" target="_self">view example</a>.</p>
<p>The only other order of business is to save this information to a database. This would be easy if we had static field names for the whole form, but we don&#8217;t. We have the following:</p>
<pre class="brush: xml; title: ; notranslate">&lt;input id=&quot;name&quot; name=&quot;name&quot; type=&quot;text&quot; /&gt;
&lt;input id=&quot;password&quot; name=&quot;password&quot; type=&quot;text&quot; /&gt;
&lt;input id=&quot;field_1&quot; name=&quot;fields[]&quot; type=&quot;text&quot; /&gt;
&lt;input id=&quot;field_2&quot; name=&quot;fields[]&quot; type=&quot;text&quot; /&gt;
&lt;input id=&quot;field_3&quot; name=&quot;fields[]&quot; type=&quot;text&quot; /&gt;
.
.
.
etc.</pre>
<p>To capture this dynamic fields we&#8217;ll need to use a bit of PHP to loop through our posted array and saving the data using a many-many relationships using the following database structure &#8211; allowing a user to be associated with an unlimited number of websites:</p>
<div class="wp-caption alignnone" style="width: 510px"><img title="Data Structure For Users And Sites" src="http://www.web-design-talk.co.uk/images/users_sites.jpg" alt="Many-Many Data Structure For Users And Sites" width="500" height="125" /><p class="wp-caption-text">Many-Many Data Structure For Users And Sites</p></div>
<p>The following PHP code will grab our static and dynamic form values and insert into the above data structure. We&#8217;ll be using a class for the database to minimise the amount of code written. Firstly we&#8217;ll check if the forma has actually been submitted, grab the static values (username and password) and insert these into our users table:</p>
<pre class="brush: php; title: ; notranslate">//If form was submitted
if (isset($_POST['btnSubmit'])) {

	//create instance of database class
	$db = new mysqldb();
	$db-&amp;gt;select_db();

	//Insert static values into users table
	$sql_user = sprintf(&quot;INSERT INTO users (Username, Password) VALUES ('%s','%s')&quot;,
						mysql_real_escape_string($_POST['name']),
						mysql_real_escape_string($_POST['password']) );
	$result_user = $db-&amp;gt;query($sql_user);</pre>
<p>PHP&#8217;s <a href="http://www.php.net/sprintf">sprintf</a> function was used to format the sql query. I also escape input to prevent <a href="http://en.wikipedia.org/wiki/SQL_injection">nasty things from happening</a>. Now we need to check if the user has actually added any favourite websites in order to prevent a php error if we try to find non-existant values, if they have added a field we&#8217;ll need the user id number previously inserted into the users table, as we&#8217;ll need to insert this elsewhere later on:</p>
<pre class="brush: php; title: ; notranslate">//Check if user has actually added additional fields to prevent a php error
	if ($_POST['fields']) {

		//get last inserted userid
		$inserted_user_id = $db-&amp;gt;last_insert_id();</pre>
<p>Next we loop through the added form field values. This is where naming the field as in our append function as &#8216;txt[]&#8216; comes in, as all fields we added are submiited as an array called txt. Using a for each loop we can assign values to all dynamically added fields and save them to database. The $key is simply the fields name (field_1) and the $key is our actual value entered for that field. Now we know this, we can easily generate the sql statements within the loop. Firstly we add the website into the websites table, get the id of the inserted website and insert this into the the link table, using the userid from before. To finish, we display a confirmation message and disconnect from the database:</p>
<pre class="brush: php; title: ; notranslate">//Loop through added fields
		foreach ( $_POST['fields'] as $key=&amp;gt;$value ) {

			//Insert into websites table
			$sql_website = sprintf(&quot;INSERT INTO websites (Website_URL) VALUES ('%s')&quot;,
						    	   mysql_real_escape_string($value) );
			$result_website = $db-&amp;gt;query($sql_website);
			$inserted_website_id = $db-&amp;gt;last_insert_id();

			//Insert into users_websites_link table
			$sql_users_website = sprintf(&quot;INSERT INTO users_websites_link (UserID, WebsiteID) VALUES ('%s','%s')&quot;,
						    	   mysql_real_escape_string($inserted_user_id),
								   mysql_real_escape_string($inserted_website_id) );
			$result_users_website = $db-&amp;gt;query($sql_users_website);

		}

	} else {

		//No additional fields added by user

	}
	echo &quot;
&lt;h1&gt;User Added, &lt;strong&gt;&quot; . count($_POST['fields']) . &quot;&lt;/strong&gt; website(s) added for this user!&lt;/h1&gt;
&quot;;

	//disconnect mysql connection
	$db-&amp;gt;kill();</pre>
<p>As a result, our database should be populated as follows (these are screenshots from phpmyadmin):</p>
<div class="wp-caption alignnone" style="width: 510px"><img class=" " title="Many to many relationship" src="http://www.web-design-talk.co.uk/images/users_websites_data.jpg" alt="All our data tables are populated and the correct website and user id are inserted into the link table" width="500" height="278" /><p class="wp-caption-text">All our data tables are populated and the correct website and user id are inserted into the link table</p></div>
<p>You can view the <a href="http://www.web-design-talk.co.uk/examples/2/index-source.php">source of php file here</a> or <a href="http://www.web-design-talk.co.uk/examples/2/adding-form-elements-with-jquery.zip">download the original source files</a> (you&#8217;ll need to open classes/db.class.php and populate the sql variables with your own database details). The MySQL source to create the tables is also included in the source files.</p>
<p>&nbsp;</p>
<p><a href="http://www.b2b-directory-uk.co.uk/" target="_blank">UK Business Directory</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.web-design-talk.co.uk/58/adding-unlimited-form-fields-with-jquery-mysql/feed/</wfw:commentRss>
		<slash:comments>64</slash:comments>
		</item>
	</channel>
</rss>

