Database Articles

  Home arrow Database Articles arrow Page 9 - Create dynamic sites with PHP & MySQL
DATABASE ARTICLES

Create dynamic sites with PHP & MySQL
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 54
    2002-05-19

    Table of Contents:
  • Create dynamic sites with PHP & MySQL
  • Introduction and installation
  • Installing Apache server routines
  • Installing MySQL
  • Installing PHP
  • Your first script
  • Your first database
  • Where's my view?
  • Creating an HTML form
  • Putting it together
  • Passing variables
  • Viewing individual rows
  • Deleting rows
  • Editing data
  • Searching our data
  • Tips for common tasks

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Create dynamic sites with PHP & MySQL - Creating an HTML form


    (Page 9 of 16 )

    So now you can view records stored in your MySQL database and display them in your browser using PHP. But you want to add new record. Assuming that you know about HTML forms, let's code a page that will do just that. First we'll create a static form, datain.html:

    <HTML> 
    <BODY> 
    <form method="post" action="datain.php"> 
    First name:<input type="Text" name="first"><br> 
    Last name:<input type="Text" name="last"><br> 
    Nick Name:<input type="Text" name="nickname"><br> 
    E-mail:<input type="Text" name="email"><br> 
    Salary:<input type="Text" name="salary"><br> 
    <input type="Submit" name="submit" value="Enter information"> 
    </form> 
    </HTML>

    Now we have a form that will post the information to a page "datain.php". We must code this page so that it is able to process the posted data and send it to our MySQL database. The following listing of datain.php will do that:

    <HTML> 
    <?php 
    $db = mysql_connect("localhost", "root",""); 
    mysql_select_db("learndb",$db); 
    $sql = "INSERT INTO personnel (firstname, lastname, nick, email, salary) VALUES ('$first', '$last', '$nickname','$email','$salary')";
    $result = mysql_query($sql); 
    echo "Thank you! Information entered.\ n"; 
    ?> 
    </HTML>

    The first 3 lines are same as before, only we use the SQL command "INSERT INTO", which means insert into the database into the columns specified (here firstname, lastname, nick, email) the data contained in the variable '$first', '$last', '$nickname', '$email' respectively.

    But where did these variables come from? Well, PHP has a wonderful way of creating the variables automatically from the data posted to it. So the text box with name "first" created the variable $first and it contained the text typed in that textbox.

    Important Note: As of PHP 4.2, php no longer automatically creates the variables for you. If you want posted variables to be automatically created. You have to edit your php.ini file and set the register_globals variable to on. The line should read:

    register_globals = on

    If that is not possible, you have to initialize the variables in your code everytime. The method is, if you are posting a form with the field "name" you have to write the following code in the page where you are posting:

    <?php
    $name
    =$_POST[name];        //for post method
    $name=$_GET[name];        //for get method
    ?>

    The general format is:

    <?php
    $variablename
    =$_POST[the_field_posted_by_form];
    ?>

    So the above code will look like:

    &lt;HTML&gt;
    &lt;?php
    $db = mysql_connect("localhost", "root","");
    mysql_select_db("learndb",$db);
     
    //for new PHP
    $first=$_POST[first];
    $last=$_POST[last];
    $nickname=$_POST[nickname];
    $email=$_POST[email];
    $salary=$_POST[salary];
    //
     
    $sql = "INSERT INTO personnel (firstname, lastname, nick, email, salary) 
    VALUES ('$first','$last','$nickname','$email','$salary')";
    $result = mysql_query($sql);
    echo "Thank you! Information entered.\n";
    ?&gt;
    &lt;/HTML&gt;

    More Database Articles Articles
    More By Codewalkers

    blog comments powered by Disqus

    DATABASE ARTICLES ARTICLES

    - Completing a Book Inventory Management System
    - Uploading Images for a Book Inventory Manage...
    - Finishing the Add Book Story for a Book Inve...
    - Integration Testing for a Book Inventory Man...
    - User Stories for a Book Inventory Management...
    - Unit Testing a Book Inventory Management Sys...
    - Testing a Book Inventory Management System
    - Implementing Models for a Book Inventory Man...
    - Book Inventory Application: Publishers and B...
    - Handling Publishers in a Book Inventory Mana...
    - Publisher Administration for Book Inventory ...
    - Book Inventory Management
    - Using the SQL Reference Manual
    - Using Oracle SQL Developer with SQL Statemen...
    - Fixing Errors with Oracle SQL Developer


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 11 - Follow our Sitemap