Database Articles

  Home arrow Database Articles arrow Page 14 - 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 - Editing data


    (Page 14 of 16 )

    So far we have viewed and deleted database content. But sometimes we need to edit database content. For this we will modify our previously coded input.php file. By now you are familiar with the concept of passing variables by URL. We will call this modified script addedit.php:

    <HTML>
    <?php
    if($submit)
    {
     $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";
    }
    else if($update)
    {
     $db = mysql_connect("localhost", "root","");
     mysql_select_db("learndb",$db);
     $sql = "UPDATE personnel SET firstname='$first', 
     lastname='$last', nick='$nickname', email='$email', salary='$salary' WHERE id=$id";
     $result = mysql_query($sql);
     echo "Thank you! Information updated.\n";
    }
    else if($id)
    {
     $db = mysql_connect("localhost", "root", "");
     mysql_select_db("learndb",$db);
     $result = mysql_query("SELECT * FROM personnel WHERE id=$id",$db);
     $myrow = mysql_fetch_array($result);
    ?>
    <form method="post"action="<?php echo $PHP_SELF?>">
    <input type="hidden"name="id"value="<?php echo $myrow["id"]?>">
    First name:<input type="Text"name="first"value="<?php echo $myrow["firstname"]?>"><br>
    Last name:<input type="Text" name="last" value="<?php echo $myrow["lastname"]?>"><br>
    Nick Name:<input type="Text" name="nickname" value="<?php echo $myrow["nick"]?>"><br>
    E-mail:<input type="Text" name="email" value="<?php echo $myrow["email"]?>"><br>
    Salary:<input type="Text" name="salary" value="<?php echo $myrow["salary"]?>"><br>
    <input type="Submit" name="update" value="Update information"></form>
    <?
    }
    else
    {
    ?>
    <form method="post" action="<?php echo $PHP_SELF?>">
    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>

    Hmmm... the code looks quite complex. But really it isn't. Previously input. php had two features: it could add information to the database or could show the form. We'll add two more features to it: the ability to show the same form but with values of a particular person already there and the ability to update records for that person. The SQL commands for entering new information and updating existing information are different, so we can't use our previous code for entering information.

    The script searches for the $submit variable. If it contains some value, then someone submitted new data and the information is entered into the database. If $submit does not contain any value, then someone might have just posted their updated information, so we check $update. If it contains a value, then we update that person's record with the SQL statement "UPDATE personnel SET fieldname1= '$variablename1', fieldname2= '$variablename2' ......... WHERE id=$id";". Otherwise, if someone provided the id in the query string, we show that person's information, but this time in a form so he may change it. If all these are not the case, we simply have to show the old form.

    Experiment with the script. Open it with your browser to see what comes up. Then call it providing query string ?id=1. Change the information and click update. Verify whether the database is updated by viewing the database with viewdb3.php.

    Another new element was just introduced. It is the global PHP variable $PHP_SELF. This variable always contains the name of the script it is in and its location. We have used this variable in a 'form action' so no matter what you name this file, this script will always post information to itself.

    Once again we modify our viewing script incorporating this feature. Here's the listing for viewdb4.php:

    <HTML> 
    <?php 
    $db = mysql_connect("localhost", "root", ""); 
    mysql_ select_db("learndb",db); 
    $result = mysql_query("SELECT * FROM personnel",$db); 
    echo "<TABLE BORDER=2>"; 
    echo"<TR><TD><B>Full Name</B><TD><B>Nick Name</B><TD><B>Options</B></TR>"; 
    while($myrow = mysql_fetch_array($result)) 

    echo "<TR><TD>".$myrow["firstname"]." ".$myrow["lastname"]."</a><TD>".$myrow["nick"];
    echo "<TD><a href=\"view.php?id=".$myrow[id]."\">View</a> ";
    echo "<a href=\"delete.php?id=".$myrow[id]."\">Delete</a> ";
    echo "<a href=\"addedit.php?id=".$myrow[id]."\">Edit</ a>";

    echo "</TABLE>"; 
    ?> 
    </HTML>

    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 2 - Follow our Sitemap