PHP Classes

File: examples/example-insert-update.php

Recommend this page to a friend!
  Classes of Barton Phillips  >  PHP Site Class  >  examples/example-insert-update.php  >  Download  
File: examples/example-insert-update.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHP Site Class
Create database driven Web sites
Author: By
Last change:
Date: 5 years ago
Size: 4,067 bytes


Class file image Download
// example-insert-update.php

$_site = require_once(getenv("SITELOAD") . "/siteload.php");


$S = new $_site->className($_site);

// FORM POST 'insert' into database

if($_POST['page'] == "post") {
$id = $_POST['id'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];

$id)) {
$sql = "insert into $S->memberTable (rowid, fname, lname) values(null,'$fname', '$lname')";
  } else {
$sql = "update $S->memberTable set fname='$fname', lname='$lname' where rowid=$id";

// GET 'update'

if($_GET['page'] == "update") {
$id = $_GET['id'];
$S->query("select fname, lname from $S->memberTable where rowid=$id");
$fname, $lname) = $S->fetchrow('num');

$h->title = "update";
$h->banner = "<h1>Update Record</h1>";
$top, $footer) = $S->getPageTopBottom($h);

  echo <<<EOF
<form method="post">
First Name: <input type="text" name="fname" value="
Last Name : <input type="text" name="lname" value="
<input type="submit" value="Submit">
<input type="hidden" name="page" value="post">
<input type="hidden" name="id" value="


$T = new dbTables($S);

// Pass some info to getPageTopBottom method
$h->title = "Insert Update"; // Goes in the <title></title>
$h->banner = "<h1>Insert Update Test</h1>"; // becomes the <header> section
// Add some local css to but a border and padding on the table
$h->css = <<<EOF
#tbl * {
  padding: .5em;
  border: 1px solid black;
.odd {
  color: white;
  background-color: red;
.oddtr {
  background-color: green;

$top, $footer) = $S->getPageTopBottom($h);

// create a table from the memberTable

$inc = 0;

// The maketable() callback function gets the current row and the row description.
// This table has two fields in each row, 'First Name' and 'Last Name'.
// The description looks like "<tr><td>First Name</td><td>Last Name</td></tr>"
// for the first row. The $desc still has the field keys not the final values.
// This callback uses a counter to determin which rows are odd. We take the value of the
// first field in the row ($row['First Name'] and add a span around it.
// We then change the <tr> into <tr class='oddtr'> in the description.
// Note the arguments are passed by reference rather than value so we can modify the results.

function callback(&$row, &$desc) {

$inc++ % 2)) {
$row['First Name'] = "<span class='odd'>".$row['First Name']."</span>";
$desc = preg_replace('~<tr>~', "<tr class='oddtr'>", $desc);
$row['ID'] = "<a href='example-insert-update.php?page=update&id=".$row['ID']."'>".$row['ID']."</a>";

// We use the 'as' to give our column headers nice names otherwise they would be
// 'fname' and 'lname'.

$sql = "select rowid as ID, fname as 'First Name', lname as 'Last Name' from $S->memberTable";

// The second argument to the maketable method is an array with the following properties:
// 'callback', 'callback2', 'footer'] 'attr'.
// 'attr' is an assoc array that can has attributes for the <table> tag,
// like 'id', 'title', 'class', 'style' etc.
// 'callback2' has the final row with the keys replaced by the column values. 'callback2'
// just has the &$desc argument.
// 'footer' is a footer string

$f = "<tfoot><tr><th colspan='2'>Footer goes here</th></tr><tfoot>";
$tbl) = $T->maketable($sql, array(

echo <<<EOF
<h2>Enter a first and last name to insert</h2>
<form method="post">
<input type="text" name="fname" placeholder="first name" autofocus>
<input type="text" name='lname' placeholder="last name"><br>
<input type="submit" value="Submit">
<input type="hidden" name="page" value="post">

For more information send a message to info at phpclasses dot org.