MySQL Database Guide to Installation and Basic Usage with PHP


(Important: Read agreement at bottom of document before continuing)


  1. Install the group 'MySQL Database' group which contains the database.

    [root@lintush ~]# yum groupinstall 'MySQL Database'



  1. Install packages php-mysql and mod_auth_mysql to interact with the MySQL database. This module is not installed automatically at least it didn't for me. But running this command will either install the module or let you know it's already installed.

    [root@lintush ~]# yum install php-mysql mod_auth_mysql


  2. Setup MySQL to start automatically on runlevels 2, 3, and 5.

    [root@lintush ~]# chkconfig --level 235 mysqld on


  3. Start the MySQL daemon.

    [root@lintush ~]# service mysqld start


    Initializing MySQL database: Installing MySQL system tables...
    OK
    Filling help tables...
    OK


    To start mysqld at boot time you have to copy
    support-files/mysql.server to the right place for your system


    PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
    To do so, start the server, then issue the following commands:
    /usr/bin/mysqladmin -u root password 'new-password'
    /usr/bin/mysqladmin -u root -h lintush.m2t.hm password 'new-password'
    See the manual for more instructions.
    You can start the MySQL daemon with:
    cd /usr ; /usr/bin/mysqld_safe &


    You can test the MySQL daemon with mysql-test-run.pl
    cd mysql-test ; perl mysql-test-run.pl


    Please report any problems with the /usr/bin/mysqlbug script!


    The latest information about MySQL is available on the web at
    http://www.mysql.com
    Support MySQL by buying support/licenses at http://shop.mysql.com
    [ OK ]
    Starting MySQL: [ OK ]





  1. Set the database administrator password(root password that is). Replace 'secret_password' with the root password.

    [root@lintush ~]# /usr/bin/mysqladmin -u root password 'secret_password'


  2. Set the hostname that will be allowed to access the database. Replace 'secret_password' with the root password.
    [root@lintush ~]# /usr/bin/mysqladmin -u root -h lintush.m2t.hm password 'secret_password'



  1. (OPTIONAL) This step is not necessary but if you want to install the test data then run the following command:



    [root@lintush ~]# mysql_install_db
    Installing MySQL system tables...
    OK
    Filling help tables...
    OK


    To start mysqld at boot time you have to copy
    support-files/mysql.server to the right place for your system


    PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
    To do so, start the server, then issue the following commands:
    /usr/bin/mysqladmin -u root password 'new-password'
    /usr/bin/mysqladmin -u root -h lintush.m2t.hm password 'new-password'
    See the manual for more instructions.
    You can start the MySQL daemon with:
    cd /usr ; /usr/bin/mysqld_safe &


    You can test the MySQL daemon with mysql-test-run.pl
    cd mysql-test ; perl mysql-test-run.pl


    Please report any problems with the /usr/bin/mysqlbug script!


    The latest information about MySQL is available on the web at
    http://www.mysql.com
    Support MySQL by buying support/licenses at http://shop.mysql.com


  1. Login to the MySQL database. This will use user root and will prompt for password. (-u for user)(-p for password prompt).


    root@lintush ~]# mysql -u root -p
    Enter password:
    Welcome to the MySQL monitor. Commands end with ; or \g.
    Your MySQL connection id is 4
    Server version: 5.0.45 Source distribution


    Type 'help;' or '\h' for help. Type '\c' to clear the buffer.


    mysql>


  1. (OPTIONAL)For help with MySQL, type help.





    mysql> help ;


    For information about MySQL products and services, visit:
    http://www.mysql.com/
    For developer information, including the MySQL Reference Manual, visit:
    http://dev.mysql.com/
    To buy MySQL Network Support, training, or other products, visit:
    https://shop.mysql.com/


    List of all MySQL commands:
    Note that all text commands must be first on line and end with ';'
    ? (\?) Synonym for `help'.
    clear (\c) Clear command.
    connect (\r) Reconnect to the server. Optional arguments are db and host.
    delimiter (\d) Set statement delimiter. NOTE: Takes the rest of the line as new delimiter.
    edit (\e) Edit command with $EDITOR.
    ego (\G) Send command to mysql server, display result vertically.
    exit (\q) Exit mysql. Same as quit.
    go (\g) Send command to mysql server.
    help (\h) Display this help.
    nopager (\n) Disable pager, print to stdout.
    notee (\t) Don't write into outfile.
    pager (\P) Set PAGER [to_pager]. Print the query results via PAGER.
    print (\p) Print current command.
    prompt (\R) Change your mysql prompt.
    quit (\q) Quit mysql.
    rehash (\#) Rebuild completion hash.
    source (\.) Execute an SQL script file. Takes a file name as an argument.
    status (\s) Get status information from the server.
    system (\!) Execute a system shell command.
    tee (\T) Set outfile [to_outfile]. Append everything into given outfile.
    use (\u) Use another database. Takes database name as argument.
    charset (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
    warnings (\W) Show warnings after every statement.
    nowarning (\w) Don't show warnings after every statement.


    For server side help, type 'help contents'


    mysql>
  1. (OPTIONAL)For server side help, do the following:

    mysql> help contents;
    You asked for help about help category: "Contents"
    For more information, type 'help <item>', where <item> is one of the following
    categories:
    Account Management
    Administration
    Data Definition
    Data Manipulation
    Data Types
    Functions
    Functions and Modifiers for Use with GROUP BY
    Geographic Features
    Language Structure
    Storage Engines
    Stored Routines
    Table Maintenance
    Transactions
    Triggers


    mysql>


  2. (OPTIONAL)For something more specific, like displaying help for 'Data Definition' do the following:


    mysql> help Data Definition;
    You asked for help about help category: "Data Definition"
    For more information, type 'help <item>', where <item> is one of the following
    topics:
    ALTER DATABASE
    ALTER TABLE
    ALTER VIEW
    CONSTRAINT
    CREATE DATABASE
    CREATE INDEX
    CREATE TABLE
    CREATE VIEW
    DROP DATABASE
    DROP INDEX
    DROP TABLE
    DROP VIEW
    MERGE
    RENAME TABLE


    mysql>


  1. (OPTIONAL) If you need help with creating a database. This will show you the syntax.


    mysql> help create database;
    Name: 'CREATE DATABASE'
    Description:
    Syntax:
    CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
    [create_specification [create_specification] ...]


    create_specification:
    [DEFAULT] CHARACTER SET charset_name
    | [DEFAULT] COLLATE collation_name


    CREATE DATABASE creates a database with the given name. To use this
    statement, you need the CREATE privilege for the database. CREATE
    SCHEMA is a synonym for CREATE DATABASE as of MySQL 5.0.2.


    URL: http://dev.mysql.com/doc/refman/5.0/en/create-database.html


    mysql>



  1. Let's see what databases are on the server.

    mysql> show databases;
    +--------------------+
    | Database |
    +--------------------+
    | information_schema |
    | mysql |
    | test |
    +--------------------+
    3 rows in set (0.00 sec)


    mysql>



  1. Let's create a database.

    mysql> create database contacts;
    Query OK, 1 row affected (0.00 sec)


    mysql>


  2. Display the databases again. You should now see the database you created.

    mysql> show databases;
    +--------------------+
    | Database |
    +--------------------+
    | information_schema |
    | contacts |
    | mysql |
    | test |
    +--------------------+
    4 rows in set (0.00 sec)


  3. Switch to the database you just created.

    mysql> use contacts;
    Database changed


    mysql>

  4. Display the tables in this database. It should be none, of course, we haven't created any tables yet.



    mysql> show tables;
    Empty set (0.00 sec)


    mysql>



  1. Let's create a table called 'family' (int for integer and varchar for text):



    mysql> create table family
    -> (
    -> fam_id int not null,
    -> fam_fname varchar(50) not null,
    -> fam_lname varchar(50) not null,
    -> fam_address1 varchar(50) not null,
    -> fam_address2 varchar(50) not null,
    -> fam_phone varchar(13) not null,
    -> fam_comments varchar(100) not null
    -> );
    Query OK, 0 rows affected (0.00 sec)


    mysql>



  1. Display the tables again and the table should now show up as a table in the contacts database.


    mysql> show tables;
    +--------------------+
    | Tables_in_contacts |
    +--------------------+
    | family |
    +--------------------+
    1 row in set (0.00 sec)


    mysql>

  1. Create another table called 'friends'.


    mysql> create table friends ( fr_id int not null, fr_fname varchar(50) not null, fr_lname varchar(50) not null, fr_address1 varchar(50) not null, fr_address2 varchar(50) not null, fr_phone varchar(13) not null, fr_comments varchar(100) not null );
    Query OK, 0 rows affected (0.01 sec)


    mysql>


  1. Display tables again and the new table shows up.



    mysql> show tables;
    +--------------------+
    | Tables_in_contacts |
    +--------------------+
    | family |
    | friends |
    +--------------------+
    2 rows in set (0.00 sec)


    mysql>


  1. Create another table. Called it 'work.'


    mysql> create table work ( wr_id int not null, wr_fname varchar(50) not null, wr_lname varchar(50) not null, wr_address1 varchar(50) not null, wr_address2 varchar(50) not null, wr_phone varchar(13) not null, wr_comments varchar(100) not null );
    Query OK, 0 rows affected (0.00 sec)


    mysql>


  1. Display tables. Three tables should now show up.



    mysql> show tables;
    +--------------------+
    | Tables_in_contacts |
    +--------------------+
    | family |
    | friends |
    | work |
    +--------------------+
    3 rows in set (0.00 sec)


    mysql>


  1. Add this record to the table 'family.'

    mysql> insert into family values(1,'Mike','Robinson','5534 S. State','Chicago, IL 60635','(773)575-5678','He is graduating next month.');
    Query OK, 1 row affected (0.00 sec)


    mysql>


  2. Display all records in the 'family' table. You should see the record you just added.

    mysql> select * from family;
    +--------+-----------+-----------+---------------+-------------------+---------------+------------------------------+
    | fam_id | fam_fname | fam_lname | fam_address1 | fam_address2 | fam_phone | fam_comments |
    +--------+-----------+-----------+---------------+-------------------+---------------+------------------------------+
    | 1 | Mike | Robinson | 5534 S. State | Chicago, IL 60635 | (773)575-5678 | He is graduating next month. |
    +--------+-----------+-----------+---------------+-------------------+---------------+------------------------------+
    1 row in set (0.00 sec)


    mysql>


  3. Add another record to the 'family' table.


    mysql> insert into family (fam_id,fam_fname,fam_lname) values(2,'Tony',' Banderas');
    Query OK, 1 row affected, 4 warnings (0.00 sec)


    mysql>


  1. Display all records in the 'family' table. You should see both records you entered.


    mysql> select * from family;
    +--------+-----------+-----------+---------------+-------------------+---------------+------------------------------+
    | fam_id | fam_fname | fam_lname | fam_address1 | fam_address2 | fam_phone | fam_comments |
    +--------+-----------+-----------+---------------+-------------------+---------------+------------------------------+
    | 1 | Mike | Robinson | 5534 S. State | Chicago, IL 60635 | (773)575-5678 | He is graduating next month. |
    | 2 | Tony | Banderas | | | | |
    +--------+-----------+-----------+---------------+-------------------+---------------+------------------------------+
    2 rows in set (0.00 sec)


    mysql>


  1. Create a user that will be allowed to access this database on the localhost.


    mysql> grant all on contacts.* to 'test'@'localhost' identified by 'user' with grant option;
    Query OK, 0 rows affected (0.00 sec)


    mysql>

  1. Give user access to database from the Internet or other hosts.


    mysql> grant all on contacts.* to 'test'@'%' identified by 'user' with grant option;
    Query OK, 0 rows affected (0.00 sec)


    mysql>


  1. Run command below to apply user privileges.

    mysql> flush privileges;
    Query OK, 0 rows affected (0.00 sec)


    mysql>


  2. One final change. I forgot to change the ID's to auto increment and primary keys. Make the tables (family, friends, and work) id number auto-increment.

    mysql> use contacts;


    Database changed
    mysql> alter table family modify fam_id MEDIUMINT NOT NULL PRIMARY KEY AUTO_INCREMENT;
    Query OK, 9 rows affected (0.00 sec)
    Records: 9 Duplicates: 0 Warnings: 0


    mysql> alter table friends modify fr_id MEDIUMINT NOT NULL PRIMARY KEY AUTO_INCREMENT;
    Query OK, 0 rows affected (0.01 sec)
    Records: 0 Duplicates: 0 Warnings: 0


    mysql> alter table family modify fam_id MEDIUMINT NOT NULL PRIMARY KEY AUTO_INCREMENT;
    Query OK, 0 rows affected (0.00 sec)
    Records: 0 Duplicates: 0 Warnings: 0


    mysql>

  3. Exit the database.


    mysql> quit


    [root@lintush /]#




Testing Out MySQL and PHP

  1. Switch to the Apache directory.


    [root@lintush ~]# cd /var/www/html
    [root@lintush html]#


  1. Create a file called 'getchoice.php' with your editor(nano, vi, gedit, etc.) of choice.

    [root@lintush html]# gedit getchoice.php




  2. Paste the text below in the editor. Save the file and exit.


    <HTML>
    <HEAD>
    </HEAD>
    <BODY>
    <FORM ACTION="processchoice.php" METHOD="POST">
    <P><STRONG>What table would you like to work with?</STRONG></P>
    <SELECT NAME="table">
    <p>
    <?php


    $conn = mysql_connect("localhost", "test", "user"); // Provides MySQL with host & user credentials
    mysql_select_db("contacts",$conn); // Select database to work with
    $sql = "SHOW TABLES"; // Create query
    $result = mysql_query($sql, $conn) or die(mysql_error()); // Process query
    while($newArray = mysql_fetch_array($result)) {
    echo "<OPTION VALUE=\"".$newArray[0]."\">".$newArray[0]."</OPTION>";
    echo "<BR>";
    }
    echo "</SELECT>";
    php?>


    </P>
    <P><STRONG>What would you like to do?</STRONG></P>
    <P><INPUT TYPE="RADIO" CHECKED="checked" NAME="option" VALUE="add">Add Records<BR>
    <INPUT TYPE="RADIO" NAME="option" VALUE="del">Delete Records<BR>
    <INPUT TYPE="RADIO" NAME="option" VALUE="view">View Records</P>
    <INPUT TYPE="SUBMIT" VALUE="submit">
    </FORM>
    </BODY>
    </HTML>




  1. Create another file. Call this file 'processchoice.php.'

    [root@lintush html]# gedit processchoice.php



  2. Paste the text below in the editor. Save the file and exit.

    <?php
    session_start();
    if (!isset($_POST['table'])) {
    $table = $_SESSION['tb'];
    $choice = $_SESSION['op'];
    }else{
    $_SESSION['tb'] = $_POST['table'];
    $table = $_POST['table'];
    $_SESSION['op'] = $_POST['option'];
    $choice = $_POST['option'];
    }

    ProcessHTML_Top();
    switch($choice) {


    case "add" : AddRecords($table);
    break;
    case "del" : DeleteRecords($table);
    break;
    case "view" : ViewRecords($table);
    break;
    default : echo "Nothing was obtained.";
    }
    ProcessHTML_Bottom();




    function ProcessHTML_Top(){
    php?>


    <HTML>
    <HEAD>
    </HEAD>
    <BODY>


    <?php
    }


    function ProcessHTML_Bottom(){
    php?>


    </BODY>
    </HTML>


    <?php
    }






    function AddRecords($tb){


    $Fn = $_POST['Fname'];
    $Ln = $_POST['Lname'];
    $adr1 = $_POST['addrl1'];
    $adr2 = $_POST['addrl2'];
    $ph = $_POST['phone'];
    $cm = $_POST['comment'];
    if (!isset($_POST['submit'])) {
    php?>
    <H2>Working with table <?php echo "<FONT COLOR=\"RED\"><STRONG>$tb</STRONG></FONT>";php?></H2>
    <FORM ACTION="<?php echo $PHP_SELF;?>" METHOD="POST">
    <P>First Name:<INPUT TYPE="TEXT" SIZE="25" MAXLENGTH="50" NAME="Fname"><br />
    Last Name: <INPUT TYPE="TEXT" SIZE="25" MAXLENGTH="50" NAME="Lname"><br />
    Address line1: <INPUT TYPE="TEXT" SIZE="50" MAXLENGTH="50" NAME="addrl1"><br />
    Address line2: <INPUT TYPE="TEXT" SIZE="50" MAXLENGTH="50" NAME="addrl2"><br />
    Phone: <INPUT TYPE="TEXT" SIZE="13" MAXLENGTH="13" NAME="phone"><br />
    <TEXTAREA ROWS="5" COLS="20" NAME="comment" WRAP="physical"></textarea><br />
    </P>
    <INPUT TYPE="SUBMIT" value="submit" name="submit">
    </FORM>
    <?php
    ProcessHTML_Bottom();
    }else{
    $conn =mysql_connect("localhost", "test", "user");
    //pick the database to connect
    mysql_select_db("contacts",$conn);
    switch($tb){
    case 'family' : mysql_query("INSERT INTO $tb (fam_fname,fam_lname,fam_address1,fam_address2,fam_phone,fam_comments) VALUES('$Fn','$Ln','$adr1','$adr2','$ph','$cm')");
    break;
    case 'friends' : mysql_query("INSERT INTO $tb (fr_fname,fr_lname,fr_address1,fr_address2,fr_phone,fr_comments) VALUES('$Fn','$Ln','$adr1','$adr2','$ph','$cm')");
    break;
    case 'work' : mysql_query("INSERT INTO $tb (wr_fname,wr_lname,wr_address1,wr_address2,wr_phone,wr_comments) VALUES('$Fn','$Ln','$adr1','$adr2','$ph','$cm')");
    break;
    default : echo "No data was added! Something went wrong. Check your log.";
    }
    echo "<STRONG>Record Added!</STRONG><BR>";
    mysql_close($con);
    echo "<a href=\"getchoice.php\">Return to Main Menu</><BR>";
    echo "<a href=\"processchoice.php\">Add Another Contact</><BR>";
    }
    }




    function DeleteRecords($tbl){


    switch($tbl){

    case 'family' : ProcessFamily($tbl);
    break;
    case 'friends' : ProcessFriends($tbl);
    break;
    case 'work' : ProcessWork($tbl);
    break;
    default : echo "<h2>Nothing to do</h2>";

    }
    }






    function ProcessWork($tb){


    $conn =mysql_connect("localhost", "test", "user");
    //pick the database to connect
    mysql_select_db("contacts",$conn);
    echo "<H2>Delete Mode on table: <FONT COLOR=\"RED\"><STRONG>$tb</STRONG></FONT></H2>";

    echo "<FORM ACTION=\"".$PHP_SELF."\" METHOD=\"POST\">";
    if (isset($_POST['submit'])) {
    $recs = $_POST['recs'];
    echo "<font color=\"red\"><h2>Deleted records</h2>";
    echo "<table border=1><tr><td>Selected</td><td>Record#</td><td>First Name </td><td>Last Name</td><td>Address Line1</td><td>Address Line2</td><td>Phone#</td><td>Comments</td></tr>";
    foreach ($recs as $item) {
    $sql = "SELECT * FROM $tb where wr_id = $item";
    $result = mysql_query($sql, $conn) or die(mysql_error());
    while($newArray = mysql_fetch_array($result)) {
    $recno = $newArray['wr_id'];
    $fname = $newArray['wr_fname'];
    $lname = $newArray['wr_lname'];
    $adr1 = $newArray['wr_address1'];
    $adr2 = $newArray['wr_address2'];
    $phone = $newArray['wr_phone'];
    $comm = $newArray['wr_comments'];
    echo "<tr><td>$recno</td><td>$fname</td><td>$lname</td><td>$adr1</td><td>$adr2</td><td>$phone</td><td>$comm</td></tr>";
    }
    $sql = "DELETE FROM $tb where wr_id = $item";
    $result = mysql_query($sql, $conn) or die(mysql_error());
    }
    echo "</table><br><br></font>";
    }
    $sql = "SELECT * FROM $tb";
    $result = mysql_query($sql, $conn) or die(mysql_error());
    echo "<font color=\"blue\"><h2>Current Records on $tb database</h2></font>";
    echo "<table border=1><tr><td>Selected</td><td>Record#</td><td>First Name </td><td>Last Name</td><td>Address Line1</td><td>Address Line2</td><td>Phone#</td><td>Comments</td></tr>";
    while($newArray = mysql_fetch_array($result)) {
    $recno = $newArray['wr_id'];
    $fname = $newArray['wr_fname'];
    $lname = $newArray['wr_lname'];
    $adr1 = $newArray['wr_address1'];
    $adr2 = $newArray['wr_address2'];
    $phone = $newArray['wr_phone'];
    $comm = $newArray['wr_comments'];
    echo "<tr><td><input type=\"checkbox\" value=\"".$recno."\" name=\"recs[]\"></td><td>$recno</td><td>$fname</td><td>$lname</td><td>$adr1</td><td>$adr2</td><td>$phone</td><td>$comm</td></tr>";
    }
    echo "<th colspan=\"8\" align=\"center\"><INPUT TYPE=\"submit\" value=\"submit\" name=\"submit\"></th>";
    echo "</FORM>";
    echo "</table>";
    echo "<P><a href=\"getchoice.php\">Return to Main Menu</></P>";
    }








    function ProcessFriends($tb){
    $conn =mysql_connect("localhost", "test", "user");
    mysql_select_db("contacts",$conn);
    echo "<H2>Delete Mode on table: <FONT COLOR=\"RED\"><STRONG>$tb</STRONG></FONT></H2>";
    echo "<FORM ACTION=\"".$PHP_SELF."\" METHOD=\"POST\">";
    if (isset($_POST['submit'])) {
    $recs = $_POST['recs'];
    echo "<font color=\"red\"><h2>Deleted records</h2>";
    echo "<table border=1><tr><td>Selected</td><td>Record#</td><td>First Name</td><td>Last Name</td><td>Address Line1</td><td>Address Line2</td><td>Phone#</td><td>Comments</td></tr>";
    foreach ($recs as $item) {
    $sql = "SELECT * FROM $tb where fr_id = $item";
    $result = mysql_query($sql, $conn) or die(mysql_error());
    while($newArray = mysql_fetch_array($result)) {
    $recno = $newArray['fr_id'];
    $fname = $newArray['fr_fname'];
    $lname = $newArray['fr_lname'];
    $adr1 = $newArray['fr_address1'];
    $adr2 = $newArray['fr_address2'];
    $phone = $newArray['fr_phone'];
    $comm = $newArray['fr_comments'];
    echo "<tr><td>$recno</td><td>$fname</td><td>$lname</td><td>$adr1</td><td>$adr2</td><td>$phone</td><td>$comm</td></tr>";
    }
    $sql = "DELETE FROM $tb where fr_id = $item";
    $result = mysql_query($sql, $conn) or die(mysql_error());
    }
    echo "</table><br><br></font>";
    }
    $sql = "SELECT * FROM $tb";
    $result = mysql_query($sql, $conn) or die(mysql_error());
    echo "<font color=\"blue\"><h2>Current Records on $tb database</h2></font>";
    echo "<table border=1><tr><td>Selected</td><td>Record#</td><td>First Name </td><td>Last Name</td><td>Address Line1</td><td>Address Line2</td><td>Phone#</td><td>Comments</td></tr>";
    while($newArray = mysql_fetch_array($result)) {
    //give a name to the fields
    $recno = $newArray['fr_id'];
    $fname = $newArray['fr_fname'];
    $lname = $newArray['fr_lname'];
    $adr1 = $newArray['fr_address1'];
    $adr2 = $newArray['fr_address2'];
    $phone = $newArray['fr_phone'];
    $comm = $newArray['fr_comments'];
    echo "<tr><td><input type=\"checkbox\" value=\"".$recno."\" name=\"recs[]\"></td><td>$recno</td><td>$fname</td><td>$lname</td><td>$adr1</td><td>$adr2</td><td>$phone</td><td>$comm</td></tr>";
    }
    echo "<th colspan=\"8\" align=\"center\"><INPUT TYPE=\"submit\" value=\"submit\" name=\"submit\"></th>";
    echo "</FORM>";
    echo "</table>";
    echo "<P><a href=\"getchoice.php\">Return to Main Menu</></P>";
    }






    function ProcessFamily($tb){
    $conn =mysql_connect("localhost", "test", "user");
    mysql_select_db("contacts",$conn);
    echo "<H2>Delete Mode on table: <FONT COLOR=\"RED\"><STRONG>$tb</STRONG></FONT></H2>";
    echo "<FORM ACTION=\"".$PHP_SELF."\" METHOD=\"POST\">";
    if (isset($_POST['submit'])) {
    $recs = $_POST['recs'];
    echo "<font color=\"red\"><h2>Deleted records</h2>";
    echo "<table border=1><tr><td>Selected</td><td>Record#</td><td>First Name </td><td>Last Name</td><td>Address Line1</td><td>Address Line2</td><td>Phone#</td><td>Comments</td></tr>";
    foreach ($recs as $item) {
    $sql = "SELECT * FROM $tb where fam_id = $item";
    $result = mysql_query($sql, $conn) or die(mysql_error());
    while($newArray = mysql_fetch_array($result)) {
    $recno = $newArray['fam_id'];
    $fname = $newArray['fam_fname'];
    $lname = $newArray['fam_lname'];
    $adr1 = $newArray['fam_address1'];
    $adr2 = $newArray['fam_address2'];
    $phone = $newArray['fam_phone'];
    $comm = $newArray['fam_comments'];
    echo "<tr><td>$recno</td><td>$fname</td><td>$lname</td><td>$adr1</td><td>$adr2</td><td>$phone</td><td>$comm</td></tr>";
    }
    $sql = "DELETE FROM $tb where fam_id = $item";
    $result = mysql_query($sql, $conn) or die(mysql_error());
    }
    echo "</table><br><br></font>";
    }
    $sql = "SELECT * FROM $tb";
    $result = mysql_query($sql, $conn) or die(mysql_error());
    echo "<font color=\"blue\"><h2>Current Records on $tb database</h2></font>";
    echo "<table border=1><tr><td>Selected</td><td>Record#</td><td>First Name </td><td>Last Name</td><td>Address Line1</td><td>Address Line2</td><td>Phone#</td><td>Comments</td></tr>";
    while($newArray = mysql_fetch_array($result)) {
    $recno = $newArray['fam_id'];
    $fname = $newArray['fam_fname'];
    $lname = $newArray['fam_lname'];
    $adr1 = $newArray['fam_address1'];
    $adr2 = $newArray['fam_address2'];
    $phone = $newArray['fam_phone'];
    $comm = $newArray['fam_comments'];
    echo "<tr><td><input type=\"checkbox\" value=\"".$recno."\" name=\"recs[]\"></td><td>$recno</td><td>$fname</td><td>$lname</td><td>$adr1</td><td>$adr2</td><td>$phone</td><td>$comm</td></tr>";
    }
    echo "<th colspan=\"8\" align=\"center\"><INPUT TYPE=\"submit\" value=\"submit\" name=\"submit\"></th>";
    echo "</FORM>";
    echo "</table>";
    echo "<P><a href=\"getchoice.php\">Return to Main Menu</></P>";
    }




    function ViewRecords($tb){
    $conn =mysql_connect("localhost", "test", "user");
    //pick the database to connect
    mysql_select_db("contacts",$conn);
    //create the SQL statement
    $sql = "SELECT * FROM $tb";
    //execute the statement
    $result = mysql_query($sql, $conn) or die(mysql_error());
    //go through each row in the result set and display data
    echo "<table border=1><tr><td>Record#</td><td>First Name </td><td>Last Name</td><td>Address Line1</td><td>Address Line2</td><td>Phone#</td><td>Comments</td></tr>";



    switch($tb){


    case 'family' :
    while($newArray = mysql_fetch_array($result)) {
    //give a name to the fields
    $recno = $newArray['fam_id'];
    $fname = $newArray['fam_fname'];
    $lname = $newArray['fam_lname'];
    $adr1 = $newArray['fam_address1'];
    $adr2 = $newArray['fam_address2'];
    $phone = $newArray['fam_phone'];
    $comm = $newArray['fam_comments'];

    echo "<tr><td>$recno</td><td>$fname</td><td>$lname</td><td>$adr1</td><td>$adr2</td><td>$phone</td><td>$comm</td></tr>";
    }
    break;


    case 'friends' :
    while($newArray = mysql_fetch_array($result)) {
    //give a name to the fields
    $recno = $newArray['fr_id'];
    $fname = $newArray['fr_fname'];
    $lname = $newArray['fr_lname'];
    $adr1 = $newArray['fr_address1'];
    $adr2 = $newArray['fr_address2'];
    $phone = $newArray['fr_phone'];
    $comm = $newArray['fr_comments'];
    echo "<tr><td>$recno</td><td>$fname</td><td>$lname</td><td>$adr1</td><td>$adr2</td><td>$phone</td><td>$comm</td></tr>";
    }
    break;


    case 'work' :
    while($newArray = mysql_fetch_array($result)) {
    //give a name to the fields
    $recno = $newArray['wr_id'];
    $fname = $newArray['wr_fname'];
    $lname = $newArray['wr_lname'];
    $adr1 = $newArray['wr_address1'];
    $adr2 = $newArray['wr_address2'];
    $phone = $newArray['wr_phone'];
    $comm = $newArray['wr_comments'];
    echo "<tr><td>$recno</td><td>$fname</td><td>$lname</td><td>$adr1</td><td>$adr2</td><td>$phone</td><td>$comm</td></tr>";
    }
    break;
    }
    echo "</table>";
    echo "<P><a href=\"getchoice.php\">Return to Main Menu</></P>";
    }


    php?>

  3. Now run the following command to change the rights of the files. Just in case the rights are not set correctly.


    [root@lintush html]# chmod 755 *

  4. Now Open firefox and type http://localhost/getchoice.php You should get a working page that will allow you to insert, delete, or view records in the three tables (family, friends, and work) from the 'contacts' database.




    That's all!





Agreement

This guide is provided as is with no warranty, garantee, or liability of any kind. Use it at your own risk. I am not responsible for any damages arising from the use of this guide. If you agree to these terms than you will use the guide with the understanding that you will not hold me responsible for any damages, whatever their nature, resulting from the use of this guide.





Comments






Message posted on Sunday, December 9, 2007 at 09:40:10 pm By: Jesse
No picture
Available
Sorry about the double spacing. I just had a hard time getting this stuff to stay in single line. I will correct it as I have time.
Message posted on Tuesday, December 18, 2007 at 10:04:38 pm By: Jesse
No picture
Available
Finally got around to fix the double spacing.





Post your Comments






Enter your message:

Your name:

Date: Friday-November-21-2008 02:46:17 am
(Actual time will vary at the time the message is posted.)

You can also leave a picture to go with your message.(optional)
All image types supported.
Picture to upload:

Message: