Client Management for a PHP Invoicing System

What’s an invoicing system that can’t manage the data for the clients you’re invoicing? This article, the third of four parts, shows how to make managing your clients easy. This part of the system allows you to view a full list of client names, and add, update or remove clients from your database.

It is up to you how much information about clients you want to keep. As far as this article is concerned, I will only keep the information that is needed to run an invoice system. The obvious information to store about clients is name, address, email, phone number and of course the date that you started working with the client. You are very limited in what you can do with client information, in fact the few things you can do are add a new client, and remove, update, and view client data. 

So let’s create a table that will reflect this information:

CREATE TABLE `client` (
  `id` int(4) unsigned zerofill NOT NULL auto_increment,
  `name` varchar(100) NOT NULL default ”,
  `address` text NOT NULL,
  `date_added` date NOT NULL default ‘0000-00-00′,
  `email` varchar(100) NOT NULL default ”,
  `contact_name` varchar(100) NOT NULL default ”,
  `phone_no` varchar(60) NOT NULL default ”,
  PRIMARY KEY  (`id`)
) TYPE=MyISAM AUTO_INCREMENT=12 ;

Simply copy and paste the above code into your MySQL clients’ SQL window and run the SQL. Once the table has been created and it’s all working, create a new PHP document and save it as “allclients.php.” This page will list all the clients in the database. At the very top of the page, add the following code:

Code7 allclients.php

<?
include “config.php”;
$query_clients = “SELECT * FROM client ORDER BY id DESC”;
$result_clients = mysql_query($query_clients);
$num_clients = mysql_num_rows($result_clients);
?>

The above code simply retrieves all the client data from the database. The retrieved number of rows is stored in the “$num_clients” variable.

{mospagebreak title=Building a table}

The next step is to build an HTML table that will display the following headers:

  • Client Name
  • Number of Invoices
  • Action

Here’s the HTML to build the table:

</tr>
            <tr class=”tblheadings”>
        <td><strong>Client Name</strong> </td>
            <td><strong>Number of Invoices</strong></td>
        <td><strong>Action</strong></td>
  </tr>

Next we built the dynamic side of the table according to the results from the query:

<?
                        if($num_clients > 0){
                        while($clients = mysql_fetch_assoc
($result_clients)){
                        ?>
        <tr class=”tblinfo”>
          <td><?=$clients['name'];?> </td>
                           <td><? $query_invoices = “SELECT *
FROM invoices WHERE cid = ‘”.$clients['id'].”‘ “; 
                           $qi=mysql_query($query_invoices);
                           $numqi=mysql_num_rows($qi);
                           if($numqi > 0){
                           echo $numqi;
                           }else{
                           echo “0”;                     
                           }?>  </td>
          <td><a href=”cdetails.php?cid=<?=$clients['id']?>”>View
Details </a> |<a href=”delclient.php?cid=<?=$clients['id']?
>”>Delete</a> <a href=”edClient.php?cid=<?=$clients['id']?
>”>Edit</a></td>
        </tr>
                        <? } 
                        }else{?>
                        <tr>
                        <td colspan=”2″><p>There are currently no
client details available.</p></td>
                        </tr>
                        <? }?>

The reason why we always list the contents of a particular table is because it gives us the means to delete and update the information. If you look at the Action column of the table you will see several actions that can be taken regarding a particular record. You can either delete, view or update a record. Let’s deal with all three actions and see how it’s done.

{mospagebreak title=Deleting clients and viewing their details}

Delete Client

Simply deletes a client from the database:

<?
include “config.php”;
if(isset($_GET['cid'])){

$query = “DELETE FROM client WHERE id = ‘”.$_GET['cid'].”‘”;
if(mysql_query($query))
{
header(“location:allclients.php”);

}else{
echo mysql_error();
}

}
?>

View Client Details

Shows the client information, this is done by sending a client id to the cdetails.php page:

<?
include “FCKeditor/fckeditor.php”;
include “config.php”;
if(isset($_GET['cid'])){
$query_clients = “SELECT * FROM client WHERE id = ‘”.$_GET
['cid'].”‘”;
$result_clients = mysql_query($query_clients);
$num_clients = mysql_num_rows($result_clients);
$clients = mysql_fetch_assoc($result_clients);
}
?>

Once the script receives the client id, it searches for that client and shows it in an HTML table:

<table width=”100%” border=”0″ cellspacing=”1″ class=”block”>
        <tr>
          <td colspan=”2″><table width=”100%” height=”19″
border=”0″>
            <tr class=”heading”>
              <td width=”22%”><a href=”Main.php” class=”link”>
