Zend - The PHP Company




Databases

Add Code


K.Palani  

Type: class library
Added by: idiskp
Entered: 11/02/2005
Last modified: 02/12/2004
Rating: - (fewer than 3 votes)
Views: 5142
Name: K.Palani, email: ilikephp@yahoo.co.in It is a simple form wizard. The form contains all basic database functions and is styled like Visual Basic/FoxPro environment. The form is not using any external libraries. It is pure PHP and HTML. The code is very simple to understand and can be used by beginners and intermediate programmers. It has lot of scope for improvement. The beauty of the form is that it is a single form and it is not like clicking somewhere and viewing somewhere else. The form has navigation buttons like MoveFirst, Next, Previous, MoveLast, Search, Delete. Also, there are buttons for inserting and updating records. Primary key values can be searched using the search button. Values are fetched from table using the primary key value. It is ideal for creating table master forms and can be included in the project. Step 1: The user may copy these files including /lib sub folder in the place understood by PHP and Apache web server. Step 2: The user may copy the billunit.php and name it differently. Connection parameters may be set in 'lib/conn.php'. Step 3: The table name may be passed to the '$tablename' variable. Primary key field name may be passed to the '$pk' variable. Step 4: A table template may be created/modified suitably according to your table requirements like label for the field and text input. The sample 'billunit.php' contains 3 fields. Step 5: The names of the text input may be passed in the action buttons of 'Insert' and 'Update' as shown in the example. That is all. Save it and run from your browser. Comments/criticisms/suggestions are welcome. I am at ilikephp@yahoo.co.in .


*************************
billunit.php - sample form code
*************************

<html>
<title>
    BillUnit  <!-- window title may be suitably modified by the user -->
</title>
<head>
<center>
<h1>BillUnit Master</h1> <!-- heading may be suitably modified by the user -->
</center>
</head>

<body bgcolor='#9999ff'>
<?php

// for establishing connection.
include 'lib/conn.php'

$tablename='billunits'// actual tablename may be passed here.

$pk='billunit';  // name of the primary field may be passed here

include 'lib/buttonactions.php'

// for inserting records, user have to select fields manually. The list of text inputs may be created
// first in the table available inside the form and the respectve names of the text input may be
// passed here.

if ($_POST['insert']):
    
$billunit=$_POST['txtbu'];
    
$shopdetail=$_POST['txtsc'];
    
$shopcode=$_POST['txtsd'];
        
$addqrymysql_query("insert into $tablename values('$billunit', '$shopdetail', '$shopcode')");
        if (!
$addqry):
        echo 
"Could not successfully add the record ($addqry): " mysql_error();
    else:
        echo 
"'$addqry' Record added successfully!";
    endif;

// likewise for updating records, user have to select fields manually.    

elseif ($_POST['update']):
    
$billunit=$_POST['txtbu'];
    
$shopdetail=$_POST['txtsc'];
    
$shopcode=$_POST['txtsd'];
    
$bu=$_POST['tbu']; // $bu is temporary variable used by the programme and may be untouched.
        
