Simple address book with php and mysql using pdo

by  
Share:
Simple address book with php and mysql using pdo

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

Get the latest updates directly in your inbox for FREE: Subscribe here

Share your feedback / let me know your doubts regarding this tutorial in the comment box given below.

Share:
Hasan

Shahrukh Khan (Hasan)

A software engineer who's a die-hard coder, blogger, dreamer and mentor with years of expertise in web development. Know more...

Related Posts

Tanjina on

Helpful

Reply
elsgraph on

awesome, really help me.

Reply
Shahrukh Khan on

Thanks

Reply
Marina on

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

Reply
Shahrukh Khan on

I will try. thanks for ur suggestions.

Reply
zenel on

beutiful like a charm

Reply
Shahrukh Khan on

Thank You.

Reply
jam on

how do you run a simple crud in Linux CentOS? I want to run this and i’m using CentOS. What database do i need to install?

Reply
Bram on

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

Reply
Shahrukh Khan on

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.

Reply
Bram on

Thanks it works

silva_web on

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

Reply
Shahrukh Khan on

I think you should go to bootstrap official website to get to know abt it better http://getbootstrap.com/

Reply
Bram on

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

Reply
Shahrukh Khan on

Add a php condition clause in the checkbox
<input type=”checkbox” name=”month_jan” value=”1″ />

Reply
Michael on

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

Reply
Shahrukh Khan on

check your session conditions.

Reply
Manas on

This is a great article.
Can you please give me the link where I can download the script of this article.

Reply
Shahrukh Khan on

please click the social button it give you download link.

Reply
piya on

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

Reply
Shahrukh Khan on

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

Reply
Aditya on

you are great bhai

Reply
john on

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

Reply
kieran on

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.

Reply
kirtan on

is this address book available for code igniter?

Reply
Shahrukh Khan on

no this is made with core php, using mysql database and with bootstrap library framework.

Reply
kirtan on

if i want to convert it for code igniter how can i do it ?

Shahrukh Khan on

sorry I dont work in Codeignitor. But it must be that tough. You must put all the UI ( bootstrap files) in view folder and make a model and controller for managing pages and database interaction.

gautam on

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…

Reply
Shahrukh Khan on

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.

Reply
Gautam on

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 ….

Shahrukh Khan on

I use ob_start() mostly so to suppress warning like header already sent and similar errors.

Freddie on

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

Reply
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.

Reply
Shahrukh Khan on

the tutorial is already available for download, what help do you need other than that.

Reply
manudelosreyes on

Hello!!!

Thank you for this!! It’s perfect for me!! But I have a question:

How I can change the number of results page? For example showing 20

Thank you!!!!

Reply
Shahrukh Khan on

Change the page limit on the index page

Reply
Carl Dekker on

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.

Reply
Carl Dekker on

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.

Reply
zac on

I would like to add pdf extension for profile pics, but cannot find it in the code

Reply
Dave on

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.

Reply
produsen kaos muslimah on

Right here is the right webpage for everyone who wishes to understand
this topic. You realize a whole lot its almost hard to argue
with you (not that I really will need to…HaHa).
You certainly put a fresh spin on a topic which has been discussed
for many years. Excellent stuff, just excellent!

Reply
Shahrukh Khan on

Thanks for your appreciation.

Reply
Daniel on

Hello,

a very nice tutorial! I have a question. How can i delete also the image from the folder when i push the button delete of a row? I tried with unlink but it does not work…
Thank you for your help!
Kind greetings from Austria!

Reply
Shahrukh Khan on

unlink work great. make sure the path is correct and you have permission to delete

Reply
Ali Islam on

Download is not working. Please mail me link for download.

Reply
Shahrukh Khan on

Download is working fine. It is at the given at the end of the tutorial.

Reply
Samual on

This website was… how do you say it? Relevant!!

Finally I have found something which helpedd me. Thanks a lot!

Reply
Shahrukh Khan on

Thanks

Reply
Christian on

After setup I try to run index in my browser and it redirects me to myphp local host dashboard. My personal projects work fine. Is the something I need to check?

Reply
Shahrukh Khan on

check the config file. and update it all on one folder

Reply
Warlins on

Do you have the tutorial showing step by step?

Thank you very much for your help.

Reply
Shahrukh Khan on

i will try for sure.

Reply
Bruce R Norman on

Very helpful – I was well into creating my own address book for local community and learning PDO when I found this.

Thanks!

Reply
Jeff on

Thanks for sharing!
I added job title, organization and notes columns to the database and edited the files accordingly. It all works just fine except I can’t get the search function to return any results from the new columns. Also, if the last name contains two names, I get no search results from it.

Below is the only line that I changed for the search query:
$sql = “SELECT * FROM tbl_contacts WHERE 1 AND ”
. ” (first_name LIKE :keyword OR last_name LIKE :keyword OR last_name LIKE :keyword OR job_title LIKE :keyword OR organization LIKE :keyword OR address LIKE :keyword OR notes LIKE :keyword) ORDER BY first_name “;

Can you help me fix the search?

Reply
Shahrukh Khan on

try different :keyword names for placeholder and check

Reply
Christian on

Good evening,
I have to create a module in PHP linked to a mysql database
This module is similar to user management and therefore to an address book.
I would have to add an authentication to protect its access.
This module will be used to manage the members of an association (voluntary non-profit). As a beginner, could you give me the right to modify your code to adapt it to my needs?
Christian

Reply
Shahrukh Khan on

Hi.

if its a NON-PROFIT you can use it as per your needs, but a backlink (credit) to my site will be appreciated.

Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.