Simple address book with php and mysql using pdo

45

PDO or PHP Data Objects is a very powerful yet lightweight API for accessing database in MySQL. If you are using PHP 5.1.0 or above it comes pre-installed. So if you are one of the guys still using the old mysql_query and related functions, this is the right time to upgrade. Using this mini project you can learn how to create a simple address book with PHP and MySQL using PDO classes.

View Demo

Why should you use pdo?

Well, most of my friends asked me why should we use PDO as the older one (mysql_query and all) are far more easier to use and does the job perfectly. Yes, I do agree with their statement that the older one does the job perfectly but let’s get to a situation where you have an application running in php and mysql and after some time say 5 years, you want to change your database to Oracle or MS-SQL Server or any other. “Are you kidding me???” will say your old API, since you have coded specifically for mysql, so you have to change you codes all over and that will hurt your ar** badly.

To help you avoid these situations comes PDO. All you have to do is change the database driver and some queries and you are done. PDO has prepared statements which escapes user input variables that to most extent prevents sql injection. Since I love Object Oriented programming from the days I used to code in Java. This is what every object-oriented programming lover want.

So don’t just sit and think too much, from my experience I can personally gaurantee you once you learn PDO you will never think coding with the older API ever. You can find some more awesome tutorials fom PHP.net and tutplus for very basic stuffs. If you are a novice and starting php for the first time then I would strongly recommend you to learn php basics first. You can get some good basic tutorials in PHP for beginners section.

Just to let you know that the old mysql_query and related function are being deprecated, so you have other way now either learn PDO or mysqli (Mysql improved).

What are the features available in this address book project?

I have tried to make it as simple as possible yet covering all the basic features in simple address book.

  • Made with Bootstrap. Design is responsive to devices like smart phones, tablets etc.
  • Create, Update, View, Delete contacts (CRUD)
  • Jquery validations for input fields.
  • UTF-8 encoding used, so you can store contacts in different languages (multilingual).
  • User can search for contacts (filtering).
  • Pagination for viewing large contacts.

View Demo
Download

Share.
 

About Author

Entrepreneur & Dreamer

I am a passionate Software Professional, love to learn and share my knowledge with others.

Software is the hardware of my life.