$chgqrymysql_query("update $tablename set billunit='$billunit',
                         shopdetail='
$shopdetail',
                 shopcode='
$shopcode'
                 where 
$pk = '$bu'");
        if (!
$chgqry):
        echo 
"Could not successfully update the record ($chgqry): " mysql_error();
    else:
        echo 
"'$chgqry' Record updated successfully!";
    endif;
endif;
?>

<form action="<?=$_SERVER['PHP_SELF']?>" method="POST">
<br><br>

<?
if (!$_POST['add'] and !$_POST['insert'] and !$_POST['update'] ):
   
$row=mysql_fetch_assoc($result);
endif;
?>

<!-- this table consists of both labels and field values. user may select their own
 fields of their choice. Here $bu is temporary variable used by the programme
 internally and may not be touched by the user. The $bu variable may be assigned
 the value of primary key. The name of the 'text input' is to match when
 passing values in the above 'insert' and 'update' actions.  -->

    <table bgcolor="cyan" align="center" border="1">
    <tr>
    <td>BillUnit :</td>
    <td> <input type="text" name="txtbu" value="<?php echo $row["billunit"]; ?>" >
            <?  $bu=$row["billunit"]; ?> </td> </tr>
    <tr>
    <td><b>Shop Detail:</b></td>
    <td> <input type="text" name="txtsc" value="<?php echo $row["shopdetail"]; ?>" > </td>
    </tr>
    <tr>
    <td>Shop Code:</td>
    <td> <input type="text" name="txtsd" value="<?php echo $row["shopcode"]; ?>" > </td>
    </tr>
    </table>
<?
// table ends.

include 'lib/navibuttons.php';
?>
</form>
</body>
</html>

***********************

// The following php files may be created in sub
// folder by name /lib.

// buttonactions.php 
// -------------------

<?php
if ($_POST['first']):
    include 
'lib/first.php';
elseif (
$_POST['last']):
    include 
'lib/last.php';
elseif (
$_POST['search']):
    include 
'lib/search.php';
elseif (
$_POST['del']):
    include 
'lib/delete.php';
    include 
'lib/first.php';
elseif (
$_POST['next']):
    include 
'lib/next.php';
elseif (
$_POST['prev']):
    include 
'lib/prev.php';
elseif (
$_POST['add']):
    
mysql_query("select * from $tablename where $pk = '-1'");
elseif (
$_POST['chg']):
    
$bu=$_POST['tbu'];
    
$result=mysql_query("select * from $tablename where $pk= '$bu'");
elseif (
$_POST['cancel']):
    include 
'lib/first.php';
else:
    include 
'lib/first.php';
endif;
?>


// delete.php
// -------------

<?php
    $bu
=$_POST['tbu'];


    
$sql "delete
            FROM 
$tablename where $pk='$bu'" ;
    
$result mysql_query($sql);
    if (
$result):
            echo 
"Record Deleted";
     else:
        echo 
"Could not successfully run query ($sql) from DB: " mysql_error();
        exit;
    endif;

?>


// last.php
// --------

<?php
    $sql 
"SELECT *
            FROM 
$tablename order by $pk desc limit 0,1" ;
    
$result mysql_query($sql);
    if (!
$result) {
        echo 
"Could not successfully run query ($sql) from DB: " mysql_error();
        exit;
    }
    if (
mysql_num_rows($result) == 0) {
        echo 
"No rows found, nothing to print so am exiting";
        exit;
    }
?>

// next.php
// -----------


<?php
    $bu
=$_POST['tbu'];
    
$sql "SELECT *
            FROM 
$tablename where $pk > '$bu' order by $pk limit 1" ;
    
$result mysql_query($sql);
    if (!
$result) {
        echo 
"Could not successfully run query ($sql) from DB: " mysql_error();
        exit;
    }
    if (
mysql_num_rows($result) == 0) {
        include 
'last.php';
        echo 
"reached last record!";
     
//   echo "No rows found, nothing to print so am exiting";
     //   exit;
    
}
    
//echo $result;

?>


// search.php
// ------------ 

<?php
   $search
=$_POST['searchval'];
    
$sql "SELECT *
                FROM 
$tablename where $pk = '$search' limit 0,1" ;
    
$result mysql_query($sql);
    if (!
$result) {
        echo 
"Could not successfully run query ($sql) from DB: " mysql_error();
        exit;
    }
    if (
mysql_num_rows($result) == 0) {
        echo 
"No rows found, nothing to print so am exiting";
        exit;
    }
?>

// conn.php
// ----------

<?php
    $conn 
mysql_connect("servername""username""password");
    if (!
$conn) {
        echo 
"Unable to connect to DB: " mysql_error();
        exit;
    }
    if (!
mysql_select_db("database"$conn)) {
        echo 
"Unable to select mydbname: " mysql_error();
        exit;
    }
    
// include 'first.php';
?>


// first.php
// --------

<?php
    $sql 
"SELECT *
            FROM 
$tablename order by $pk limit 0,1" ;
    
$result mysql_query($sql);
    if (!
$result) {
        echo 
"Could not successfully run query ($sql) from DB: " mysql_error();
        exit;
    }
    if (
mysql_num_rows($result) == 0) {
        echo 
"No rows found, nothing to print so am exiting";
        exit;
    }
?>

// navibuttons.php
// ------------------

<html>
<br> <br>
<?php
if ($_POST['add']):
?>
    <table align="center"> <tr> <td><b>
    <? print "<input type="submit" name="insert" value="Insert">"?></b> </td>
   <td><b> <? print "<input type="submit" name="cancel" value="Cancel">"?></b>
   </td> </tr> </table>
<?
elseif ($_POST['chg']): ?>
        <input name="tbu" type="hidden" value="<? echo $bu?>"> 
    <table align="center"> <tr> <td><b>
    <? print "<input type="submit" name="update" value="Update"> "?></b> </td>
  <td><b> <? print "<input type="submit" name="cancel" value="Cancel">"?></b>
   </td> </tr> </table> <?
else:
?>
<table align="center">
<tr>
<td><b>    <input name="tbu" type="hidden" value="<? echo $bu?>"></b> </td>
<td><b>    <input type="submit" name="first" value=" |< " > </b></td>
<td><b>    <input type="submit" name="next" value=" >> "> </b></td>
<td><b>    <input type="submit" name="prev" value=" << "> </b></td>
<td><b>    <input type="submit" name="last" value=" >| "> </b></td>
<td><b>    <input type="submit" name="add" value="New"> </b></td>
<td><b>    <input type="submit" name="chg" value="Chg"> </b></td>
<td><b>    <input type="text" name="searchval" value=""> </b></td>
<td><b>    <input type="submit" name="search" value="Search"></b> </td>
<td><b>    <input type="submit" name="del" value="Del" ></b> </td>
</tr>
</table>
<?
endif;
?>
</html>

// prev.php
// ---------

<?php
    $bu
=$_POST['tbu'];
    
$bu=trim($bu);
    
$sql "SELECT *
            FROM 
$tablename where $pk < '$bu' order by $pk desc limit 1" ;
    
$result mysql_query($sql);
    if (!
$result) {
        echo 
"Could not successfully run query ($sql) from DB: " mysql_error();
        exit;
    }
    if (
mysql_num_rows($result) == 0) {
        include 
'first.php';
        echo 
"reached first record!";
      
//  echo "No rows found, nothing to print so am exiting";
      //  exit;
    
}
//echo $result;
?>


Usage Example


billunit.php


Rate This Script





Search



This Category All Categories