Databases
|
|
|
|
*************************
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'];
$addqry= mysql_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.
$chgqry= mysql_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
|
|
|
Rate This Script
|
|
|
|