Nunc rutrum nunc in enim bibendum at pellentesque est pellentesque. Pellentesque sodales bibendum elit non pulvinar. Mauris fermentum lectus ac risus dapibus cursus eu non ipsum.

  • Example Title 1

    Replace this sample description with your own description. A free premium template by NewBloggerThemes.com.

  • Example Title 2

    Replace this sample description with your own description. A free premium template by NewBloggerThemes.com.

  • Example Title 3

    Replace this sample description with your own description. A free premium template by NewBloggerThemes.com.

    Saturday, 26 January 2013

    This is my first practical tutorial using PHP/MYSQL. If you want to add a simple add/edit/delete functionality to category listing page. Then, first if all you have to create 5 php files as given below:

    1. db_connect.php
    2. add_category.php
    3. edit_category.php
    4. delete_category.php
    5. view_category.php

    We also need a table to create with the following structure given in below given image:
    Table Structure


    Now, let me describe the use of all these files.

    Step 1:
    "db_connect.php" is created in order to create a database connection between PHP & MYSQL. The file contains the below given code.
    <?php
    $connect=mysql_connect("localhost","root","") or die("Server Connection Failed");
    mysql_select_db("ebuksmania",$connect)
    ?>
    Here, "ebuksmania" is the name of the database, "localhost" is the name of the host/server and "root" is the username. This file is included in all the other php files as database connectivity is needed in other php files and this file will provide database connectivity to other files.

    Step 2:
    "add_category.php" is created in order to add category to the specified category table in MYSQL. The file contains the below given code.


    <?php
    include_once("db_connect.php");
    ?>
    <!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=iso-8859-1" />
    <title>Add Category</title>
    </head>
    <body>
    <form method="post">
    <table border="2">
    <tr><th colspan="2">Add Category</th></tr>
    <tr>
    <td>Category Name</td>
    <td><input type="text" name="category_name" /></td>
    </tr>
    <tr>
    <td><input type="submit" name="add_category" value="Add Category" /></td>
    </tr>
    </table>
    </form>
    </body>
    </html>
    <?php
    if(isset($_POST['add_category']))
    {
    $category_name=$_POST['category_name'];
    $date=date("Y-m-d H:i:s");
    $query="INSERT INTO `category_simple`(name,created_date,updated_date) VALUES('$category_name','$date','$date')";
    mysql_query($query) or die("Incorrect Query Error");
    header("Location: view_category.php");
    }
    ?>

    Step 3:
    "edit_category.php" is created in order to edit the name of the category if mistaken. The file contains the below given code.


    <?php
    include_once("db_connect.php");
    if(isset($_GET['id']))
    {
    $id=$_GET['id'];
    }
    $fetch_query="SELECT * FROM `category_simple` WHERE id='$id'";
    $result=mysql_query($fetch_query) or die("Incorrect Query Error");
    ?>
    <!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=iso-8859-1" />
    <title>Edit Category</title>
    </head>
    <body>
    <form method="post">
    <table border="2">
    <tr><th colspan="2">Update Category</th></tr>
    <?php
    while($row=mysql_fetch_array($result))
    {
    ?>
    <tr>
    <td>Category Name</td>
    <td><input type="text" name="category_name" value="<?php echo $row['name']; ?>" /></td>
    </tr>
    <tr>
    <td><input type="submit" name="save_category" value="Save Category" /></td>
    </tr>
    <?php
    }
    ?>
    </table>
    </form>
    </body>
    </html>
    <?php
    if(isset($_POST['save_category']))
    {
    $category_name=$_POST['category_name'];
    $date=date("Y-m-d H:i:s");
    $query="UPDATE `category_simple` SET name='$category_name',updated_date='$date' WHERE id='$id'";
    mysql_query($query) or die("Incorrect Query Error");
    header("Location: view_category.php");
    }
    ?>


    Step 4:
    "delete_category.php" is created in order to delete the category already added, if don't need it. The file contains the below given code.


    <?php
    include_once("db_connect.php");
    if(isset($_GET['id']))
    {
    $id=$_GET['id'];
    $query="DELETE FROM `category_simple` WHERE id='$id'";
    mysql_query($query) or die("Incorrect Query Error");
    header("Location: view_category.php");
    }
    ?>

    Step 5:
    "view_category.php" is created in order to check whether the add/edit/delete functionality is working properly or not. The file contains the below given code.


    <?php
    include_once("db_connect.php");
    ?>
    <!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=iso-8859-1" />
    <title>View Category Listing</title>
    </head>
    <body>
    <a href="add_category.php">Add Category</a><br /><br />
    <table border="2">
    <tr><th colspan="6">View Category Listing</th></tr>
    <tr><th>ID</th><th>Name</th><th>Created Date</th><th>Updated Date</th><th colspan="2">Actions</th></tr>
    <?php
    $query="SELECT * FROM `category_simple`";
    $result=mysql_query($query) or die("Incorrect Query Error");
    while($row=mysql_fetch_array($result))
    {
    ?>
    <tr><td>
    <?php echo $row['id']; ?>
    </td><td>
    <?php echo $row['name']; ?>
    </td><td>
    <?php echo $row['created_date']; ?>
    </td><td>
    <?php echo $row['updated_date']; ?>
    </td><td>
    <a href="edit_category.php?id=<?php echo $row['id']; ?>">Edit</a>
    </td><td>
    <a href="delete_category.php?id=<?php echo $row['id']; ?>">Delete</a>
    </td></tr>
    <?php
    }
    ?>
    </table>
    </body>
    </html>
    That's All. The work is finish just run the file "view_category.php" using it you can access to all other files easily during runtime.

    Reactions:

    0 comments:

    Post a Comment