[MENU]</a></td>
              <td width=”17%”> <a href=”allclients.php”>CLIENT
LISTINGS</a> </td>
              <td width=”24%”>USER:<?=$user;?></td>
              <td width=”37%”><a href=”logout.php”
class=”link”>LOGOUT</a></td>
            </tr>
          </table></td>
        </tr>
        <tr>
          <td width=”46%”><?=$clients['name'];?> </td>
          <td width=”54%”>Date Added:<?=$clients['date_added'];?> </td>
        </tr>
        <tr>
          <td><?php
$oFCKeditor = new FCKeditor(‘address’) ;
$oFCKeditor->BasePath = ‘FCKeditor/';
$oFCKeditor->Value = $clients['address'];
$oFCKeditor->Create() ;
?></td>
          <td><a href=”edClient.php?cid=<?=$clients['id']?
>”>Update Details </a></td>
        </tr>
      </table>

There is also a link to update the client information if you so wish.

{mospagebreak title=Updating client information}

To update client information we need to create a form that will display all of the client’s current information:

<form action=”edClient.php” method=”post” name=”updateclient”>
            <input name=”cid” type=”hidden” value=”<?=$_GET
['cid'];?>” />
            <input name=”dates” type=”hidden” value=”<?=$clients
['date_added'];?>” />
            <table width=”100%” border=”0″ cellspacing=”1″
class=”block”>
        <tr>
          <td colspan=”2″><table width=”100%” height=”19″
border=”0″>
            <tr class=”heading”>
              <td width=”22%”><a href=”Main.php” class=”link”>
[MENU]</a></td>
              <td width=”17%”> <a href=”allclients.php”>CLIENT
  LISTINGS</a> </td>
              <td width=”24%”>USER:<?=$user?></td>
              <td width=”37%”><a href=”logout.php”
class=”link”>LOGOUT</a></td>
            </tr>
          </table></td>
          </tr>                         

        <tr>
          <td width=”13%”>Client Name </td>
          <td width=”87%”><input name=”cname” type=”text”
id=”cname” size=”60″  value=”<?=$clients['name'];?>”/></td>
        </tr>
        <tr>
          <td>Address</td>
          <td>
                           <?php
$oFCKeditor = new FCKeditor(‘address’) ;
$oFCKeditor->BasePath = ‘FCKeditor/';
$oFCKeditor->Value = $clients['address'];
$oFCKeditor->Create() ;
?>
                          </td>
        </tr>
        <tr>
          <td>Contact Name </td>
          <td><input name=”contact” type=”text” id=”contact”
size=”60″  value=”<?=$clients['contact_name'];?>”/></td>
        </tr>
                        <tr>
          <td>Telephone Number </td>
          <td><input name=”phone” type=”text” id=”phone”
size=”60″  value=”<?=$clients['phone_no'];?>”/></td>
        </tr>
                        <tr>
          <td>Email Address </td>
          <td><input name=”email” type=”text” id=”email”
size=”60″  value=”<?=$clients['email'];?>”/></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td><input type=”submit” name=”submit” value=”Update
Client” /></td>
        </tr>                       

      </table>

As you can see from the code, the form itself is contained within a table and has several form elements in which the retrieved query results are placed. The code below shows how the records were retrieved:

<?
ob_start();
include “config.php”;
include “FCKeditor/fckeditor.php”;

A

if(isset($_GET['cid'])){
$query_clients = “SELECT * FROM client WHERE id = ‘”.$_GET
['cid'].”‘”;
$result_clients = mysql_query($query_clients);
$num_clients = mysql_num_rows($result_clients);
$clients = mysql_fetch_assoc($result_clients);
}

B

if(isset($_POST['submit'])){

$query_ins = “UPDATE client SET name='”.trim(addslashes($_POST
['cname'])).”‘,address='”.trim(addslashes($_POST
['address'])).”‘,”;
$query_ins .= “date_added = ‘”.trim(addslashes($_POST
['dates'])).”‘,email = ‘”.trim(addslashes($_POST['email'])).”‘,”;
$query_ins .= “contact_name = ‘”.trim(addslashes($_POST
['contact_name'])).”‘,phone_no = ‘”.trim(addslashes($_POST
['phone_no'])).”‘ WHERE id = ‘”.$_POST['cid'].”‘”;
if(mysql_query($query_ins)){
header(“location:allclients.php”);
}else{
echo mysql_error();
}

}
?>

The code has two parts. First it retrieves client information based on the client id that is sent from the allclients page:

