<?
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# #
# MySQLTable Class - The MySQL Form Generator #
# Tobie van der Spuy - 2001 #
# hide@address.com #
# #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
class MySQLTable {
var $db;
var $name;
var $ignore; // array of dbfield names that should be ignored
var $passwd; // array of dbfields that should be passworded
var $hide; // array of dbfields that should be hidden
var $action; // Action (1 for Insert, 2 for Edit)
var $where; // SQL Where Clause
var $order; // SQL Order Clause
var $limit; // SQL Limit Clause
var $fields; // array of fieldnames in table
var $sql; // sql query
function MySQLTable ($db,$name,$ignore,$passwd,$hide,$action,$where,$order,$limit) {
if ($hide == "") { $hide = array(); }
else {
if (strlen($hide) <= "1") { $hide = array($hide); }
else { $hide = explode(":",$hide); }
}
if ($ignore == "") { $ignore = array(); }
else {
if (strlen($ignore) <= "1") { $ignore = array($ignore); }
else { $ignore = explode(":",$ignore); }
}
if ($passwd == "") { $passwd = array(); }
else {
if (strlen($passwd) <= "1") { $passwd = array($passwd); }
else { $passwd = explode(":",$passwd); }
}
$this->name = $name;
$this->db = $db;
$this->sql = "show fields from " . $this->name;
$this->hide = $hide;
$this->action = $action;
if ($where != "") { $this->where = " where " . $where; }
else { $this->where = ""; }
if ($order != "") { $this->order = " order by " . $order; }
else { $this->order = ""; }
if ($limit != "") { $this->limit = " limit " . $limit; }
else { $this->limit = ""; }
$this->ignore = $ignore;
$this->passwd = $passwd;
$this->hide = $hide;
$this->fields = array();
$result0 = mysql_list_fields($db->name,$this->name);
$raw = $db->get($this->sql,"2");
if ($this->action == "2") {
$getval = "select * from " . $this->name . $this->where;
$raw2 = $db->get($getval,"1");
}
for ($i = 0 ; $i < sizeof($raw); $i++) {
$len = mysql_field_len($result0, $i);
$row = $raw[$i];
if (in_array($i,$this->passwd)) { $passwd = "1"; }
else { $passwd = "0"; }
if (in_array($i,$this->hide)) { $hidden = "1"; }
else { $hidden = "0"; }
if (in_array($i,$this->ignore)) { }
else {
if ($this->action == "2") { $value = $raw2[$i]; }
else { $value = ""; }
$field[id] = $this->name . "f" . $i;
array_push($this->fields,$field[id]);
$temp = $field[id];
$GLOBALS[$temp] = new MySQLField($field[id],$row[0],$row[1],$row[2],$row[3],$row[4],$row[5],$len,$passwd,$hidden,$value);
}
}
}
// Submit the fields to DB <
// -------------------------
function submit ($form) {
$db = $this->db;
$string = "";
$tempid = $GLOBALS[$form]->ids;
$tempid = array_flip($tempid);
$tempde = $GLOBALS[$form]->details;
// Start Setting Query
// -------------------
if ($this->action == "2") {
$strstart = "Update ";
$strtemp = " set ";
$bodyend = "";
$bodyend2 = "";
}
else {
$strstart = "Insert into ";
$strtemp = "";
$bodyend = "values (";
$bodyend2 = ")";
}
// Get Table Details and Add Query Body
// ------------------------------------
if ($this->action == "2") { $body = ""; $bodyend = "";}
else { $body = "("; $bodyend = ") values (";}
$ac0 = "0";
$ac1 = "0";
$ac2 = "0";
for ($i = 0; $i < sizeof($this->fields); $i++) {
$temp = $this->fields[$i];
$GLOBALS[$temp]->GetValue();
$fid = $GLOBALS[$temp]->ID;
$number = $tempid[$fid];
$defield = $tempde[$number];
$field = $GLOBALS[$temp]->name;
$value = $GLOBALS[$temp]->value;
if ($value != "") {
if ($this->action != "2") {
$body .= $this->add_body($field,$value,$ac0,"0");
$ac0++;
$bodyend .= $this->add_body($field,$value,$ac1,"1");
$ac1++;
}
else {
$body .= $this->add_body($field,$value,$ac2,"2");
$ac2++;
}
}
else {
if ($value == "") { $erval = "Nothing!"; }
else { $erval = $value; }
$GLOBALS[input_error] .= "Invalid <b>value</b> ($erval) for <b>field</b>: $defield!<br>\n"; }
}
$bodyend .= $bodyend2;
$body .= $bodyend;
// Compile Query End
// -----------------
if (($this->action != "") && ($this->where != "")) {
$this->where = " " . $this->where;
$stringend = $this->where;
}
else { $stringend = ""; }
// Make Query
// -----------------
$string .= $strstart;
$string .= $this->name;
$string .= $strtemp;
$string .= $body;
$string .= $stringend;
// Execute Query, return result if successful, return error if failed
// ------------------------------------------------------------------
if ($GLOBALS[input_error] == "") {
if ($db->put($string,"1")) {
return True;
}
else {
echo "Unknown Error!";
return False;
}
}
else {
echo $GLOBALS[input_error];
return False;
}
}
// Turn the field object's variable into a SQL update/insert query
// ---------------------------------------------------------------
function add_body($field,$value,$ac,$action) {
$out = "";
if ($ac != "0") { $out .= ","; }
else { $out .= ""; }
switch ($action) {
case 0:
$out .= "$field";
break;
case 1:
$out .= " \"$value\"";
break;
case 2:
$out .= " $field = \"$value\"";
break;
}
return $out;
}
}
?>