Pages

Tuesday, July 10, 2012

Inserting data into a database table

This script is a simple code that inserts data into a database. Save this file as add_record.php. However ensure it is saved in the same folder with the connection.php file. Run your file through the browser. I have used an object oriented approach in a simple manner to introduce any beginner of OOP.
<?php

/*

    I have assumed you have created a database with a table named address_bk
    It should have the following fields:
    id: int(3) Set as primary
    fname: VARCHAR(20)
    lname: VARCHAR(20)
    phone: VARCHAR(10)
    email_address: VARCHAR(30)
    NB: This table will be used for the next several code samples
    Reach me at: allan.koskei@gmail.com
   
*/

class InsertData
{
   
    private $fname, $lname, $phone, $email_address;
   
    #This is the class constructor
    public function __construct($fname, $lname, $phone, $email_address)
    {
        $this->fname = $fname;
        $this->lname = $lname;
        $this->phone = $phone;
        $this->email_address = $email_address;
        $this->insertQuery(); //Instanciating the object will automatically call the insert statement
    }
   
    public function insertQuery()
    {
        $sql = "INSERT INTO address_bk(fname, lname, phone, email_address)
            VALUES('$this->fname', '$this->lname', '$this->phone', '$this->email_address')";
        $result = mysql_query($sql);
        //If record has been successfully added
        if($result){
            echo "Contact has been successfully added";
        }
        else{
            echo "Error! Inserting new contact";
        }
    }
}

//If the form submit button has been clicked

if($_SERVER['REQUEST_METHOD']=='POST')
{
    include_once 'connection.php';
   
    //Instantiate the class(InsertData class call)
    new InsertData($_POST['fname_post'], $_POST['lname_post'], $_POST['phone_post'], $_POST['email_post']);
   
    //Close connection after transaction
    mysql_close($server_connect);
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Insert Data Demo</title>
</head>
<body>
<h2>Insert Data Demo</h2>
<form action="<?php $_SERVER['PHP_SELF']?>" method="post">
    <table>
        <tr>
            <td>Firstname</td>
            <td><input type="text" name="fname_post" value="" /></td>
        </tr>
        <tr>
            <td>Lastname</td>
            <td><input type="text" name="lname_post" value="" /></td>
        </tr>
        <tr>
            <td>Phone number</td>
            <td><input type="text" name="phone_post" value="" /></td>
        </tr>
        <tr>
            <td>Email Address</td>
            <td><input type="text" name="email_post" value="" /></td>
        </tr>
        <tr>
            <td></td>
            <td>
                <input type="submit" value="Add Contact" name="submit_data" />
                <input type="reset" value="Clear" name="clear_date" />
            </td>
        </tr>
    </table>
</form>
</body>
</html>



No comments:

Post a Comment