Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Aug 2005
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts

    tutorial - simple survey using pear DB

    I've created a tutorial to show you how you might handle a simple survey using pear's DB class. This doesn't have any validation, and I'll create another tutorial going into validation. I hope this helps get an understanding of php and how easy it can be to create and handle forms and working with databases.

    This has three pages form.php, form_insert.php and connect.php. form.php is the page that just simply has the html form that the user will use. form_insert.php is the page that will handle the form and insert the values into the database. connect.php is the page that handles the connection to the database and will be included in the form_insert.php page.

    The database create table statement.
    PHP Code:
    create table survey (
    surveyID int not null primary key,
    knowledge int,
    pear int,
    surveys int

    form.php
    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

    <
    html>
    <
    head>
        <
    title>Survey</title>
    </
    head>

    <
    body>
    <
    form method="post" action="form_insert.php">
     <
    table align="center">
      <
    tr>
       <
    td>Rate your knowledge of PHP:</td>
       <
    td>
        <
    select name="knowledge">
         <
    option value=''>Please Select</option>
         <
    option value='1'>1</option>
         <
    option value='2'>2</option>
         <
    option value='3'>3</option>
         <
    option value='4'>4</option>
         <
    option value='5'>5</option>
        </
    select>
       </
    td>
      </
    tr>
      <
    tr>
       <
    td>Have you ever used pear DB?</td>
       <
    td>
        <
    select name="pear">
         <
    option value=''>Please Select</option>
         <
    option value='1'>Yes</option>
         <
    option value='0'>No</option>
        </
    select>
       </
    td>
      </
    tr>
      <
    tr>
       <
    td>Do you make alot of surveys?</td>
       <
    td>
        <
    select name="surveys">
         <
    option value=''>Please Select</option>
         <
    option value='1'>Yes</option>
         <
    option value='0'>No</option>
        </
    select>
       </
    td>
      </
    tr>
      <
    tr>
       <
    td><input type='submit' name='Submit'></td>
      </
    tr>
     </
    table>
    </
    form>
    </
    body>
    </
    html
    connect.php
    PHP Code:
    <?php
    /*
    include DB.php is part of the pear DB class. This is not a file that you will find
    in its current working directory, its in the php include path. You need to have
    php configured with pear (--with-pear) and then go into your shell and install DB. 

    To install pear DB you would simply run the following line in a shell.
    pear install DB
    */
    include "DB.php";


    /*
    This is our database connection and we are putting it into a database handle
    called $dbh. $dbh is what we will use throughout our code to work with the 
    database. 

    you will want to change the following to fit the values associated with your 
    database.
    */
    $dbh DB::connect('mysql://username:password@host/database');
    ?>
    form_insert.php
    PHP Code:
    <?php
    // include the connection to the database
    require_once "connect.php";

    /*
    Loop through all the post variables assigning the name of the field to the 
    variable $n and the value of the field to the variable $v.

    Because in this example we are only expecting numeric values, we check to see if
    the value is numeric. If it is, then we assign the name of the associative array 
    to the name of the field, which is and in this example has to be the same name
    as the field in the database. Then we assign the value to that element in the array.
    */
    foreach ($_POST as $n=>$v) {
        if (
    is_numeric($v)) {
            
    $insert[$n] = $v;
        }
    }

    /*
    We are using pear DB class to work with our database, and because pear DB is used 
    for multiple databases, we want to use sequencing. Not all databases allow auto 
    sequencing like (mysql's auto_increment) or (mssql's identity), so we need to
    set up a table to handle these sequences. The database user will need to have
    create table permissions for this to work, because pear will create this table
    for you the first time it is ran. 

    $dbh is our database handle, and we are getting the nextID out of our survey
    sequencing table. We have named our sequencing table the same as the table
    we are associating it to, however when this is ran, it creates a table called 
    survey_seq.
    */
    $insert['surveyID'] = $dbh->nextID('survey');

    /*
    The example below is the main reason I love the pear DB class. This next statement
    is automatically creating my insert statment and executing it for me. 

    $dbh again is our database handle
    autoExecute is going to prepare and execute the query.
    "survey" is the table name in the database

    $insert is our insert array which will look like this.
    Array
    (
        [knowledge] => 3
        [pear] => 1
        [surveys] => 0
        [surveyID] => 3
    )

    DB_AUTOQUERY_INSERT is telling autoExecute that we want to 
    create and execute an insert statement. If you wanted to do an 
    update it would look something like the following.

    $dbh->autoExecute("survey", $insert, DB_AUTOQUERY_UPDATE, "surveyID = $surveyID");
    "surveyID = $surveyID" is the where clause, and of course your array would have 
    to change to not include surveyID in it.
    */
    $dbh->autoExecute("survey"$insertDB_AUTOQUERY_INSERT);

    // here we are unsetting the $insert array to free up some memory.
    unset($insert);

    /*
    This is how you might redirect your user to a thank you page or
    anywhere else you might want to take them next.
    */
    header ("Location: thank_you.php");
    ?>

  • #2
    Regular Coder xconspirisist's Avatar
    Join Date
    Jun 2006
    Location
    Great Britain.
    Posts
    138
    Thanks
    1
    Thanked 6 Times in 6 Posts
    It looks very nice indeed. I've never seen autoExecute before, but that looks like a rather useful feature I may well use in the future.

    I commend you on your use of comments within the source and that you code looks very portable.

    The input validation is excellent.

    I am surprised at how having the form declared explicitly, rather than generated makes it the code so very elegant.

    One suggestion at the moment:

    1) You could do with some error handling to catch if the insert was unsuccessful.
    If I have been helpful, use the "thank" button - It makes me happy!

    xconspirisist.co.uk - homepage of my online alias
    technowax.net - a community for people interested in all forms of modern technology.

  • #3
    New Coder
    Join Date
    Aug 2005
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you are correct, that is a bad habit of mine. the autoExecute with error handling would look something like so.

    PHP Code:
    $sth $dbh->autoExecute("survey"$insertDB_AUTOQUERY_INSERT);
    if (
    $dbh::isError($sth)) {
        die(
    $sth->getMessage() . "<br>" $sth->getUserInfo);

    and thank you for your comments and mentioning that i forgot the error handling.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •