<?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>Coding Friends &#187; SQL (Structured Query Language)</title>
	<atom:link href="http://www.codingfriends.com/index.php/category/programming/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codingfriends.com</link>
	<description>Coding Friends, place for developers.</description>
	<lastBuildDate>Sun, 04 Dec 2011 21:11:08 +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>MySQL storage engines</title>
		<link>http://www.codingfriends.com/index.php/2010/08/12/mysql-storage-engines/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=mysql-storage-engines</link>
		<comments>http://www.codingfriends.com/index.php/2010/08/12/mysql-storage-engines/#comments</comments>
		<pubDate>Thu, 12 Aug 2010 13:00:38 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[SQL (Structured Query Language)]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Storage engines]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=1158</guid>
		<description><![CDATA[In MySQL, there is a couple of different storage engines that can be used for different tables within a database. There is myiasm, innodb, cvs, memory, merge, BDB (BerkeleyDB) example, federated, archive and blackhole. The main two are that are used are myiasm innodb The main differences between them both are speed, the myiasm is [...]]]></description>
			<content:encoded><![CDATA[<p>In MySQL, there is a couple of different <a href="http://dev.mysql.com/doc/refman/5.0/en/storage-engines.html">storage engines</a> that can be used for different tables within a database.  There is myiasm, innodb, cvs, memory, merge, BDB (BerkeleyDB) example, federated, archive and blackhole.</p>
<p>The main two are that are used are </p>
<ul>
<li><a href="http://dev.mysql.com/doc/refman/5.0/en/myisam-storage-engine.html">myiasm</a></li>
<li><a href="http://dev.mysql.com/doc/refman/5.0/en/innodb.html">innodb</a></li>
</ul>
<p>The main differences between them both are speed, the myiasm is more designed for speed to access the database tables but does not support the rollback where you are able stop the current set of instructions and rollback the updates/inserts to where it was at a previous time, because of this the programmer is the person to make sure that what they want to do is and will happen, otherwise the programmer will have to sort out the tables that could out of stink.  But compared to the innodb, where it supports the rollback function, it does have to take a hit somewhere and that is the speed.</p>
<p>The main database that I use is the myiasm, purely because of the speed of access, and also it appears that most web/applications appear to use that.  But if you running some circuital data then of course I would use innodb database type purely for data.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/08/12/mysql-storage-engines/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CS75 &#8211; Ass1 &#8211; Finance</title>
		<link>http://www.codingfriends.com/index.php/2010/08/11/cs75-ass1-finance/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=cs75-ass1-finance</link>
		<comments>http://www.codingfriends.com/index.php/2010/08/11/cs75-ass1-finance/#comments</comments>
		<pubDate>Wed, 11 Aug 2010 14:57:53 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[SQL (Structured Query Language)]]></category>
		<category><![CDATA[Assignment 1]]></category>
		<category><![CDATA[CS75]]></category>
		<category><![CDATA[Finance]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=1143</guid>
		<description><![CDATA[I am doing the Harvards building dynamic websites called CS-75 (also could be called E-75), because someone told me about it and I just thought might as well, it is all learning even if allot of it you may already know. I have done the previous project here Three aces, where it was to develop [...]]]></description>
			<content:encoded><![CDATA[<p><span id="zipfile"><a href="http://www.codingfriends.com/wp-content/uploads/2010/08/cs75-ass1.zip"></a></span>I am doing the <a href="http://www.harvard.edu/">Harvards</a> building dynamic websites called <a href="http://www.cs75.net/">CS-75</a> (also could be called E-75), because someone told me about it and I just thought might as well, it is all learning <img src='http://www.codingfriends.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  even if allot of it you may already know.</p>
<p>I have done the previous project here <a href="http://www.codingfriends.com/index.php/2010/07/23/cs75-assignment-0-three-aces-menu/">Three aces</a>, where it was to develop a menu system using simpleXML.</p>
<p>This project is to communicate with the <a href="http://uk.finance.yahoo.com/">Yahoo finance</a> website, and bring back details of stock prices for different companies, and also to keep track of the users purchases and also allow them to sell the stock that they have purchased, I have attached the PDF of the assignment if you want more information about the assignment.</p>
<p>Since it is a bit bigger than the previous project I am going to split each part into a different post, so this one is going to be about the database setup.  So to start with here is the details of the database tables, I have included the full source code and sql file to create the database within <a href="http://www.mysql.com/">MySQL</a>, here is what phpmyadmin export created, I did not include the trigger on the users table that was creating a <a href="http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_uuid">UUID</a>, because some versions of MySQL does not support that syntax</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">--</span>
<span style="color: #808080; font-style: italic;">-- Table structure for table `stocks`</span>
<span style="color: #808080; font-style: italic;">--</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> <span style="color: #ff0000;">`stocks`</span>;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> <span style="color: #ff0000;">`stocks`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`UID`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'0'</span> COMMENT <span style="color: #ff0000;">'Link to users table'</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`SYMBOL`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`Quantity`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #ff0000;">`UID`</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`UID`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>InnoDB <span style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET<span style="color: #66cc66;">=</span>latin1;
&nbsp;
<span style="color: #808080; font-style: italic;">--</span>
<span style="color: #808080; font-style: italic;">-- Dumping data for table `stocks`</span>
<span style="color: #808080; font-style: italic;">--</span>
&nbsp;
<span style="color: #808080; font-style: italic;">-- --------------------------------------------------------</span>
&nbsp;
<span style="color: #808080; font-style: italic;">--</span>
<span style="color: #808080; font-style: italic;">-- Table structure for table `users`</span>
<span style="color: #808080; font-style: italic;">--</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> <span style="color: #ff0000;">`users`</span>;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> <span style="color: #ff0000;">`users`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`UID`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`username`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`pass`</span> <span style="color: #993333; font-weight: bold;">BLOB</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`cash`</span> <span style="color: #993333; font-weight: bold;">DECIMAL</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'10000.00'</span> COMMENT <span style="color: #ff0000;">'the default value is the free gift'</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`GUID`</span> <span style="color: #993333; font-weight: bold;">CHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">36</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> COMMENT <span style="color: #ff0000;">'Use this string to validate the user, if the value is 0, then validated'</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`UID`</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">UNIQUE</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #ff0000;">`username`</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`username`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>InnoDB  <span style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET<span style="color: #66cc66;">=</span>latin1 <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">11</span> ;
&nbsp;
<span style="color: #808080; font-style: italic;">--</span>
<span style="color: #808080; font-style: italic;">-- Constraints for table `stocks`</span>
<span style="color: #808080; font-style: italic;">--</span>
<span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`stocks`</span>
  <span style="color: #993333; font-weight: bold;">ADD</span> <span style="color: #993333; font-weight: bold;">CONSTRAINT</span> <span style="color: #ff0000;">`stocks_ibfk_1`</span> <span style="color: #993333; font-weight: bold;">FOREIGN</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`UID`</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">REFERENCES</span> <span style="color: #ff0000;">`users`</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`UID`</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">DELETE</span> CASCADE;</pre></div></div>

<p>If you are running the latest version of MySQL then you can have the trigger on the database table <img src='http://www.codingfriends.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> , and just need to take out the uncomment code within the users.php file!.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- Triggers `users`</span>
<span style="color: #808080; font-style: italic;">--</span>
<span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">TRIGGER</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> <span style="color: #ff0000;">`users_insert`</span>;
DELIMITER <span style="color: #66cc66;">//</span>
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TRIGGER</span> <span style="color: #ff0000;">`users_insert`</span> <span style="color: #993333; font-weight: bold;">BEFORE</span> <span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #ff0000;">`users`</span>
 <span style="color: #993333; font-weight: bold;">FOR</span> EACH <span style="color: #993333; font-weight: bold;">ROW</span> <span style="color: #993333; font-weight: bold;">BEGIN</span>
<span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #993333; font-weight: bold;">NEW</span><span style="color: #66cc66;">.</span>guid <span style="color: #66cc66;">=</span> uuid<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">END</span>
<span style="color: #66cc66;">//</span>
DELIMITER ;</pre></div></div>

<p>Here is my database.php file where I connect to the database and also perform some of the required actions, like queries etc and also since I am using the InnoDB MySQL engine, then I can use the START TRANSACTION which will allow the ROLLBACK function within mysql so that if any of the SQL between them do not work, then I can roll back to where I was before I started that transaction, or commit the sql to the database.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #000000; font-weight: bold;">class</span> mysqlConnect
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$connectionHost</span><span style="color: #339933;">,</span> <span style="color: #000088;">$connectionUser</span><span style="color: #339933;">,</span> <span style="color: #000088;">$connectionPW</span><span style="color: #339933;">,</span> <span style="color: #000088;">$connectionDB</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">theConnection</span> <span style="color: #339933;">=</span> 
				<span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$connectionHost</span><span style="color: #339933;">,</span> <span style="color: #000088;">$connectionUser</span><span style="color: #339933;">,</span> <span style="color: #000088;">$connectionPW</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span>
				<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Problems - connection to the database, please check&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$connectionDB</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">theConnection</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span>
				<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Problems - connected to database engine, but not the database&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">theConnection</span><span style="color: #009900;">&#41;</span>
				<span style="color: #990000;">mysql_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">theConnection</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> query<span style="color: #009900;">&#40;</span><span style="color: #000088;">$stringQuery</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$q</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stringQuery</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stringQuery</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// if the database supports the result of returning the number of rows effected with the last call.</span>
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> rowsNumber<span style="color: #009900;">&#40;</span><span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #990000;">mysql_num_rows</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> rowsAffected<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #990000;">mysql_affected_rows</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> arrayResults<span style="color: #009900;">&#40;</span><span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> freeResult<span style="color: #009900;">&#40;</span><span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #990000;">mysql_free_result</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> startTransaction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;START TRANSACTION&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> commitTransaction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;COMMIT&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> rollbackTransaction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ROLLBACK&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$theConnection</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Shall does the classes next of the project and then the actual php files that do the user interaction.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/08/11/cs75-ass1-finance/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Unique fileds Foreign keys duplicate key and Joins</title>
		<link>http://www.codingfriends.com/index.php/2010/08/06/joins_foreign_key/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=joins_foreign_key</link>
		<comments>http://www.codingfriends.com/index.php/2010/08/06/joins_foreign_key/#comments</comments>
		<pubDate>Fri, 06 Aug 2010 10:15:38 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[SQL (Structured Query Language)]]></category>
		<category><![CDATA[duplicate key]]></category>
		<category><![CDATA[foreign key]]></category>
		<category><![CDATA[inner]]></category>
		<category><![CDATA[joins]]></category>
		<category><![CDATA[left]]></category>
		<category><![CDATA[outer]]></category>
		<category><![CDATA[right]]></category>
		<category><![CDATA[unique key]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=1130</guid>
		<description><![CDATA[I have been asked about some SQL questions, so I thought that I would post on here encase it helps others as well. The questions was based around, what is the unique key compared to a primary key, foreign keys (and how to link) and also the joins within a relationship database(outer, inner, left, right). [...]]]></description>
			<content:encoded><![CDATA[<p>I have been asked about some SQL questions, so I thought that I would post on here encase it helps others as well.  The questions was based around, what is the unique key compared to a primary key, foreign keys (and how to link) and also the joins within a relationship database(outer, inner, left, right).</p>
<p><strong>Unique key</strong></p>
<p>An unique key is kinder similar to a primary key, where the primary key is the main index into the table itself (kinder like a index at the back of the book, where is item 5, arrh there in the book) well the unique key basically means that this column within the table cannot have a similar value within this column compared to any other values stored in other rows in the table.  So for example,</p>
<table>
<tr>
<td>Index</td>
<td>UniqueKey</td>
</tr>
<tr>
<td>1</td>
<td>genux</td>
</tr>
<tr>
<td>2</td>
<td>bob</td>
</tr>
</table>
<p>now if I tried to insert a name into the uniquekey field name of &#8220;genux&#8221; it would complain because there is already a field name of that.</p>
<p><strong>Foreign key</strong></p>
<p>A foreign key is a value that is linked to another table that if that other table ( which normally is the main table of lets say customers) deletes a row from it, then you can setup in the second table (where the foreign key is) to also delete data from its table where they are linked with the foreign key (this can also happen on updates as well instead of deleting data).</p>
<p>So as taken from <a href="http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html">this</a> page on the mysql website.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> parent <span style="color: #66cc66;">&#40;</span>id <span style="color: #993333; font-weight: bold;">INT</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
                     <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>INNODB;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> child <span style="color: #66cc66;">&#40;</span>id <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">,</span> parent_id <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">,</span>
                    <span style="color: #993333; font-weight: bold;">INDEX</span> par_ind <span style="color: #66cc66;">&#40;</span>parent_id<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
                    <span style="color: #993333; font-weight: bold;">FOREIGN</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>parent_id<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">REFERENCES</span> parent<span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>
                      <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">DELETE</span> CASCADE
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>INNODB;</pre></div></div>

<p>The foreign key is the parent_id field within the child table where it links to the parent tables id field and with the &#8220;ON DELETE CASCADE&#8221; it will delete any rows within the childs table if the parent table deletes its linking row.</p>
<p><strong>Duplicate keys</strong></p>
<p><a href="http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html">Duplicate keys</a> are nice, when you want to insert some data into a table and the index key is already present then you can update the value within the already present row in the table and thus get around the testing for any duplicate keys, you can also use the <a href="http://dev.mysql.com/doc/refman/5.0/en/replace.html">REPLACE</a> syntax as well (but this will not allow you to increment the present value within the field, but update it to a new value). </p>
<p><strong>Joins, left,right, inner, outer</strong></p>
<p>Joins are basically in mathematical terms linking sets of data together, so a inner join</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> table1 <span style="color: #993333; font-weight: bold;">INNER</span> <span style="color: #993333; font-weight: bold;">JOIN</span> table2 <span style="color: #993333; font-weight: bold;">ON</span> table1<span style="color: #66cc66;">.</span>linkId <span style="color: #66cc66;">=</span> table2<span style="color: #66cc66;">.</span>Id</pre></div></div>

<p>This will bring back only the data from both tables that have the linking column values from table1.linkId and table2.Id, in contrast to outer join</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> table1 <span style="color: #993333; font-weight: bold;">OUTER</span> <span style="color: #993333; font-weight: bold;">JOIN</span> table2 <span style="color: #993333; font-weight: bold;">ON</span> table1<span style="color: #66cc66;">.</span>linkId <span style="color: #66cc66;">=</span> table2<span style="color: #66cc66;">.</span>Id</pre></div></div>

<p>where all of the values from within the two tables are outputted in the results but values of NULL are inserted into the unmatched result fields.</p>
<p>There is also, left and right join where it is similar to the outer join as in</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> table1 <span style="color: #993333; font-weight: bold;">LEFT</span> <span style="color: #993333; font-weight: bold;">JOIN</span> table2 <span style="color: #993333; font-weight: bold;">ON</span> table1<span style="color: #66cc66;">.</span>linkId <span style="color: #66cc66;">=</span> table2<span style="color: #66cc66;">.</span>Id</pre></div></div>

<p>But only all of the values from the left table are still present in the results output and NULLs inserted into the fields where there is no link with the right had side table, of course the values from the right table are inserted where there is a link with the left table, and the right join is similar to the left join apart from all rows are in the results from the right table instead of the left. As a side note, the left table is the table on the left hand side of the join syntax so table1 in this instance.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/08/06/joins_foreign_key/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Web page inputs and insert into database &#8211; Part 2</title>
		<link>http://www.codingfriends.com/index.php/2010/03/16/web-page-inputs-and-insert-into-database-part-2/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=web-page-inputs-and-insert-into-database-part-2</link>
		<comments>http://www.codingfriends.com/index.php/2010/03/16/web-page-inputs-and-insert-into-database-part-2/#comments</comments>
		<pubDate>Tue, 16 Mar 2010 21:02:51 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[HTML  & CSS]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[SQL (Structured Query Language)]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[datainsert]]></category>
		<category><![CDATA[sanitize]]></category>
		<category><![CDATA[web page]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=812</guid>
		<description><![CDATA[As from here, where I outlined the javascript, html part of the exercised here is the php and mysql parts of the problem. Here is the table that I created within MySQL CREATE TABLE `User` &#40; `id` INT&#40;11&#41; NOT NULL AUTO_INCREMENT, `firstName` VARCHAR&#40;50&#41; DEFAULT NULL, `lastName` VARCHAR&#40;50&#41; DEFAULT NULL, `email` VARCHAR&#40;50&#41; DEFAULT NULL, `phoneNum` VARCHAR&#40;20&#41; [...]]]></description>
			<content:encoded><![CDATA[<p>As from <a href="http://www.codingfriends.com/index.php/2010/03/16/web-page-inputs-and-insert-into-database/">here</a>, where I outlined the javascript, html part of the exercised here is the php and mysql parts of the problem.</p>
<p>Here is the table that I created within <a href="http://www.mysql.com/">MySQL</a></p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`User`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`id`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`firstName`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`lastName`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`email`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`phoneNum`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`guid`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">36</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>MyISAM <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">9</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET<span style="color: #66cc66;">=</span>latin1;</pre></div></div>

<p>As from my other post about MySQL <a href="http://www.codingfriends.com/index.php/2010/03/11/mysql-triggers/">triggers</a>, so to find out more information about why please check out that page, and here is the trigger that I created for the above table.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TRIGGER</span> User_insert 
<span style="color: #993333; font-weight: bold;">BEFORE</span> <span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">USER</span> 
<span style="color: #993333; font-weight: bold;">FOR</span> EACH <span style="color: #993333; font-weight: bold;">ROW</span> 
<span style="color: #993333; font-weight: bold;">BEGIN</span> 
        <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #993333; font-weight: bold;">NEW</span><span style="color: #66cc66;">.</span>guid <span style="color: #66cc66;">=</span> uuid<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; 
<span style="color: #993333; font-weight: bold;">END</span></pre></div></div>

<p>The reason why I created this for was because within the exercise they asked &#8220;would forward planning, i.e. adding a unique code to the user details that could be used to validate their email address&#8221;, which by using a uuid, you can use that as a unique code.</p>
<p>so the only part left is the PHP that will connect to the database and also sanitize the inputs within sql insert. So created a class that has a constructor that will open a database connection to a database</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">      <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$dbLink</span><span style="color: #339933;">;</span>
      <span style="color: #666666; font-style: italic;">/* connect to the database*/</span>
      <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;user&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;password&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$database</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;database&quot;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbLink</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$user</span><span style="color: #339933;">,</span> <span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbLink</span><span style="color: #009900;">&#41;</span> <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;no database present&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$database</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbLink</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	  <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;no database within the database&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here I try to sanitize the insert string so that it will use the <a href="http://uk3.php.net/mysql_real_escape_string">mysql_real_escape_string</a>, this will use a php function that helps with SQL injections, also I am using the <a href="http://uk.php.net/stripslashes">stripslashes</a> and then <a href="http://uk.php.net/trim">trim</a>  the string to make sure that there is no white spaces (or any valid text left).</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">      <span style="color: #666666; font-style: italic;">/* santize the input for a mysql database */</span>
      <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> santizeInput<span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">stripslashes</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span></pre></div></div>

<p>and then to insert the data, just use the <a href="http://uk.php.net/mysql_query">mysql_query</a> and the sanitize function above to insert data into the database.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">      <span style="color: #666666; font-style: italic;">/* insert the data into the database */</span>
      <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> insertData<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sqlData</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">santizeInput</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sqlData</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbLink</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error inserting data&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span></pre></div></div>

<p>And here is where I get the data from the form post from the web page and then double sanitize the data and then create a database link, which in-turn use to insert the data.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #666666; font-style: italic;">// could use foreach loop $_POST inputs, but I personally prefer to pick them up.</span>
    <span style="color: #000088;">$firstname</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'firstname'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$secondname</span> <span style="color: #339933;">=</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'secondname'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$email</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$phonenumber</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'phonenumber'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">/* could do additional checks on input incase it is sent via backend POST and not via the webpage,  could do with regular expression as well ? */</span>
    <span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> databaseAccess<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;username&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;password&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;database&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">/* can santize the inputs to make sure that there is some data to &quot;play&quot; with */</span>
    <span style="color: #000088;">$firstname</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">santizeInput</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$firstname</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$secondname</span> <span style="color: #339933;">=</span><span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">santizeInput</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$secondname</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$email</span> <span style="color: #339933;">=</span><span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">santizeInput</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$phonenumber</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">santizeInput</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$phonenumber</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>checkLength<span style="color: #009900;">&#40;</span><span style="color: #000088;">$firstname</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> checkLength<span style="color: #009900;">&#40;</span><span style="color: #000088;">$secondname</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>	
	<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;insert into User (firstname, lastname, email, phoneNum) values (<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">$firstname</span><span style="color: #000099; font-weight: bold;">\&quot;</span>,<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">$secondname</span><span style="color: #000099; font-weight: bold;">\&quot;</span>,<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">$email</span><span style="color: #000099; font-weight: bold;">\&quot;</span>, <span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">$phonenumber</span><span style="color: #000099; font-weight: bold;">\&quot;</span>)&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">insertData</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Data inserted&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>I did write within the exercise that since someone may try and post the data to the server within using the webpage (naughty people that they are!!) you could also check the inputs again for there data validity.</p>
<p>Here is the full code for the web page in total.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
    <span style="color: #000000; font-weight: bold;">class</span> databaseAccess
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$dbLink</span><span style="color: #339933;">;</span>
      <span style="color: #666666; font-style: italic;">/* connect to the database*/</span>
      <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;user&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;password&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$database</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;database&quot;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbLink</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$user</span><span style="color: #339933;">,</span> <span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbLink</span><span style="color: #009900;">&#41;</span> <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;no database present&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$database</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbLink</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	  <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;no database within the database&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">/* disconnect */</span>
      <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbLink</span><span style="color: #009900;">&#41;</span> <span style="color: #990000;">mysql_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbLink</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">/* santize the input for a mysql database */</span>
      <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> santizeInput<span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">stripslashes</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">/* insert the data into the database */</span>
      <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> insertData<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sqlData</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">santizeInput</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sqlData</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbLink</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error inserting data&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">function</span> checkLength<span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> 
	<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span> 
      <span style="color: #b1b100;">else</span> 
	<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// could use foreach loop $_POST inputs, but I personally prefer to pick them up.</span>
    <span style="color: #000088;">$firstname</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'firstname'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$secondname</span> <span style="color: #339933;">=</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'secondname'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$email</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$phonenumber</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'phonenumber'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">/* could do additional checks on input incase it is sent via backend POST and not via the webpage, not sure if SOAP are looking for that as well ? 
      could do with regular expression as well ? */</span>
    <span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> databaseAccess<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;User&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;PW&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Test&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">/* can santize the inputs to make sure that there is some data to &quot;play&quot; with */</span>
    <span style="color: #000088;">$firstname</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">santizeInput</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$firstname</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$secondname</span> <span style="color: #339933;">=</span><span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">santizeInput</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$secondname</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$email</span> <span style="color: #339933;">=</span><span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">santizeInput</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$phonenumber</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">santizeInput</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$phonenumber</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>checkLength<span style="color: #009900;">&#40;</span><span style="color: #000088;">$firstname</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> checkLength<span style="color: #009900;">&#40;</span><span style="color: #000088;">$secondname</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>	
	<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;insert into User (firstname, lastname, email, phoneNum) values (<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">$firstname</span><span style="color: #000099; font-weight: bold;">\&quot;</span>,<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">$secondname</span><span style="color: #000099; font-weight: bold;">\&quot;</span>,<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">$email</span><span style="color: #000099; font-weight: bold;">\&quot;</span>, <span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">$phonenumber</span><span style="color: #000099; font-weight: bold;">\&quot;</span>)&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">insertData</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Data inserted&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt;
&lt;html&gt;
&lt;head&gt;
&lt;script language=&quot;javascript&quot;&gt;
  /* check the length of the element, focus is none present */
  function lengthCheck(elem, thename)
  {
    if (elem.value.length&gt; 0) 
      return true;
    else
    {
      alert(&quot;Please insert the &quot; + thename);
      elem.focus();
    }
  }
&nbsp;
  /* check a email address, using regular expression */
  function emailChecker(elem)
  {
    var reg = /^[\w\-\.\+]+\@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    if (elem.value.match(reg))
      return true;
    else
    {
      alert (&quot;Please insert a valid email address&quot;);
      elem.focus();
      return false;
    }
  }
&nbsp;
  /* check against a phone number. a number being between 11-15 numbers*/
  function checkPhone(elem)
  {
    var reg = /^[0-9]{11,15}$/;
    if (elem.value.match(reg))
      return true;
    else
    {
      alert (&quot;Please insert a valid phone number&quot;);
      elem.focus();
      return false;
    }
  }
&nbsp;
  function checkInputs()
  {
    // obtain inputs
    var firstname = document.getElementsByName(&quot;firstname&quot;).item(0);
    var secondname = document.getElementsByName(&quot;secondname&quot;).item(0);
    var email= document.getElementsByName(&quot;email&quot;).item(0);
    var phonenum = document.getElementsByName(&quot;phonenumber&quot;).item(0);
    /* check the inputs */
    if (lengthCheck(firstname, &quot;first name&quot;)) 
      if (lengthCheck(secondname, &quot;second name&quot;))
	if (emailChecker(email))
	  if (checkPhone(phonenum))
	    return true;
    return false;
  }
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form name=&quot;input&quot; action=&quot;insertData.php&quot; method=&quot;post&quot; onSubmit=&quot;return checkInputs()&quot;&gt;
First Name :
&lt;input type=&quot;text&quot; name=&quot;firstname&quot;/&gt;
&nbsp;
Second Name : 
&lt;input type=&quot;text&quot; name=&quot;secondname&quot;/&gt;
&nbsp;
Email : 
&lt;input type=&quot;text&quot; name=&quot;email&quot;/&gt;
&nbsp;
Phone number : 
&lt;input type=&quot;text&quot; name=&quot;phonenumber&quot;/&gt;
&nbsp;
&lt;input type=&quot;submit&quot; value=&quot;Submit&quot;/&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>If you save that as insertData.php then open up within your web-server.  You will be able to insert data into a database with some javascript / php checks.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/03/16/web-page-inputs-and-insert-into-database-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Web page inputs and insert into database</title>
		<link>http://www.codingfriends.com/index.php/2010/03/16/web-page-inputs-and-insert-into-database/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=web-page-inputs-and-insert-into-database</link>
		<comments>http://www.codingfriends.com/index.php/2010/03/16/web-page-inputs-and-insert-into-database/#comments</comments>
		<pubDate>Tue, 16 Mar 2010 20:38:04 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[HTML  & CSS]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[SQL (Structured Query Language)]]></category>
		<category><![CDATA[data insert]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[santize]]></category>
		<category><![CDATA[web page]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=809</guid>
		<description><![CDATA[Also from the other posts, Join files together and simple calculator , I was also asked to do &#8220;Accept form input for a user registration form, and store results in a MySQL table, using PHP. Applicant should demonstrate a knowledge of input validation, and using server-side code to avoid sql injection exploits. The user data [...]]]></description>
			<content:encoded><![CDATA[<p>Also from the other posts, <a href="http://www.codingfriends.com/index.php/2010/03/11/join-files-together-into-a-single-file-and-expand-out/">Join files together</a> and <a href="http://www.codingfriends.com/index.php/2010/03/11/simple-calculator/">simple calculator</a> , I was also asked to do</p>
<p>&#8220;Accept form input for a user registration form, and store results in a MySQL table, using PHP. Applicant should demonstrate a knowledge of input validation, and using server-side code to avoid sql injection exploits. The user data should include first name, last name, email, an phone number.</p>
<p>Usage of Javascript pre-validation would be a plus, as would forward planning, i.e. adding a unique code to the user details that could be used to validate their email address. Suitable MySQL table schema should be demonstrated.&#8221;</p>
<p>To start with, I started at the data insert within a web page</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;input&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;insertData.php&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span> <span style="color: #000066;">onSubmit</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;return checkInputs()&quot;</span>&gt;</span>
First Name :
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;firstname&quot;</span><span style="color: #66cc66;">/</span>&gt;</span>
Second Name : 
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;secondname&quot;</span><span style="color: #66cc66;">/</span>&gt;</span>
Email : 
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;email&quot;</span><span style="color: #66cc66;">/</span>&gt;</span>
Phone number : 
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;phonenumber&quot;</span><span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Submit&quot;</span><span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span></pre></div></div>

<p>Which takes in the required input&#8217;s and also within the <a href="http://www.w3schools.com/TAGS/tag_form.asp">form</a> HTML tag, but before sending to the back end PHP part of the exercise, there was a requirement to do some Javascript checking on the inputs.</p>
<p>So here is the javascript that will check the inputs, within the form onsubmit action I call this function checkInputs and the return value (true/false) is returned which gives the form either a action to post back to the server (true return) or to wait for the user correct there inputs (false return).</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">    <span style="color: #003366; font-weight: bold;">var</span> firstname <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementsByName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;firstname&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I get the data from the webpage, <a href="http://www.java2s.com/Code/JavaScriptReference/Javascript-Methods/getElementsByName.htm">getElementsByName</a> (which since it is a name there could be x amount of elements with that name, so I want the first one (.item(0))</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #003366; font-weight: bold;">function</span> checkInputs<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// obtain inputs</span>
    <span style="color: #003366; font-weight: bold;">var</span> firstname <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementsByName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;firstname&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> secondname <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementsByName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;secondname&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> email<span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementsByName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;email&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> phonenum <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementsByName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;phonenumber&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009966; font-style: italic;">/* check the inputs */</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lengthCheck<span style="color: #009900;">&#40;</span>firstname<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;first name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> 
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lengthCheck<span style="color: #009900;">&#40;</span>secondname<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;second name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>emailChecker<span style="color: #009900;">&#40;</span>email<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>checkPhone<span style="color: #009900;">&#40;</span>phonenum<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>and then after getting the elements, I then call different additional functions that I did write within javascript to check the inputs gained.  Here I check the length of a element passed within the one of the parameters within the function parameter list, with using objects you can access the objects value.length (javascript object of a element) and also use the focus function with the element which will focus the element on the webpage for the user to know where to check there input (also with a alert window to say why, e.g. &#8220;please insert some data&#8221;.)</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #003366; font-weight: bold;">function</span> lengthCheck<span style="color: #009900;">&#40;</span>elem<span style="color: #339933;">,</span> thename<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>elem.<span style="color: #660066;">value</span>.<span style="color: #660066;">length</span><span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> 
      <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">else</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Please insert the &quot;</span> <span style="color: #339933;">+</span> thename<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      elem.<span style="color: #000066;">focus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here is a way of using <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> to check email inputs, basically it first checks to make sure there is a name before the &#8220;@&#8221; and also a at between 2 and 4 names with a &#8220;.&#8221; better them.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #009966; font-style: italic;">/* check a email address, using regular expression */</span>
  <span style="color: #003366; font-weight: bold;">function</span> emailChecker<span style="color: #009900;">&#40;</span>elem<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> reg <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/^[\w\-\.\+]+\@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>elem.<span style="color: #660066;">value</span>.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span>reg<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">else</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000066;">alert</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Please insert a valid email address&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      elem.<span style="color: #000066;">focus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here is a similar way as above for checking a phone number input between 11-15 numbers</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #009966; font-style: italic;">/* check against a phone number. a number being between 11-15 numbers*/</span>
  <span style="color: #003366; font-weight: bold;">function</span> checkPhone<span style="color: #009900;">&#40;</span>elem<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> reg <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/^[0-9]{11,15}$/</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>elem.<span style="color: #660066;">value</span>.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span>reg<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">else</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000066;">alert</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Please insert a valid phone number&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      elem.<span style="color: #000066;">focus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is the web page part, and here is the second part where I insert the data into database with php.</p>
<p>But here is the full web page part of the first part.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">language</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;javascript&quot;</span>&gt;</span>
  /* check the length of the element, focus is none present */
  function lengthCheck(elem, thename)
  {
    if (elem.value.length&gt; 0) 
      return true;
    else
    {
      alert(&quot;Please insert the &quot; + thename);
      elem.focus();
    }
  }
&nbsp;
  /* check a email address, using regular expression */
  function emailChecker(elem)
  {
    var reg = /^[\w\-\.\+]+\@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    if (elem.value.match(reg))
      return true;
    else
    {
      alert (&quot;Please insert a valid email address&quot;);
      elem.focus();
      return false;
    }
  }
&nbsp;
  /* check against a phone number. a number being between 11-15 numbers*/
  function checkPhone(elem)
  {
    var reg = /^[0-9]{11,15}$/;
    if (elem.value.match(reg))
      return true;
    else
    {
      alert (&quot;Please insert a valid phone number&quot;);
      elem.focus();
      return false;
    }
  }
&nbsp;
  function checkInputs()
  {
    // obtain inputs
    var firstname = document.getElementsByName(&quot;firstname&quot;).item(0);
    var secondname = document.getElementsByName(&quot;secondname&quot;).item(0);
    var email= document.getElementsByName(&quot;email&quot;).item(0);
    var phonenum = document.getElementsByName(&quot;phonenumber&quot;).item(0);
    /* check the inputs */
    if (lengthCheck(firstname, &quot;first name&quot;)) 
      if (lengthCheck(secondname, &quot;second name&quot;))
	if (emailChecker(email))
	  if (checkPhone(phonenum))
	    return true;
    return false;
  }
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;input&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;insertData.php&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span> <span style="color: #000066;">onSubmit</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;return checkInputs()&quot;</span>&gt;</span>
First Name :
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;firstname&quot;</span><span style="color: #66cc66;">/</span>&gt;</span>
&nbsp;
Second Name : 
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;secondname&quot;</span><span style="color: #66cc66;">/</span>&gt;</span>
&nbsp;
Email : 
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;email&quot;</span><span style="color: #66cc66;">/</span>&gt;</span>
&nbsp;
Phone number : 
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;phonenumber&quot;</span><span style="color: #66cc66;">/</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Submit&quot;</span><span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/03/16/web-page-inputs-and-insert-into-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL &#8211; triggers</title>
		<link>http://www.codingfriends.com/index.php/2010/03/11/mysql-triggers/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=mysql-triggers</link>
		<comments>http://www.codingfriends.com/index.php/2010/03/11/mysql-triggers/#comments</comments>
		<pubDate>Thu, 11 Mar 2010 11:58:16 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[SQL (Structured Query Language)]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[triggers]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=801</guid>
		<description><![CDATA[In MySQL version 5 onwards you can use Triggers, triggers are a nice was of checking values within a insert/update/delete process. A trigger happens either just before the insert actually happens into the database, or just after Lets say that you create a table as below CREATE TABLE testTable &#40;id INT NOT NULL AUTO_INCREMENT, guid [...]]]></description>
			<content:encoded><![CDATA[<p>In MySQL version 5 onwards you can use <a href="http://dev.mysql.com/doc/refman/5.5/en/triggers.html">Triggers</a>, triggers are a nice was of checking values within a insert/update/delete process.  A trigger happens either just before the insert actually happens into the database, or just after</p>
<p>Lets say that you create a table as below</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> testTable 
<span style="color: #66cc66;">&#40;</span>id <span style="color: #993333; font-weight: bold;">INT</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span> 
  guid <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">36</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> 
  name <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> 
<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>and then if you want to insert a name into the name column you could do</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> testTable <span style="color: #66cc66;">&#40;</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;thename&quot;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>but you are not inserting the anything into the guid column because you can force a value into that column with a trigger (the id is already creating a value with the primary key, auto_increment).</p>
<p>To create a trigger you need to set the delimiter to &#8220;//&#8221; because within sql you need to use the &#8220;;&#8221; which is the standard delimiter (when the MySQL will try and execute the query)</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">delimiter <span style="color: #66cc66;">//</span>
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TRIGGER</span> testTable_insert 
<span style="color: #993333; font-weight: bold;">BEFORE</span> <span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #ff0000;">'testTable'</span> 
<span style="color: #993333; font-weight: bold;">FOR</span> EACH <span style="color: #993333; font-weight: bold;">ROW</span> 
<span style="color: #993333; font-weight: bold;">BEGIN</span> 
    <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #993333; font-weight: bold;">NEW</span><span style="color: #66cc66;">.</span>guid <span style="color: #66cc66;">=</span> uuid<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; 
<span style="color: #993333; font-weight: bold;">END</span>;
delimiter ;</pre></div></div>

<p>&#8220;CREATE TRIGGER&#8221; is just like creating a table, apart from you are creating a trigger, so the next value is the trigger name, normally it is the table name with what you are doing, e.g. inserting</p>
<p>&#8220;BEFORE INSERT ON&#8221; means before you actually insert the data into the database (it is in a hold area as such). the next value is the actual table that you are linking this trigger to.</p>
<p>&#8220;FOR EACH ROW&#8221; means each row of the insert, since you can insert x amount of lines into a table.</p>
<p>the sql code is between the &#8220;BEGIN&#8221; and &#8220;END&#8221;, and all is what is being set is the guid column within the &#8220;NEW&#8221; (this is the new row to be inserted) and setting that value to <a href="http://dev.mysql.com/doc/refman/5.1/en/miscellaneous-functions.html#function_uuid">uuid</a>() which is a mysql function.</p>
<p>so before you would have inserted into a table and the table would have looked like</p>
<table>
<tr>
<td width="100">
<td width="100">
<td width="100">
</tr>
<tr>
<td>ID</td>
<td>GUID</td>
<td>Name</td>
</tr>
<tr>
<td>1</td>
<td></td>
<td>thename</td>
</tr>
</table>
<p>but once the trigger is in place and you did the same process again of inserting a new name</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> testTable <span style="color: #66cc66;">&#40;</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;a new name&quot;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<table>
<tr>
<td width="100">
<td width="100">
<td width="100">
</tr>
<tr>
<td>ID</td>
<td>GUID</td>
<td>Name</td>
</tr>
<tr>
<td>1</td>
<td></td>
<td>thename</td>
</tr>
<tr>
<td>1</td>
<td>3ace82c-2cf1-11df-b1c3-00a0d1a1240a</td>
<td>a new name</td>
</tr>
</table>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/03/11/mysql-triggers/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Blob to store data in mysql database</title>
		<link>http://www.codingfriends.com/index.php/2010/02/09/blob-to-store-data-in-mysql-database/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=blob-to-store-data-in-mysql-database</link>
		<comments>http://www.codingfriends.com/index.php/2010/02/09/blob-to-store-data-in-mysql-database/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 20:22:23 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[SQL (Structured Query Language)]]></category>
		<category><![CDATA[blob]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=676</guid>
		<description><![CDATA[To store data within a blob in a database can be a good thing at times because then you can just copy the database from one place to another and use the access rights on the database to restrict access to the &#8220;files&#8221; within the database. There could be a few reasons why you want [...]]]></description>
			<content:encoded><![CDATA[<p>To store data within a blob in a database can be a good thing at times because then you can just copy the database from one place to another and use the access rights on the database to restrict access to the &#8220;files&#8221; within the database.</p>
<p>There could be a few reasons why you want to store the data within a blob in the database, but here how the basics would work.</p>
<p>To start with you have to create a database and a table to store the data/file within the blob, of course if you have already created the database and/or the tables then alter as you think, but here is the basics.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">  <span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">DATABASE</span> phptestplace;
  <span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> storingData <span style="color: #66cc66;">&#40;</span>id <span style="color: #993333; font-weight: bold;">INT</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> auto_incremenet<span style="color: #66cc66;">,</span> lblob <span style="color: #993333; font-weight: bold;">BLOB</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>And then within a php file you can access the database and a file.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000088;">$link</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'username'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'userpassword'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$link</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Could not connect: '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">// alter to your database name</span>
  <span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;phptestplace&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$link</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>and now access the file and read in file</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;filetoload.txt&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$handle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;r&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$contents</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fread</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #339933;">,</span> <span style="color: #990000;">filesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// to insert into the database you need to add in the slashes for characters like / \ etc.</span>
  <span style="color: #000088;">$contents</span> <span style="color: #339933;">=</span> <span style="color: #990000;">addslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$contents</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>to insert into the blob you just, change the table and table name to what may have called it.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000088;">$sqlquery</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;insert into storingData(largeblob) values ('<span style="color: #006699; font-weight: bold;">$contents</span>')&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sqlquery</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ERROR&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;*/</span></pre></div></div>

<p>to get the data back  (I am calling back the last inserted value into the table)</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// get the data into a result variable</span>
  <span style="color: #000088;">$return</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;select lblob from storingData where id = (select max(id) from storingData)&quot;</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;LLL&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// get the contents of the blob from the return variable (it returns a array of data) and the list takes out the data from a array each part at time.</span>
  <span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$newcontents</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$return</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>and then store the data from the database pull into a file, I have called it newfile.txt, but it is up to you.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'NEWFILE.txt'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #000088;">$newcontents</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Of course can do it via a web page, using a HTML FORM enctype=&#8221;multipart/form-data&#8221; within the form tag otherwise it may not work.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/02/09/blob-to-store-data-in-mysql-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