if(isset($_GET['cid'])){
$query_clients = “SELECT * FROM client WHERE id = ‘”.$_GET
['cid'].”‘”;
$result_clients = mysql_query($query_clients);
$num_clients = mysql_num_rows($result_clients);
$clients = mysql_fetch_assoc($result_clients);
}

Then it updates the record when the form is submitted:

$query_ins = “UPDATE client SET name='”.trim(addslashes($_POST
['cname'])).”‘,address='”.trim(addslashes($_POST
['address'])).”‘,”;
$query_ins .= “date_added = ‘”.trim(addslashes($_POST
['dates'])).”‘,email = ‘”.trim(addslashes($_POST['email'])).”‘,”;
$query_ins .= “contact_name = ‘”.trim(addslashes($_POST
['contact_name'])).”‘,phone_no = ‘”.trim(addslashes($_POST
['phone_no'])).”‘ WHERE id = ‘”.$_POST['cid'].”‘”;
if(mysql_query($query_ins)){
header(“location:allclients.php”);
}else{
echo mysql_error();
}

Once the update has been completed, the user is sent back to the allclients page. Below is a screen shot of the allclients page:

{mospagebreak title=Adding a new client}

To add a new client, I created a form that takes the following information:

  • Client name
  • Address
  • Email
  • Phone number
  • Contact Name

Below is a screen shot of what the form looks like.

Here’s the HTML for the form:

<table width=”100%” border=”0″ cellspacing=”1″ class=”block”>
             <tr>
             <td>
             <table width=”100%” height=”19″ border=”0″>
            <tr class=”heading”>
              <td><a href=”Main.php” class=”link”>[MENU]</a></td>
              <td>USER:</td>
              <td><a href=”logout.php”
class=”link”>LOGOUT</a></td>
            </tr>
          </table>             

             </td>
             </tr>
        <tr>
          <td height=”34″ colspan=”2″>
            <form  name=”newc” action=”NewClient.php”
method=”post”>
<table width=”100%” border=”0″>
  <tr>
    <td>&nbsp;</td>
    <td><? if(isset($msg)){ echo $msg;}?></td>
  </tr>
  <tr>
    <td width=”122″><span class=”style1″> Company Name
</span></td>
    <td width=”426″><input name=”name” type=”text” id=”name”
size=”90″></td>
  </tr>
  <tr>
  <td>Address</td>
  <td><?php
$sBasePath = ‘FCKeditor/’ ;
$oFCKeditor = new FCKeditor(‘address’) ;
$oFCKeditor->BasePath         = $sBasePath ;
//$oFCKeditor->Value             = ‘This is some <strong>sample
text</strong>. You are using <a
href=”http://www.fckeditor.net/”>FCKeditor</a>.’ ;
$oFCKeditor->Create() ;
?></td>
  </tr>
  <tr>
    <td>Email</td>
    <td><input name=”email” type=”text” size=”90″></td>
  </tr>
  <tr>
    <td>Phone</td>
    <td><input name=”phone” type=”text” size=”90″></td>
  </tr>
  <tr>
    <td>Contact name </td>
    <td><input name=”contact_name” type=”text” id=”contact_name”
size=”90″></td>
  </tr>
  <tr>
    <td colspan=”2″><div align=”center” class=”style1″>
      <input name=”submit” type=”submit” id=”submit”
value=”Submit New Client”>
    </div></td>
  </tr>
</table>
</form>
</td>
</tr>

</table>

Once the form is submitted, the following code handles the form data:

<? include “FCKeditor/fckeditor.php”;
include “config.php”;
if(isset($_POST['submit'])){
$query_ins = “INSERT INTO client SET name='”.trim(addslashes
($_POST['name'])).”‘,address='”.trim(addslashes($_POST
['address'])).”‘,”;
$query_ins .= “date_added = ‘”.$td.”‘,email = ‘”.trim(addslashes
($_POST['email'])).”‘,”;
$query_ins .= “contact_name = ‘”.trim(addslashes($_POST
['contact_name'])).”‘,phone_no = ‘”.trim(addslashes($_POST
['phone'])).”‘”;
if(mysql_query($query_ins)){
header(“location:allclients.php”);
}else{
echo mysql_error();
}
}
?>

All that the code above does is match the form data to the table fields and send it to the database, after which the user is sent back to the allclients page.

Conclusion

Client management is the third in the series on creating a invoicing system. The system makes managing your clients easy by allowing you to view a full list of client names and also allowing you to update or remove clients from your database. 

[gp-comments width="770" linklove="off" ]

antalya escort bayan antalya escort bayan Antalya escort diyarbakir escort