acd.php - Generic MySQL Add/Change/Delete

See CRUD

Wow, even this HTML page looks ancient.

This code started out as an ad hoc mechanism to quickly have Add/Change/Delete functionality for MySQL tables. However, I have come to rely on it. After using it for more than fifteen years, I decided it needed to be made more featureful and robust. And hey, object-oriented.

        $HumanObjectNoun = "Car";
        $table = "cars";
        $fieldlist = "invnum,make,model,year,color,certified,type,mileage,price,imagefile";
        $idfield = "id";
        $relation["color"] = "colors,id,name";          // table,join,display

        include "/usr/local/www/carlot/include/db-admin.php";

// I need to add a lot of functionality.  First, I need to make this
// included, so I can update it without fussing with each file each time.
// I should probably stop mucking with the title/header and the
// click-here-for-home link.  Other features needed:
//
//      * customized display headers
//      * checkboxes, radio buttons
//      * optional Add, Edit, and Delete images
//      * a subset criteria, either fixed or passed along in the URL
//      * include file for special field handling
//      * join lookups in the list query
//

Table: $table

Use the $table variable to specify the name of the table being managed.

	$table = "cars";

Fields: $fieldlist

The $fieldlist variable is used to specify a list of the fields that will be displayed or edited by this module. This list is comma seperated, with no spaces.

        $fieldlist = "invnum,make,model,year,color,certified,type,mileage,price,imagefile";

Table Key: $idfield

Use this variable to specify the primary key for the table. This will be used to uniquely identify records. A table must have a primary key to be used with this program.

	$idfield = "id";

Description of Record: $HumanObjectNoun

	$HumanObjectNoun = "Car";

Relations with SELECT boxes: $relation

The $relation array specifies a relationship to another table, so that values can be selected from that other table. For instance, if I have a table of cars and a table of colors, I want to select the color_id from the color table, but I would like to display the name rather than the number of the color.

The variable is set as a string of three values, comma seperated, with no spaces. The first value is the name of the other table. The second value is the field whose value will be stored in the main table. The third value is the field which will be displayed to the user.

        $relation["color"] = "colors,id,name";

The example above will relate the color column in the main table to the id column in the colors table. The user will see the value from the name column.

Grouping: $groupfield

Sometimes a table as multiple records in "groups". Think of the SQL "GROUP BY" clause. For example, I have a table of shipping charges, based on the shipping method and various price ranges for the items being sold. I want to let the user select the shipping method first, then select among only those records in the table which are assocaited with that shipping method.

Enter the $groupfield variable. Set this to the name of the field you want to group by. It works okay with the $relation and $fldhdr variables.

	$groupfield = "shipmeth_id";

User-Defined Actions: $action

Okay, I want user-defined actions. They will appear in the record list. Specify a URL with $action. The index is a description that will be used for the link.

The URL will have the record id appended to it, as a GET using the field name "id" (ie: "script.php?id=73").

	$action["Upload"] = "/admin/image-upload.php";

Example

<HTML>
<HEAD><TITLE>Add/Change/Delete Cars</TITLE><HEAD>
<BODY>
<CENTER>
<H1>Add/Change/Delete Cars</H1>

<?
	// Connect to Database
        include "$DOCUMENT_ROOT/include/db-admin.php";

	// Settings for Add/Change/Delete Module (acd.php)
        $HumanObjectNoun = "Car";
        $table = "cars";
        $fieldlist = "invnum,make,model,year,color,certified,type,mileage,price,imagefile";
        $idfield = "id";
        $relation["make"] = "makes,id,name";
        $relation["model"] = "models,id,name";
        $relation["year"] = "years,id,year";
        $relation["color"] = "colors,id,name";
        $relation["type"] = "types,id,description";

	// Call Add/Change/Delete Module (acd.php)
        include "$DOCUMENT_ROOT/include/acd.php";
?>
<P>
Back to <A HREF=/admins>Admins Menu</A>
</CENTER>

Features to Add

Graphical Buttons: allow the specification of Add, Edit, Delete, and Save buttons.

Better Data Types: A single array (like $datatype) which specifies the datatype of fields (the default being a normal input field). Textarea, two types of boolean (N/N or 0/1), etc.

$fldhdr on Edit Page

Sort Order for $relation: It would be nice of the items in the drop-down box on relations was sorted.