SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot vbthanks's Avatar
    Join Date
    May 2001
    Location
    Sydney
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi all,
    How do I send the contents of a web form to a table in a MySQL database ?

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    The form should have an "action" attribute that lists a .php script. When the user clicks on the submit button, the .php script will be called and executed. Each form input element has a name, like 'address', and the names will be available to your php script as $address. Then you connect to your database and insert the variables. See this tutorial:

    http://www.webmasterbase.com/article.php/228/

  3. #3
    SitePoint Zealot vbthanks's Avatar
    Join Date
    May 2001
    Location
    Sydney
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks very much - anymore examples anyone ?
    Regards,
    Mike

  4. #4
    What's HTML?
    Join Date
    May 2001
    Location
    San Diego, CA
    Posts
    1,701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mike, the code below would be another example. It's a pretty simple script that allows me to add information about an article author to a table containing only authors. Pay attention to the databaseEntry() function.

    PHP Code:
    function printForm()
    {
            /* Prints out HTML form to allow user to create new author account.
               Contains three form fields: authorname, authoremail, authorbyline.
               Contains one hidden field: form.
            */

        global $authorname, $authoremail, $authorbyline;    //Global variables from input form.
        $scriptaddress = "http://" . getenv("HTTP_HOST") . getenv("SCRIPT_NAME");
        
        ?>
        
        A
        <form action="<?php print "$scriptaddress"?>" method="post">
        <table align="center" border=0 cellpadding=5 bgcolor="black">
            <tr>
                <td valign="top" bgcolor="white">Author Name:</td>
                <td valign="top" bgcolor="white"><INPUT TYPE="text" NAME="authorname" size="40" value="<?php print "$authorname"?>"></td>
            </tr>
            <tr>
                <td valign="top" bgcolor="white">Author Email:</td>
                  <td valign="top" bgcolor="white"><INPUT TYPE="text" NAME="authoremail" size="40" value="<?php print "$authoremail"?>"></td>
            </tr>
            <tr>
                <td valign="top" bgcolor="white">Author Byline:</td>
                <td valign="top" bgcolor="white"><TEXTAREA NAME="authorbyline" ROWS="3" COLS="35"><?php print "$authorbyline"?></TEXTAREA></td>
            </tr>
            <tr>
                <td valign="top" bgcolor="white" colspan="2">
                    <center><input type="hidden" name="form" value="1"><input type="reset"> <input type="submit" value="Add Author"></center>
                </td>
            </tr>
        </table>
        </FORM>
        
        <?php
        
        $authoremail 
    strtolower($authoremail);    //Converts $authoremail to lowercase.
        
    }

    /************************************************************/

    function errorCheck()
    {               
    // Checks over all form input information for valid responses.
        
    global $authorname$authoremail$authorbyline;
            
            if (empty(
    $authorname))
            {
                
    $errormsg .= "<li><font color=red>No author name specified.</font><br>\n";
                
    $error 1;
            }

            if (empty(
    $authoremail))
            {
                
    $errormsg .= "<li><font color=red>No author email specified.</font><br>\n";
                
    $error 1;
            }
            
            elseif (empty(
    $authoremail))
            {
                
    $errormsg .= "<li><font color=red>Author email in incorrect syntax.</font><br>\n";
                
    $error 1;
            }
                  
            if (empty(
    $authorbyline))
        {
                
    $errormsg .= "<li><font color=red>No author byline specified.</font><br>\n";
                   
    $error 1;
            }
            
            if (
    $error)
            {
            
    printHeader("JustForWebmasters.com: Error Adding New Author""Error Adding New Author");
            
    ?>
            
            The following errors have occurred while processing your request:<br>
            <ul>
            <?php print $errormsg?>
            </ul>
            <p>
            Please submit your information again using the form provided below.  Thank you.
            <p>
            
            <?php printForm();
            
    printFooter();   
            
            exit;
            }
    }

    /************************************************************/

    function databaseEntry()
    {        
    // Inserts records into database
        
    global $cnx$db_name$table_name;    //Global variables needed to connect to proper database.
        
    global $authorname$authoremail$authorbyline;    //Global variables from input form.
        
        
    $aname addslashes($authorname);
        
    $aemail addslashes($authoremail);
        
    $abyline addslashes($authorbyline);
        
        
    $db mysql_select_db($db_name$cnx) or die("Couldn't select database.");
        
    $sql "INSERT INTO $table_name (Name, Email, Byline) VALUES ('$aname', '$aemail', '$abyline')";
        
    $result mysql_query($sql,$cnx) or die("Could not execute query. The error is on the SQL or connection line.");

        print 
    "Successful!";

    }

    if(
    $form)
    {
        
    errorCheck();
        
    databaseEntry();
    }
    else
    {
        
    printHeader("JustForWebmasters.com: Add New Author""Add New Author");
        
    printForm();
        
    printFooter();
    }

    ?>
    Ryan Kuhle - A Proud Advisor - Got Questions? Just Ask!
    Get your website started for less than $20! Click Here


Bookmarks

Posting Permissions

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