45 Comments

  1. This is awesome!
    It would be great if you could give us step by step tutorial for this address book!
    Greetings from Croatia!

  2. Hi thanks for the great article.
    I want to create a filter with active and inactive contacts
    i am a beginner. can you please help me with this.
    i want a dropdown box beside de search box with Yes or No
    Thanks in advance

    • To achieve that filter query. you first add a field to the table say ‘status’ and the type can be enum status enum(‘yes’, ‘no’) NOT NULL DEFAULT ‘yes’;
      after that set a dropdown with two options yes and no. when ever user select a option and search say yes/no then using that query parameter of the the dropdown check add a similar coding logic similar to the search, but dont use LIKE %% which is used there use query like status = ‘yes/no’ whatever the value is. Hope that helps you solve the problem.

  3. Hi..this is a great work am new to pdo and not very good in php..i downloaded the file and when i open it i saw a bootstrap folder in it which i dont know what its stands for and the purpose.. and if i delete it hope it wont affect the codes from working..i really wonna know the meaning of the bootstrap because i always come across it in all files i download

  4. I have another question:
    I have checkboxes i can save the value to the database “1” or “0” Checked or not checked

    When is load the page the checkbox is not checked but the database value is set to 1
    how can i read de value from the database

    i have this: <input type="checkbox" name="month_jan" value="1" />

    But this is not working
    Thanks

    • Add a php condition clause in the checkbox
      <input type=”checkbox” name=”month_jan” value=”1″ < ?php echo ($yourvalue == 1) ? 'checked' : ''; ?> />

  5. Hi, I tried to use this as an example and use it with your multiple user role example. I can add an administrator but cannot view or delete an administrator. This is what I have in the process_form.php section. Please help:
    prepare($sql);

    // bind the values
    $stmt->bindValue(":uname", $u_username);
    $stmt->bindValue(":password", $u_password);
    $stmt->bindValue(":urole", $u_rolecode);

    // execute Query
    $stmt->execute();
    $result = $stmt->rowCount();
    if ($result > 0) {
    $_SESSION["errorType"] = "success";
    $_SESSION["errorMsg"] = "Administrator added successfully.";
    } else {
    $_SESSION["errorType"] = "danger";
    $_SESSION["errorMsg"] = "Failed to add Administrator.";
    }
    } catch (Exception $ex) {

    $_SESSION["errorType"] = "danger";
    $_SESSION["errorMsg"] = $ex->getMessage();
    }
    }

    header("location:admin.php");

    } elseif ( $mode == "update_old" ) {

    $u_username = trim($_POST['u_username']);
    $u_password = trim($_POST['u_password']);
    $u_rolecode = trim($_POST['u_rolecode']);
    $cid = trim($_POST['cid']);
    $error = FALSE;

    if (!$error) {
    $sql = "UPDATE
    system_users SET u_username = :uname, u_password = :password, u_rolecode = :urole, " . " WHERE u_userid = :cid ";

    try {
    $stmt = $DB->prepare($sql);

    // bind the values
    $stmt->bindValue(":uname", $u_username);
    $stmt->bindValue(":password", $u_password);
    $stmt->bindValue(":urole", $u_rolecode);
    $stmt->bindValue(":cid", $cid);

    // execute Query
    $stmt->execute();
    $result = $stmt->rowCount();
    if ($result > 0) {
    $_SESSION["errorType"] = "success";
    $_SESSION["errorMsg"] = "Administrator updated successfully.";
    } else {
    $_SESSION["errorType"] = "info";
    $_SESSION["errorMsg"] = "No changes made to Administrator.";
    }
    } catch (Exception $ex) {

    $_SESSION["errorType"] = "danger";
    $_SESSION["errorMsg"] = $ex->getMessage();
    }
    }

    header("location:admin.php?pagenum=".$_POST['pagenum']);

    } elseif ( $mode == "delete" ) {
    $cid = intval($_GET['cid']);

    $sql = "DELETE FROM system_users WHERE u_userid = :cid";
    try {

    $stmt = $DB->prepare($sql);
    $stmt->bindValue(":cid", $cid);

    $stmt->execute();
    $res = $stmt->rowCount();
    if ($res > 0) {
    $_SESSION["errorType"] = "success";
    $_SESSION["errorMsg"] = "Administrator deleted successfully.";
    } else {
    $_SESSION["errorType"] = "info";
    $_SESSION["errorMsg"] = "Failed to delete Administrator.";
    }

    } catch (Exception $ex) {
    $_SESSION["errorType"] = "danger";
    $_SESSION["errorMsg"] = $ex->getMessage();
    }

    header("location:admin.php?pagenum=".$_GET['pagenum']);
    }
    ?>

    Thanks

  6. hii ..this is very useful…
    bt i want to ask that…which steps i have to follow ..if i wanted to create the contact mgmt system..means firstly i hv to create a database or bootstrap design..m confused

    • Hi.
      first of all you document all your requirement in a word file and make use cases / E-R relationship diagrams. Also create a list of pages that will be created for that. Next step is using your E-R diagram create a database. Next step using your use case ( work flow ) create a static website and link them together. Once all this is done then CODE and TEST. Hope this answers your question. Best of Luck

  7. hey there nice project thanks
    plz sove my problem i want to insert 3 images in on row in datatable and want to insert into mysql how to do i m getting error thanks

  8. Hi great address book and just what I need, how would I go about adding a staff member who could add address’s but not edit or delete them. Thanks.

  9. hello sir

    great article for beginners like me , thansk for this , but i am confuse in mysql query following

    SELECT * FROM tbl_contacts WHERE 1 AND contact_id = 1
    SELECT * FROM tbl_contacts WHERE contact_id = 1

    whats the difference in both query, they returns same record, so whats the difference…

    • Hello Gautam.
      Its a very good question and I have been asked about this specific difference a lot. So answering your question “there is no difference”.
      WHERE 1 ( 1 is true) so giving 1 or not doesnt really do anything. its just my way of coding sql. But using where 1 comes in handy in situation where you have to add lots of additional filters in the where clause dynamically so I append 1 after WHERE. it helps me a lot on making complex queries but for simple queries you can exclude 1.

      • hello sir,

        thanks for the kind reply,

        and there’s one more confusion in my mind and that is …..
        in most of your tutorial you start “ob_start()” function output_buffering at the top of “config.php” file but why don’t you end output_buffering, means why don’t u use “ob_flush()” and “ob_end_flush()”, “ob_end_clean()” functions,

        and why we need to use these functions …. ,

        hope to hear again ….

  10. Nice Tuts but I have an auto pagination feature on a layout I am working on I don’t seem to have success on editing the code. Could You Point out or just give me a hint on which part is the PAGINATE feature

  11. Olamilekan oshodi on

    Hello my friend, have been following your work since. Now am hoping you can help.

    Can you show me how super admin can assign a task or job to admin or users.

    Example, super admin: assisgn task, manage task, view task and delete task
    admin/users: view task, assisgn task

    This can be done using the CRUD, but i just don’t know how to implement the assigning to admin/users

    please help.

  12. Hi,

    I was looking for a simple adressbook and this one look very good.
    A simple but great script.
    The download function is blocked.
    Can you send me the script instead..?
    Unlocking by the social media still doesn’t work.
    The download link keeps on locked position.

    Thanks

    Carl.

  13. Thanks for the download.
    Installing was easy and the script works fine.

    Two things are changed:
    1. The search function is now searching from the LAST NAME.
    2. An extra field is summited called: NOTE, for adding personal info.

    Carl.

  14. I’m looking for something like this for my family. What I would need is for each family member to be able to login and setup their own contact details and change them when needed. I wouldn’t want all members of the family to be able to see or edit other family members. Would this work for this job?

    Ideally I would like all members to be able to see the names of everyone and be able to choose the ones who can see their contact details from the member list.

Leave A Reply

CommentLuv badge

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.