You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl-cvs@perl.apache.org by ri...@hyperreal.org on 1999/12/20 06:05:04 UTC

cvs commit: modperl-site/embperl Intrors.pod.1.html Intrors.pod.2.html Intrors.pod.3.html Intrors.pod.4.html Intrors.pod.cont.html Changes.pod.1.html index.html

richter     99/12/19 21:05:04

  Modified:    embperl  Changes.pod.1.html index.html
  Added:       embperl  Intrors.pod.1.html Intrors.pod.2.html
                        Intrors.pod.3.html Intrors.pod.4.html
                        Intrors.pod.cont.html
  Log:
  Embperl Webpages - Changes
  
  Revision  Changes    Path
  1.114     +1 -1      modperl-site/embperl/Changes.pod.1.html
  
  Index: Changes.pod.1.html
  ===================================================================
  RCS file: /export/home/cvs/modperl-site/embperl/Changes.pod.1.html,v
  retrieving revision 1.113
  retrieving revision 1.114
  diff -u -r1.113 -r1.114
  --- Changes.pod.1.html	1999/12/12 19:33:08	1.113
  +++ Changes.pod.1.html	1999/12/20 05:05:02	1.114
  @@ -18,7 +18,7 @@
     <blockquote>
   [<a href="index.html">HOME</a>]&nbsp;&nbsp; [<a href="Changes.pod.cont.html">CONTENT</a>]&nbsp;&nbsp; [<a href="Changes.pod.cont.html">PREV (Revision History - Content)</a>]&nbsp;&nbsp; [<a href="Changes.pod.2.html">NEXT (1.2.0 17 Nov 1999)</a>]&nbsp;&nbsp; <br><hr>
   <P>
  -Last Update: Sun Dec 12 20:32:26 1999 (MET)
  +Last Update: Mon Dec 20 06:04:14 1999 (MET)
   
   <P>
   NOTE: This version is only available via <A HREF="CVS.pod.1.html#INTRO">"CVS"</A>
  
  
  
  1.61      +6 -1      modperl-site/embperl/index.html
  
  Index: index.html
  ===================================================================
  RCS file: /export/home/cvs/modperl-site/embperl/index.html,v
  retrieving revision 1.60
  retrieving revision 1.61
  diff -u -r1.60 -r1.61
  --- index.html	1999/12/12 19:33:09	1.60
  +++ index.html	1999/12/20 05:05:03	1.61
  @@ -53,7 +53,7 @@
             <td bgcolor="#80ff00"><big>&nbsp;<a href="Sites.pod.1.html">Sites using Embperl</a>&nbsp;</big></td>
           </tr>
           <tr>
  -          <td bgcolor="#60ff00"><big>&nbsp;<a href="Intro.pod.8.html#DBIx_Recordset">DBIx::Recordset</a>&nbsp;</big></td>
  +          <td bgcolor="#60ff00"><big>&nbsp;<a href="Intrors.pod.cont.html">DBIx::Recordset</a>&nbsp;</big></td>
           </tr>
           <tr>
             <td bgcolor="#40ff00"><big>&nbsp;<a href="http://www.apache.org/search.html">Search</a>&nbsp;</big></td>
  @@ -131,6 +131,11 @@
             </tr>
             <tr>
               <td vAlign="top"><img height="13" src="bullet.gif" width="13"></td>
  +            <td vAlign="top">20. Dec 99</td>
  +            <td><font face="Helvetica,Arial" size="2">Added Introduction to <a href="Intrors.pod.cont.html">DBIx::Recordset</a></font></td>
  +          </tr>
  +          <tr>
  +            <td vAlign="top"><img height="13" src="bullet.gif" width="13"></td>
               <td vAlign="top">4. Dec 99</td>
               <td><font face="Helvetica,Arial" size="2">Added Page with <a href="mirror.html">WorldWide Mirrors</a></font></td>
             </tr>
  @@ -225,7 +230,7 @@
   <blockquote>
     <p><font color="#808080">___________________________________________________________________________________<br>
     HTML::Embperl - Copyright (c) 1997-99 Gerald Richter / ECOS &lt;richter@dev.ecos.de&gt;<br>
  -  Last Update $Id: index.html,v 1.60 1999/12/12 19:33:09 richter Exp $</font></p>	
  +  Last Update $Id: index.html,v 1.61 1999/12/20 05:05:03 richter Exp $</font></p>	
   </blockquote>
   </td></tr><!--msnavigation--></table></body>
   </html>
  
  
  
  1.1                  modperl-site/embperl/Intrors.pod.1.html
  
  Index: Intrors.pod.1.html
  ===================================================================
  <HTML>
  <HEAD>
  <TITLE>Embperl and DBIx::Recordset</TITLE>
  <LINK REV="made" HREF="mailto:root@venus.gr.ecos.de">
  </HEAD>
  
  <BODY bgcolor="#FFFFFF">
  <blockquote>
    <blockquote>
      <H1><strong>
  <A NAME="Embperl_and_DBIx_Recordset">Embperl and DBIx::Recordset</a></strong></h1>
    </blockquote>
  </blockquote>
  
  <img src="line.jpg" align="left" alt="________" WIDTH="732" HEIGHT="35">
  <BR CLEAR=LEFT>
  <blockquote>
    <blockquote>
  [<a href="index.html">HOME</a>]&nbsp;&nbsp; [<a href="Intrors.pod.cont.html">CONTENT</a>]&nbsp;&nbsp; [<a href="Intrors.pod.cont.html">PREV (Recordset Introduction - Content)</a>]&nbsp;&nbsp; [<a href="Intrors.pod.2.html">NEXT (Basic Example )</a>]&nbsp;&nbsp; <br>	<UL>
  
  		<LI><A href="Intrors.pod.1.html#Overview">Overview</A>
  		<LI><A href="Intrors.pod.1.html#Embperl">Embperl</A>
  	</UL>
  <hr>
  <P>
  This introduction gives an overview how to use <EM>DBIx::Recordset</EM> together with <EM>HTML::Embperl</EM>. Since there are only a few <EM>Embperl</EM> specific things herein, it should be also usefull for non <EM>Embperl</EM> users.
  
  <P>
  <HR>
  <H2><img src="sq.gif" width="16" height="16" alt="-"> <A NAME="Overview">Overview</A></H2>
  <P>
  It is often very difficult to layout and design the output of normal CGI
  scripts, because you are dealing with HTML-sourcecode which spans multiple
  prints, and it isn't possible to use some sort of HTML-editor. Embperl
  takes a different approach to this problem. With Embperl, you can build
  your HTML-pages with any tool you like, and you can embed fragments of code
  directly in the page. This makes it much easier for non-programmers to use,
  because they are able to use their usual tools and they see the fragments
  of code as normal text. This indroduction will deal with the Perl Modules <EM>HTML::Embperl</EM> and
  <EM>DBIx::Recordset</EM>, with a focus on database access.
  
  <P>
  <HR>
  <H2><img src="sq.gif" width="16" height="16" alt="-"> <A NAME="Embperl">Embperl</A></H2>
  <P>
  In brief, the purpose of Embperl is to execute code that is embedded in
  HTML-pages as the page is requested from the server. There are two ways to
  do this with Embperl. The first way is to embed the code between [- and -]
  tags. This will cause Embperl to execute the code and remove it from the
  source before sending the page. The second way is to use [+ and +] as the
  delimiter, in which case the code will be executed and the result of the
  execution is send to the browser in place of the code. All database access
  is done via the module <EM>DBIx::Recordset</EM>, which simplifies a lot of common tasks when accessing a database via DBI.
  
  <p>[<a href="index.html">HOME</a>]&nbsp;&nbsp; [<a href="Intrors.pod.cont.html">CONTENT</a>]&nbsp;&nbsp; [<a href="Intrors.pod.cont.html">PREV (Recordset Introduction - Content)</a>]&nbsp;&nbsp; [<a href="Intrors.pod.2.html">NEXT (Basic Example )</a>]&nbsp;&nbsp; <br>
      <font color="#808080">___________________________________________________________________________________<br>
      HTML::Embperl - Copyright (c) 1997-1999 Gerald Richter / ECOS
      </font></p>
    </blockquote>
  </blockquote>
  </td></tr></table></body>
  </html>
  </BODY>
  
  </HTML>
  
  
  
  1.1                  modperl-site/embperl/Intrors.pod.2.html
  
  Index: Intrors.pod.2.html
  ===================================================================
  <HTML>
  <HEAD>
  <TITLE>Basic Example </TITLE>
  <LINK REV="made" HREF="mailto:root@venus.gr.ecos.de">
  </HEAD>
  
  <BODY bgcolor="#FFFFFF">
  <blockquote>
    <blockquote>
      <H1><strong>
  <A NAME="Basic_Example">Basic Example </a></strong></h1>
    </blockquote>
  </blockquote>
  
  <img src="line.jpg" align="left" alt="________" WIDTH="732" HEIGHT="35">
  <BR CLEAR=LEFT>
  <blockquote>
    <blockquote>
  [<a href="index.html">HOME</a>]&nbsp;&nbsp; [<a href="Intrors.pod.cont.html">CONTENT</a>]&nbsp;&nbsp; [<a href="Intrors.pod.1.html">PREV (Embperl and DBIx::Recordset)</a>]&nbsp;&nbsp; [<a href="Intrors.pod.3.html">NEXT (Multiple tables)</a>]&nbsp;&nbsp; <br>	<UL>
  
  		<LI><A href="Intrors.pod.2.html#Search">Search</A>
  		<LI><A href="Intrors.pod.2.html#Display_the_table">Display the table</A>
  		<LI><A href="Intrors.pod.2.html#Supplying_query_parameters">Supplying query parameters</A>
  	</UL>
  <hr>
  <P>
  The following example shows the basic functions of both modules. It shows
  the contents of a table whose name is passed as a parameter:
  
  <P>
  &lt;body&gt; &lt;h1&gt;Contents of table ``[+ $fdat{'!Table'}
  +]''&lt;/h1&gt;
  
  <P>
  <PRE>  [-
    use DBIx::Recordset ;
   
    $fdat{'!DataSource'} = 'dbi:mysql:test' ;
    *set = DBIx::Recordset -&gt; Search(\%fdat) ;
    $names = $set -&gt; Names ;
    -]
  </PRE>
  <P>
  <PRE>  &lt;table&gt;
      &lt;tr&gt;
        &lt;th&gt;[+ $names -&gt; [$col] +]&lt;/th&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        [- $rec = $set[$row] -]
        &lt;td&gt;[+ $rec -&gt; {$names-&gt;[$col]} +]&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/table&gt;
  &lt;/body&gt;
  </PRE>
  <P>
  To show the contents of the table <CODE>address</CODE> you may call it with:
  
  <P>
  <PRE>  <A HREF="http://www.domain.com/path/to/example1.htm?!Table=address">http://www.domain.com/path/to/example1.htm?!Table=address</A>
  </PRE>
  <P>
  All query parameters are placed in the hash <CODE>%fdat</CODE> by Embperl. In our example, <CODE>$fdat{'!Table'}</CODE> would contain the value <CODE>address</CODE>. Additionally, Embperl replaces the code between <CODE>[+</CODE> and <CODE>+]</CODE> with the result, so the headline of the page would be 'Contents of table
  ``address'''.
  
  <P>
  The following [- -] block will be executed by Embperl. No trace of it will
  show up in the page that is sent to the browser. The first line sets the
  database which should be accessed. The syntax is the same as for the DBI
  connect call. If you omit the line, you must additionally send the
  databasename as a query parameter - but for security reasons, that isn't a
  very good idea. 
  
  <P>
  <HR>
  <H2><img src="sq.gif" width="16" height="16" alt="-"> <A NAME="Search">Search</A></H2>
  <P>
  Next we call the method <CODE>Search</CODE> of <EM>DBIx::Recordset</EM>, where we have the choice between the object and the class-method. This
  applies to a lot of other methods as well. When we call it as a class
  method, as we do in our example, it constructs a new <EM>DBIx::Recordset</EM>
  object and uses the passed parameters to query the database. It's also
  possible to divide these two steps and call <CODE>Setup</CODE> to first construct the object and then 
  <CODE>Search</CODE> with this object to execute the Search. In the example above, we do not
  pass any query parameters -- so  <CODE>Search</CODE> will return the contents of the whole table. (<EM>DBIx::Recordset</EM> converts the call internally to the SQL statement <CODE>SELECT * FROM address</CODE>).
  
  <P>
  The last line of the [- -] block retrieves the fieldnames of the table.
  Here we can see a special feature of <EM>DBIx::Recordset</EM>, which we will discuss in detail later on. The constructor returns a <STRONG>typeglob</STRONG> (<CODE>*set</CODE>), but the call to <CODE>Names</CODE> uses a <STRONG>scalar</STRONG> (<CODE>$set</CODE>). By returning a typeglob, <EM>DBIx::Recordset</EM> is able to return a <STRONG>scalar</STRONG>, an <STRONG>array</STRONG> and a <STRONG>hash</STRONG> at the same time. (If you don't like the idea of using typeglobs, you can
  also construct all three with different methods).
  
  <P>
  <HR>
  <H2><img src="sq.gif" width="16" height="16" alt="-"> <A NAME="Display_the_table">Display the table</A></H2>
  <P>
  At first glance, the following might appear to be a simple HTML-table. But <EM>Embperl</EM>
  expands it, so that the full contents of the database table is shown. Let
  us first look at the header, which should show the fieldnames of the
  database-table: <CODE>$names</CODE>
  contains a reference to an array which contains the fieldnames. <EM>Embperl</EM>
  gives us the magical variable <CODE>$col</CODE>. <CODE>$col</CODE> will be automatically incremented as long as the result of the expression
  which contains <CODE>$col</CODE>
  doesn't return <STRONG>undefined</STRONG>. At the same time, <EM>Embperl</EM> repeats the surrounding <CODE>&lt;th</CODE>&gt; or <CODE>&lt;td</CODE>&gt; tags. If we have a table with the three columns
  <CODE>name</CODE>, <CODE>firstname</CODE> and <CODE>town</CODE>, the output would look like this:
  
  <P>
  <PRE>  &lt;th&gt;name&lt;/th&gt;&lt;th&gt;firstname&lt;/th&gt;&lt;th&gt;town&lt;/th&gt;
  </PRE>
  <P>
  Now the header is ready and we can start to output the contents. Here we
  use the array part of the typeglob that is returned by <CODE>Search</CODE>. Access to the results of the SQL-query is done via the array <CODE>@set</CODE>, and every row of the array ``contains'' one row of the database-table. It
  does not really contain the row, but <EM>DBIx::Recordset</EM> will fetch the row from the databases for you if you access the
  corresponding array row. The rows are stored as a hash, where the
  fieldnames are the hashkeys. This is the same mechanism that helped us to
  expand the columns of the header, but it's at work here in a
  two-dimensional manner. <CODE>$row</CODE> contains the row-count and <CODE>$col</CODE> contains the column-count.
  
  <P>
  <HR>
  <H2><img src="sq.gif" width="16" height="16" alt="-"> <A NAME="Supplying_query_parameters">Supplying query parameters</A></H2>
  <P>
  But our small example can do even more: If we supply more query parameters
  in our request, we can decide which parts of the table should be selected
  (and therefor, shown). If we request the page with
  
  <P>
  <PRE>  <A HREF="http://wwww.domain.com/path/to/example1.htm?!Table=address&amp">http://wwww.domain.com/path/to/example1.htm?!Table=address&amp</A>;town=Berlin
  </PRE>
  <P>
  <EM>Embperl</EM> will not only place <CODE>!</CODE><CODE>T</CODE><CODE>able</CODE> in the hash <CODE>%fdat</CODE>, but also <CODE>town</CODE>. Since <CODE>town</CODE> corresponds to a fieldname in our table, <EM>DBIx::Recordset</EM> interprets it as a parameter for the <CODE>WHERE</CODE> part of the <CODE>SELECT</CODE> command. <EM>DBIx::Recordset</EM>
  will generate the following SQL-query:
  
  <P>
  <PRE>  SELECT * FROM address WHERE town='Berlin' ;
  </PRE>
  <P>
  The programmer doesn't have to pay attention to datatypes or quoting, this
  is done automatically by <EM>DBIx::Recordset</EM>.
  
  <P>
  Also, complex queries are easy to implement: if, for example, the user
  wants to be able to search for a name or for a town, it would be possible
  to use the following form:
  
  <P>
  <PRE>  &lt;form action=&quot;/path/to/example1.htm&quot; method=GET &gt;
      &lt;input type=text name=&quot;+name|town&quot;&gt;
      &lt;input type=hidden name=&quot;!Table&quot; value=&quot;address&quot;&gt;
      &lt;input type=submit&gt;
    &lt;/form&gt;
  </PRE>
  <P>
  If the user enters ``Richter'' to the input field and presses the submit
  button, the following SQL-query will be generated:
  
  <P>
  <PRE>  SELECT * FROM address WHERE name='Richter' OR town='Richter' ;
  </PRE>
  <P>
  Just by varying the parameters, it is possible to create simple or complex
  queries. In this way, you can use the same page with different parameters
  to create different sorts of queries.
  
  <p>[<a href="index.html">HOME</a>]&nbsp;&nbsp; [<a href="Intrors.pod.cont.html">CONTENT</a>]&nbsp;&nbsp; [<a href="Intrors.pod.1.html">PREV (Embperl and DBIx::Recordset)</a>]&nbsp;&nbsp; [<a href="Intrors.pod.3.html">NEXT (Multiple tables)</a>]&nbsp;&nbsp; <br>
      <font color="#808080">___________________________________________________________________________________<br>
      HTML::Embperl - Copyright (c) 1997-1999 Gerald Richter / ECOS
      </font></p>
    </blockquote>
  </blockquote>
  </td></tr></table></body>
  </html>
  </BODY>
  
  </HTML>
  
  
  
  1.1                  modperl-site/embperl/Intrors.pod.3.html
  
  Index: Intrors.pod.3.html
  ===================================================================
  <HTML>
  <HEAD>
  <TITLE>Multiple tables</TITLE>
  <LINK REV="made" HREF="mailto:root@venus.gr.ecos.de">
  </HEAD>
  
  <BODY bgcolor="#FFFFFF">
  <blockquote>
    <blockquote>
      <H1><strong>
  <A NAME="Multiple_tables">Multiple tables</a></strong></h1>
    </blockquote>
  </blockquote>
  
  <img src="line.jpg" align="left" alt="________" WIDTH="732" HEIGHT="35">
  <BR CLEAR=LEFT>
  <blockquote>
    <blockquote>
  [<a href="index.html">HOME</a>]&nbsp;&nbsp; [<a href="Intrors.pod.cont.html">CONTENT</a>]&nbsp;&nbsp; [<a href="Intrors.pod.2.html">PREV (Basic Example )</a>]&nbsp;&nbsp; [<a href="Intrors.pod.4.html">NEXT (Modify the Database)</a>]&nbsp;&nbsp; <br>	<UL>
  
  		<LI><A href="Intrors.pod.3.html#DBIx_Database">DBIx::Database</A>
  		<LI><A href="Intrors.pod.3.html#Sub_Objects">Sub-Objects</A>
  	</UL>
  <hr>
  <P>
  Until now, we only have worked with one table. In real life, you often have
  to deal with mulitple tables. For this reason, <EM>DBIx::Recordset</EM> helps you to reduce the expense associated with dealing with multiple
  tables. The simplest way to do this is to use the parameters <CODE>!TabJoin</CODE> and <CODE>!TabRelation</CODE> to tell <EM>DBIx::Recordset</EM> to create an SQL-join between two or more tables. This will link the tables
  together and the result looks just like one great table.
  
  <P>
  More interesting is the possibility to create <STRONG>"links"</STRONG>. As an example, we'll take the same table we used above and divide it into
  two tables: one table for the names and one table for the towns. As a link
  we add an id-field. If the fields are following some naming convention, <EM>DBIx::Recordset</EM> is able to find this link automatically. If fields are named in another
  way, you have to tell <EM>DBIx::Recordset</EM> manually how the tables belong together.
  
  <P>
  <PRE>  Table name:       firstname, name, town_id
    Table town:       id, town
  </PRE>
  <P>
  Here, every name has exactly one town and every town has a number of names
  assigned. With a simple modification of our first example, we could get the
  same result as above (except that we are now dealing with two tables
  instead of one):
  
  <P>
  <PRE>                                                                      
    [-
    use DBIx::Recordset ;
    $db = DBIx::Database -&gt; new ('dbi:mysql:test') ;
    $db -&gt; TableAttr ('ort', '!NameField', 'town') ;
  </PRE>
  <P>
  <PRE>  $fdat{'!DataSource'} = $db ;
    $fdat{'!LinkName'} = 3 ;
    *set = DBIx::Recordset -&gt; Search(\%fdat) ;
    $names = $set -&gt; Names ;
    -]
  </PRE>
  <P>
  And the request would be:
  
  <P>
  <PRE>  <A HREF="http://www.domain.com/path/to/example2.htm?!Table=name">http://www.domain.com/path/to/example2.htm?!Table=name</A>
  </PRE>
  <P>
  <HR>
  <H2><img src="sq.gif" width="16" height="16" alt="-"> <A NAME="DBIx_Database">DBIx::Database</A></H2>
  <P>
  The new thing here is the <CODE>DBIx::Database</CODE> object. It gathers meta-information about the database and stores it for
  later use. Because of the names of the fields the object can detect that
  the field <CODE>town_id</CODE> in the table <CODE>name</CODE> points to field <CODE>id</CODE> in the table <CODE>town</CODE>. Additionally, we tell the <CODE>DBIx::Database</CODE> object which <CODE>column(s)</CODE> contain the human-readable name of the
  table <CODE>town</CODE>. These initialisations only have to be executed once. If you use 
  <EM>mod_perl</EM>, for example, you should be able to move these lines into a common startup
  file.
  
  <P>
  Also new is the parameter <CODE>!LinkName</CODE>. It tells <EM>DBIx::Recordset</EM> to return the human-readable name (in our example, <CODE>town</CODE>) instead of the field which links the two tables together (<CODE>town_id</CODE> in our example). Internally, <EM>DBIx::Recordset</EM>
  generates an SQL-join, so there is only one <CODE>SELECT</CODE> command necessary and the result is just the same as in the last example.
  
  <P>
  <HR>
  <H2><img src="sq.gif" width="16" height="16" alt="-"> <A NAME="Sub_Objects">Sub-Objects</A></H2>
  <P>
  But what to do if we have the <CODE>id</CODE> of a town and want to display all the names that belongs to it? An <EM>Embperl</EM>-page that does this job might look something like this: 
  
  <P>
  &lt;body&gt; [- use DBIx::Recordset ; $fdat{'!DataSource'} =
  'dbi:mysql:test' ; $fdat{'!Table'} = 'town' ; <CODE>*set</CODE> =
  DBIx::Recordset -&gt; <CODE>Search(\%fdat)</CODE> ; -]
  
  <P>
  <PRE>  town: [+ $set{town} +]&lt;br&gt;
    &lt;table&gt;
      &lt;tr&gt;
        &lt;th&gt;name&lt;/th&gt;&lt;th&gt;firstname&lt;/th&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        [- $rec = $set{-name}[$row] -]
        &lt;td&gt;[+ $rec -&gt; {name} +]&lt;/td&gt;&lt;td&gt;[+ $rec -&gt; {firstname} +]&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/table&gt;
  &lt;/body&gt;
  </PRE>
  <P>
  A request to that page might look like this: 
  
  <P>
  <PRE>  <A HREF="http://www.domain.com/path/to/example3.htm?id=5">http://www.domain.com/path/to/example3.htm?id=5</A>
  </PRE>
  <P>
  In this example, we specify the name of the table directly inside the page,
  so it can't be overwritten from outside. The call to <CODE>Search</CODE> returns the town for the given query parameters. In our example, it will
  select the town with the <CODE>id</CODE> 5. The command <CODE>[+ $set{town} +]</CODE> shows the value of the field town in the current record. After the call to <CODE>Search</CODE>, this is the first selected record. Next, we need to display all the
  names. This is very easy using the special field
  <CODE>-name</CODE>. <CODE>-name</CODE> contains a sub-object for the table <CODE>name</CODE>. The query parameters for this sub-object are set by <EM>DBIx::Recordset</EM> in such a way that it contains all names which meet the link-condition. We
  just wrap it in a table and we are already done.
  
  <p>[<a href="index.html">HOME</a>]&nbsp;&nbsp; [<a href="Intrors.pod.cont.html">CONTENT</a>]&nbsp;&nbsp; [<a href="Intrors.pod.2.html">PREV (Basic Example )</a>]&nbsp;&nbsp; [<a href="Intrors.pod.4.html">NEXT (Modify the Database)</a>]&nbsp;&nbsp; <br>
      <font color="#808080">___________________________________________________________________________________<br>
      HTML::Embperl - Copyright (c) 1997-1999 Gerald Richter / ECOS
      </font></p>
    </blockquote>
  </blockquote>
  </td></tr></table></body>
  </html>
  </BODY>
  
  </HTML>
  
  
  
  1.1                  modperl-site/embperl/Intrors.pod.4.html
  
  Index: Intrors.pod.4.html
  ===================================================================
  <HTML>
  <HEAD>
  <TITLE>Modify the Database</TITLE>
  <LINK REV="made" HREF="mailto:root@venus.gr.ecos.de">
  </HEAD>
  
  <BODY bgcolor="#FFFFFF">
  <blockquote>
    <blockquote>
      <H1><strong>
  <A NAME="Modify_the_Database">Modify the Database</a></strong></h1>
    </blockquote>
  </blockquote>
  
  <img src="line.jpg" align="left" alt="________" WIDTH="732" HEIGHT="35">
  <BR CLEAR=LEFT>
  <blockquote>
    <blockquote>
  [<a href="index.html">HOME</a>]&nbsp;&nbsp; [<a href="Intrors.pod.cont.html">CONTENT</a>]&nbsp;&nbsp; [<a href="Intrors.pod.3.html">PREV (Multiple tables)</a>]&nbsp;&nbsp; <br><hr>
  <P>
  Up to this point, we have only discussed the retrieval and display of data.
  But of course it's also possible to modify data. The simplest way to do
  this is to assign new values to the result of a <CODE>Search</CODE> call. For example, you may write <CODE>$set{town} = 'Frankfurt'</CODE> to change the name of the town.
  <EM>DBIx::Recordset</EM> converts this into a vaild SQL-Update-command.
  
  <P>
  While this is very useful in normal Perl scripts, you probably won't use it
  very often in a cgi script. The methods <CODE>Insert</CODE>/<CODE>Update</CODE>/<CODE>Delete</CODE> will probably prove more useful. Just like <CODE>Search</CODE>, these directly accept query parameters posted to the page. The method <CODE>Execute</CODE> combines all four of these together, making it possible to control the type
  of action via the CGI-parameters.
  
  <P>
  Here is an example:
  
  <P>
  &lt;html&gt; &lt;head&gt; &lt;title&gt;Database Access with HTML::Embperl
  and DBIx::Recordset&lt;/title&gt; &lt;/head&gt; &lt;body&gt;
  
  <P>
  <PRE>  [-
    ### Database-parameter ###
    use DBIx::Recordset ;
    $fdat{'!DataSource'} = 'dbi:mysql:test' ;
    $fdat{'!Table'} ||= 'town' ;
    $fdat{'!PrimKey'} = 'id' ;
    $fdat{'$max'}     = 10 ;
  </PRE>
  <P>
  <PRE>  ### Execute action according to the query parameters ###
    *set = DBIx::Recordset -&gt; Execute (\%fdat) ;
    -]
  </PRE>
  <P>
  <PRE>  [$if $DBI::errstr $]
          &lt;h1&gt;Database Error [+ $DBI::errstr +]&lt;/h1&gt;
    [$else$]
      [-$names = $set -&gt; AllNames ; -]
      [$if $set[0] &amp;&amp; $set -&gt; MoreRecords $]
        [### We found more then one record ###]
        [### -&gt; display as a table         ###]
        &lt;table&gt;
          &lt;tr&gt;  [### Display header ###]
            &lt;th&gt;[+ ucfirst ($names -&gt; [$col]) +]&lt;/th&gt;
          &lt;/tr&gt;
          &lt;tr&gt;  [### Display record -&gt; Table will be expanded by Embperl ###]
            [- $rec = $set[$row] -]
            &lt;td&gt;
              [- $name = $names -&gt; [$col] -]
              [$if $name eq $fdat{'!PrimKey'} $]
                [### Generate HTML link to edit this record ###]
                &lt;a href=&quot;example4.htm?!Table=[+ $fdat{'!Table'} +]&amp;[+ $fdat{'!PrimKey'} +]=[+ $rec -&gt;{$fdat{'!PrimKey'}} +]&quot;&gt;[+ $rec -&gt; {$name} +]&lt;/a&gt;
              [$elsif $set -&gt; Link4Field($name) $]
                [### Link to other table -&gt; generate HTML link ###]
                [- $link = $set -&gt; Link($set -&gt; Link4Field($name)) -]
                &lt;a href=&quot;example4.htm?!Table=[+ $link -&gt; {'!Table'} +]&amp;[+ $link -&gt; {'!LinkedField'} +]=[+ $rec -&gt; {$link -&gt; {'!MainField'}} +]&quot;&gt;[+$rec -&gt; {$name}+]&lt;/a&gt;
              [$else$] 
                [### Display contents of field ###]
                [+ $rec -&gt; {$names-&gt;[$col]} +]
              [$endif$]
              &lt;/td&gt;
          &lt;/tr&gt;
        &lt;/table&gt;
        [+ $set -&gt; PrevNextForm ('\&lt;\&lt;Prev', 'Next\&gt;\&gt;', \%fdat) ; +]
        &lt;hr&gt;
        &lt;a href=&quot;example4.htm?!Table=[+ $fdat{'!Table'} +]&amp;%3dempty=1&quot;&gt;Search&lt;/a&gt; record in table '[+ $fdat{'!Table'} +]' 
      [$else$]
        [### We found no/one record(s) ###]
        [### -&gt; Display form          ###]
        &lt;form&gt;
          &lt;table&gt;
          &lt;tr&gt;
            [- $name = $names -&gt; [$row] -]
            &lt;td&gt; [### Display fieldname ###]
              [+ ucfirst ($name) +]
            &lt;/td&gt;
            &lt;td&gt; [### Display content of field ###]
              &lt;input type=text name=&quot;[+ $name +]&quot; value=&quot;[+ $set{$name} +]&quot;&gt;
              [$if $set -&gt; Link4Field($name) $]
                [### Link to other table -&gt; generate HTML link ###]
                [- $link = $set -&gt; Link($set -&gt; Link4Field($name)) -]
                &lt;a href=&quot;example4.htm?!Table=[+ $link -&gt; {'!Table'} +]&amp;[+ $link -&gt; {'!LinkedField'} +]=[+ $set{$link -&gt; {'!MainField'}} +]&quot;&gt;Show record from table '[+ $link -&gt; {'!Table'} +]'&lt;/a&gt;
              [$endif$]
            &lt;/td&gt;
          &lt;/tr&gt;
          &lt;/table&gt;
          [### Buttons for the different actions, the &quot;name&quot; attribute determinates ###]
          [###  which action should be taken                                        ###]
          &lt;input type=submit name=&quot;=search&quot; value=&quot;Search&quot;&gt;
          &lt;input type=submit name=&quot;=empty&quot;  value=&quot;New&quot;&gt;
          &lt;input type=submit name=&quot;=insert&quot; value=&quot;Add&quot;&gt;
          &lt;input type=submit name=&quot;=update&quot; value=&quot;Update&quot;&gt;
          &lt;input type=submit name=&quot;=delete&quot; value=&quot;Delete&quot;&gt;
          &lt;input type=hidden name=&quot;!Table&quot;  value=&quot;[+ $fdat{'!Table'} +]&quot;&gt;
        &lt;/form&gt;
      [$endif$]
    [$endif$]
  &lt;/body&gt;
  &lt;/html&gt;
  </PRE>
  <P>
  When you first request this page, it will show the contents of the preset
  table. Alternatively, you can supply a tablename with the parameter <CODE>!T</CODE><CODE>able</CODE>. The link, which is shown at the bottom of the page, leads you to an imput
  form. There, you can fill in one or more fields and press the <STRONG>Search</STRONG> button. This invokes the page itself and <CODE>Execute</CODE> will be instructed by the parameter <CODE>=s</CODE><CODE>earch</CODE> 
  (Name of the button ``Search'') to retrieve all records which match the
  entered values. 
  
  <P>
  If the query finds more then one record, a table with all records found
  will be shown. If there are more records than specified by the parameter <CODE>$max</CODE>, only
  <CODE>$max</CODE> records are displayed. If this is the case, the <CODE>PrevNextForm</CODE> method adds a ``Previous'' and a ``Next'' button to the page, allowing you
  to browse through the whole table. In the example above, we assume that
  every table has a primary key, which is passed to <EM>DBIx::Recordset</EM> by the line <CODE>$fdat{'!PrimKey'} = 'id' ;</CODE>. The column which contains this primary key will be displayed as an HTML
  link containing the parameters to execute a search for just this record. As
  you can see in example4.htm, this can be used to display a form which
  includes some of the data from the found record (see below). Columns which
  are links to other tables will also be shown with an HTML-link. A click on
  that link will open the linked table or record.
  
  <P>
  If the search only selects one record, the same form is shown, but with the
  data from the record filled in. Now it's possible to change the content.
  The changes are written to the database when you press the button <STRONG>Update</STRONG> (parameter <CODE>=update</CODE>). A new, empty form could be shown with the button <STRONG>New</STRONG> (parameter <CODE>=empty</CODE>) and if you have written data into this empty form, you can add it as a
  new record with the
  <STRONG>Add</STRONG> button (parameter <CODE>=insert</CODE>). Last but not least, there is a <STRONG>Delete</STRONG> button (parameter <CODE>=delete</CODE>). In all of these cases, the content of the form is sent to the page
  itself, and the <CODE>Execute</CODE> method at the start of the page executes the desired action.
  
  <P>
  More comments can be found inside the source ([# #] blocks).
  
  <p>[<a href="index.html">HOME</a>]&nbsp;&nbsp; [<a href="Intrors.pod.cont.html">CONTENT</a>]&nbsp;&nbsp; [<a href="Intrors.pod.3.html">PREV (Multiple tables)</a>]&nbsp;&nbsp; <br>
      <font color="#808080">___________________________________________________________________________________<br>
      HTML::Embperl - Copyright (c) 1997-1999 Gerald Richter / ECOS
      </font></p>
    </blockquote>
  </blockquote>
  </td></tr></table></body>
  </html>
  </BODY>
  
  </HTML>
  
  
  
  1.1                  modperl-site/embperl/Intrors.pod.cont.html
  
  Index: Intrors.pod.cont.html
  ===================================================================
  <HTML>
  <HEAD>
  <TITLE>Recordset Introduction - Content</TITLE>
  <LINK REV="made" HREF="mailto:root@venus.gr.ecos.de">
  </HEAD>
  
  <BODY bgcolor="#FFFFFF">
  <blockquote>
    <blockquote>
      <H1><strong>
  <A NAME="Content-Intrors.pod.cont">Recordset Introduction - Content</a></strong></h1>
    </blockquote>
  </blockquote>
  
  <img src="line.jpg" align="left" alt="________" WIDTH="732" HEIGHT="35">
  <BR CLEAR=LEFT>
  <blockquote>
    <blockquote>
  [<a href="index.html">HOME</a>]&nbsp;&nbsp; [<a href="Intrors.pod.cont.html">CONTENT</a>]&nbsp;&nbsp; [<a href="Intrors.pod.1.html">NEXT (Embperl and DBIx::Recordset)</a>]&nbsp;&nbsp; <br><HTML>
  <HEAD>
  <TITLE>Recordset Introduction - Content</TITLE>
  <LINK REV="made" HREF="mailto:root@venus.gr.ecos.de">
  </HEAD>
  
  <BODY>
  
  <!-- INDEX BEGIN -->
  
  <UL>
  
  	<LI><A href="Intrors.pod.1.html#Embperl_and_DBIx_Recordset">Embperl and DBIx::Recordset</A>
  	<UL>
  
  		<LI><A href="Intrors.pod.1.html#Overview">Overview</A>
  		<LI><A href="Intrors.pod.1.html#Embperl">Embperl</A>
  	</UL>
  
  	<LI><A href="Intrors.pod.2.html#Basic_Example">Basic Example </A>
  	<UL>
  
  		<LI><A href="Intrors.pod.2.html#Search">Search</A>
  		<LI><A href="Intrors.pod.2.html#Display_the_table">Display the table</A>
  		<LI><A href="Intrors.pod.2.html#Supplying_query_parameters">Supplying query parameters</A>
  	</UL>
  
  	<LI><A href="Intrors.pod.3.html#Multiple_tables">Multiple tables</A>
  	<UL>
  
  		<LI><A href="Intrors.pod.3.html#DBIx_Database">DBIx::Database</A>
  		<LI><A href="Intrors.pod.3.html#Sub_Objects">Sub-Objects</A>
  	</UL>
  
  	<LI><A href="Intrors.pod.4.html#Modify_the_Database">Modify the Database</A>
  </UL>
  <!-- INDEX END -->
  <hr><p>[<a href="index.html">HOME</a>]&nbsp;&nbsp; [<a href="Intrors.pod.cont.html">CONTENT</a>]&nbsp;&nbsp; [<a href="Intrors.pod.1.html">NEXT (Embperl and DBIx::Recordset)</a>]&nbsp;&nbsp; <br>
      <font color="#808080">___________________________________________________________________________________<br>
      HTML::Embperl - Copyright (c) 1997-1999 Gerald Richter / ECOS
      </font></p>
    </blockquote>
  </blockquote>
  </td></tr></table></body>
  </html>
  
  </body></html>