一个SQL管理员的web接口
程序员文章站
2022-06-15 17:02:07
/**************************************...
<?
/*************************************************************************************
* sqladmin v2.0 - an sql administration user interface for the web *
* copyright (c) 1997-98 alessandro vernet <avernet@scdi.org> *
*************************************************************************************
* this library is free software; you can redistribute it and/or *
* modify it under the terms of the gnu library general public *
* license as published by the free software foundation; either *
* version 2 of the license, or (at your option) any later version. *
* *
* this library is distributed in the hope that it will be useful, *
* but without any warranty; without even the implied warranty of *
* merchantability or fitness for a particular purpose. see the gnu *
* library general public license for more details. *
* *
* you should have received a copy of the gnu library general public *
* license along with this library; if not, write to the *
* free software foundation, inc., 59 temple place - suite 330, *
* boston, ma 02111-1307, usa. *
*************************************************************************************/
/* todo:
* - add sort order.
* - add simple view.
* - add some documentation.
*/
/* limitations:
* - works only with msql.
*/
/* history:
* - 97-11-05 (avernet) corrected a bug with quote.
* - 98-01-01 (avernet) added a sortcolumn parameter to
* administrationtable function.
* - 98-03-14 (avernet) added function addtable to enable users to
* add (but not modify) en entry to the database.
* - 98-05-19 (avernet) submitted to px.
* - 98-10-11 (avernet) now sqladmin works with php3. the php2 version
* will not be mainteained anymore.
* - 98-10-11 (avernet) sqladmin is now distributed under the lgpl
* instead of mpl.
*/
function escapeforhtml ($string)
{
$result = $string;
//$result = ereg_replace ("\"", """, $result);
$result = ereg_replace ("<", "<", $result);
$result = ereg_replace (">", ">", $result);
return $result;
}
function displaytuple ($fieldsnumber, $fieldnames,
$fieldlengths, $values, $mode)
{
$result = "";
$result .= "<form method=\"post\"><table border><tr>" .
"<td bgcolor=\"#ccccff\">";
$result .= "<table cellspacing=\"0\" cellpadding=\"0\">";
$fieldindex = 0;
while ($fieldindex < $fieldsnumber)
{
$result .= "<tr><td>" . $fieldnames [$fieldindex] . "</td><td>";
if ($fieldlengths [$fieldindex] <= 128)
{
$result .= "<input type=\"text\" name=\"" .
$fieldnames [$fieldindex] . "\" value=\"" .
$values [$fieldindex] . "\" size=\"64\">";
}
else
{
$result .= "<textarea name=\"" .
$fieldnames [$fieldindex] . "\"" .
" cols=\"64\" rows=\"10\" wrap=\"virtual\">" .
escapeforhtml ($values [$fieldindex]) . "</textarea>";
}
$result .= "<input type=\"hidden\" name=\"old-" .
$fieldnames [$fieldindex] .
"\" value=\"" . escapeforhtml ($values [$fieldindex]) . "\">" .
"</td></tr>";
$fieldindex++;
}
$result .= "<tr><td align=\"center\" colspan=\"2\">";
if ($mode == "modify")
{
$result .= "<input type=\"submit\" name=\"remove\" value=\"remove\">";
$result .= "<input type=\"submit\" name=\"update\" value=\"update\">";
}
else
{ $result .= "<input type=\"submit\" name=\"add\" value=\"add\">"; }
$result .= "</table></td></tr></table></form>";
return $result;
}
function fieldfromtype ($text, $type)
{
if ($type == "int" || $type == "uint" || $type == "real")
{ $result = $text; }
else
{ $result = "'" . addslashes ($text) . "'"; }
return $result;
}
function executemsql ($database, $command)
{
/*echo "<tt>" . $command . "</tt><hr>";*/
msql ($database, $command);
}
function handleremove ($database, $table, $fieldsnumber,
$fieldnames, $fieldlengths, $fieldtypes)
{
global $remove;
if ($remove != "")
{
$command = "delete from " . $table . " where ";
$fieldindex = 0;
while ($fieldindex < $fieldsnumber)
{
$fieldname = "old-" . $fieldnames [$fieldindex];
global $$fieldname;
$command .= $fieldnames [$fieldindex] . "=" .
fieldfromtype ($$fieldname, $fieldtypes [$fieldindex]);
if ($fieldindex != $fieldsnumber - 1)
{ $command .= " and "; }
$fieldindex++;
}
executemsql ($database, $command);
}
}
function handleupdate ($database, $table, $fieldsnumber,
$fieldnames, $fieldlengths, $fieldtypes)
{
global $update;
if ($update != "")
{
$command = "update " . $table . " set ";
$fieldindex = 0;
while ($fieldindex < $fieldsnumber)
{
$fieldname = $fieldnames [$fieldindex];
global $$fieldname;
$command .= $fieldname . "=" .
fieldfromtype ($$fieldname, $fieldtypes [$fieldindex]);
if ($fieldindex != $fieldsnumber - 1)
{ $command .= ", "; }
$fieldindex++;
}
$command .= " where ";
$fieldindex = 0;
while ($fieldindex < $fieldsnumber)
{
$fieldname = "old-" . $fieldnames [$fieldindex];
global $$fieldname;
$command .= $fieldnames [$fieldindex] . "=" .
fieldfromtype ($$fieldname, $fieldtypes [$fieldindex]);
if ($fieldindex != $fieldsnumber - 1)
{ $command .= " and "; }
$fieldindex++;
}
executemsql ($database, $command);
}
}
function handleadd ($database, $table, $fieldsnumber,
$fieldnames, $fieldlengths, $fieldtypes)
{
global $add;
if ($add != "")
{
$command = "insert into " . $table . " (";
$fieldindex = 0;
while ($fieldindex < $fieldsnumber)
{
$command .= $fieldnames [$fieldindex];
if ($fieldindex != $fieldsnumber - 1)
{ $command .= ", "; }
$fieldindex++;
}
$command .= ") values (";
$fieldindex = 0;
while ($fieldindex < $fieldsnumber)
{
$fieldname = $fieldnames [$fieldindex];
global $$fieldname;
$command .= fieldfromtype ($$fieldname, $fieldtypes [$fieldindex]);
if ($fieldindex != $fieldsnumber - 1)
{ $command .= ", "; }
$fieldindex++;
}
$command .= ")";
executemsql ($database, $command);
}
}
function displayremoveupdate ($database, $table, $sortcolumn,
$fieldsnumber, $fieldnames, $fieldlengths)
{
$result = "";
if ($sortcolumn != "")
{ $sortcolumn = " order by " . $sortcolumn; }
$msqlresult = msql ($database, "select * from " . $table . $sortcolumn);
$tuplesnumber = msql_numrows ($msqlresult);
$tupleindex = 0;
while ($tupleindex < $tuplesnumber)
{
$fieldindex = 0;
while ($fieldindex < $fieldsnumber)
{
$values [$fieldindex] = msql_result ($msqlresult, $tupleindex,
$fieldnames [$fieldindex]);
$fieldindex++;
}
$result .= displaytuple ($fieldsnumber, $fieldnames,
$fieldlengths, $values, "modify");
$tupleindex++;
}
return $result;
}
function displayadd ($fieldsnumber, $fieldnames, $fieldlengths)
{
$result = "";
$fieldindex = 0;
while ($fieldindex < $fieldsnumber)
{
$values [$fieldindex] = "";
$fieldindex++;
}
$result .= displaytuple ($fieldsnumber, $fieldnames,
$fieldlengths, $values, "add");
msql_close ();
return $result;
}
function administrationtable ($database, $table, $sortcolumn)
{
$result = "";
msql_connect ( "localhost");
$msqlresult = msql ($database, "select * from " . $table);
$fieldsnumber = msql_numfields ($msqlresult);
$msqlresult = msql_listfields ($database, $table);
$fieldindex = 0;
while ($fieldindex < $fieldsnumber)
{
$fieldnames [$fieldindex] = msql_fieldname ($msqlresult, $fieldindex);
$fieldlengths [$fieldindex] = msql_fieldlen ($msqlresult, $fieldindex);
$fieldtypes [$fieldindex] = msql_fieldtype ($msqlresult, $fieldindex);
$fieldindex++;
}
handleremove ($database, $table, $fieldsnumber, $fieldnames, $fieldlengths, $fieldtypes);
handleupdate ($database, $table, $fieldsnumber, $fieldnames, $fieldlengths, $fieldtypes);
handleadd ($database, $table, $fieldsnumber, $fieldnames, $fieldlengths, $fieldtypes);
$result .= displayremoveupdate ($database, $table, $sortcolumn, $fieldsnumber, $fieldnames,
$fieldlengths);
$result .= displayadd ($fieldsnumber, $fieldnames, $fieldlengths);
return $result;
}
function addtable ($database, $table)
{
$result = "";
msql_connect ( "localhost");
$msqlresult = msql ($database, "select * from " . $table);
$fieldsnumber = msql_numfields ($msqlresult);
$msqlresult = msql_listfields ($database, $table);
$fieldindex = 0;
while ($fieldindex < $fieldsnumber)
{
$fieldnames [$fieldindex] = msql_fieldname ($msqlresult, $fieldindex);
$fieldlengths [$fieldindex] = msql_fieldlen ($msqlresult, $fieldindex);
$fieldtypes [$fieldindex] = msql_fieldtype ($msqlresult, $fieldindex);
$fieldindex++;
}
handleadd ($database, $table, $fieldsnumber, $fieldnames, $fieldlengths, $fieldtypes);
$result .= displayadd ($fieldsnumber, $fieldnames, $fieldlengths);
return $result;
}
?>
/*************************************************************************************
* sqladmin v2.0 - an sql administration user interface for the web *
* copyright (c) 1997-98 alessandro vernet <avernet@scdi.org> *
*************************************************************************************
* this library is free software; you can redistribute it and/or *
* modify it under the terms of the gnu library general public *
* license as published by the free software foundation; either *
* version 2 of the license, or (at your option) any later version. *
* *
* this library is distributed in the hope that it will be useful, *
* but without any warranty; without even the implied warranty of *
* merchantability or fitness for a particular purpose. see the gnu *
* library general public license for more details. *
* *
* you should have received a copy of the gnu library general public *
* license along with this library; if not, write to the *
* free software foundation, inc., 59 temple place - suite 330, *
* boston, ma 02111-1307, usa. *
*************************************************************************************/
/* todo:
* - add sort order.
* - add simple view.
* - add some documentation.
*/
/* limitations:
* - works only with msql.
*/
/* history:
* - 97-11-05 (avernet) corrected a bug with quote.
* - 98-01-01 (avernet) added a sortcolumn parameter to
* administrationtable function.
* - 98-03-14 (avernet) added function addtable to enable users to
* add (but not modify) en entry to the database.
* - 98-05-19 (avernet) submitted to px.
* - 98-10-11 (avernet) now sqladmin works with php3. the php2 version
* will not be mainteained anymore.
* - 98-10-11 (avernet) sqladmin is now distributed under the lgpl
* instead of mpl.
*/
function escapeforhtml ($string)
{
$result = $string;
//$result = ereg_replace ("\"", """, $result);
$result = ereg_replace ("<", "<", $result);
$result = ereg_replace (">", ">", $result);
return $result;
}
function displaytuple ($fieldsnumber, $fieldnames,
$fieldlengths, $values, $mode)
{
$result = "";
$result .= "<form method=\"post\"><table border><tr>" .
"<td bgcolor=\"#ccccff\">";
$result .= "<table cellspacing=\"0\" cellpadding=\"0\">";
$fieldindex = 0;
while ($fieldindex < $fieldsnumber)
{
$result .= "<tr><td>" . $fieldnames [$fieldindex] . "</td><td>";
if ($fieldlengths [$fieldindex] <= 128)
{
$result .= "<input type=\"text\" name=\"" .
$fieldnames [$fieldindex] . "\" value=\"" .
$values [$fieldindex] . "\" size=\"64\">";
}
else
{
$result .= "<textarea name=\"" .
$fieldnames [$fieldindex] . "\"" .
" cols=\"64\" rows=\"10\" wrap=\"virtual\">" .
escapeforhtml ($values [$fieldindex]) . "</textarea>";
}
$result .= "<input type=\"hidden\" name=\"old-" .
$fieldnames [$fieldindex] .
"\" value=\"" . escapeforhtml ($values [$fieldindex]) . "\">" .
"</td></tr>";
$fieldindex++;
}
$result .= "<tr><td align=\"center\" colspan=\"2\">";
if ($mode == "modify")
{
$result .= "<input type=\"submit\" name=\"remove\" value=\"remove\">";
$result .= "<input type=\"submit\" name=\"update\" value=\"update\">";
}
else
{ $result .= "<input type=\"submit\" name=\"add\" value=\"add\">"; }
$result .= "</table></td></tr></table></form>";
return $result;
}
function fieldfromtype ($text, $type)
{
if ($type == "int" || $type == "uint" || $type == "real")
{ $result = $text; }
else
{ $result = "'" . addslashes ($text) . "'"; }
return $result;
}
function executemsql ($database, $command)
{
/*echo "<tt>" . $command . "</tt><hr>";*/
msql ($database, $command);
}
function handleremove ($database, $table, $fieldsnumber,
$fieldnames, $fieldlengths, $fieldtypes)
{
global $remove;
if ($remove != "")
{
$command = "delete from " . $table . " where ";
$fieldindex = 0;
while ($fieldindex < $fieldsnumber)
{
$fieldname = "old-" . $fieldnames [$fieldindex];
global $$fieldname;
$command .= $fieldnames [$fieldindex] . "=" .
fieldfromtype ($$fieldname, $fieldtypes [$fieldindex]);
if ($fieldindex != $fieldsnumber - 1)
{ $command .= " and "; }
$fieldindex++;
}
executemsql ($database, $command);
}
}
function handleupdate ($database, $table, $fieldsnumber,
$fieldnames, $fieldlengths, $fieldtypes)
{
global $update;
if ($update != "")
{
$command = "update " . $table . " set ";
$fieldindex = 0;
while ($fieldindex < $fieldsnumber)
{
$fieldname = $fieldnames [$fieldindex];
global $$fieldname;
$command .= $fieldname . "=" .
fieldfromtype ($$fieldname, $fieldtypes [$fieldindex]);
if ($fieldindex != $fieldsnumber - 1)
{ $command .= ", "; }
$fieldindex++;
}
$command .= " where ";
$fieldindex = 0;
while ($fieldindex < $fieldsnumber)
{
$fieldname = "old-" . $fieldnames [$fieldindex];
global $$fieldname;
$command .= $fieldnames [$fieldindex] . "=" .
fieldfromtype ($$fieldname, $fieldtypes [$fieldindex]);
if ($fieldindex != $fieldsnumber - 1)
{ $command .= " and "; }
$fieldindex++;
}
executemsql ($database, $command);
}
}
function handleadd ($database, $table, $fieldsnumber,
$fieldnames, $fieldlengths, $fieldtypes)
{
global $add;
if ($add != "")
{
$command = "insert into " . $table . " (";
$fieldindex = 0;
while ($fieldindex < $fieldsnumber)
{
$command .= $fieldnames [$fieldindex];
if ($fieldindex != $fieldsnumber - 1)
{ $command .= ", "; }
$fieldindex++;
}
$command .= ") values (";
$fieldindex = 0;
while ($fieldindex < $fieldsnumber)
{
$fieldname = $fieldnames [$fieldindex];
global $$fieldname;
$command .= fieldfromtype ($$fieldname, $fieldtypes [$fieldindex]);
if ($fieldindex != $fieldsnumber - 1)
{ $command .= ", "; }
$fieldindex++;
}
$command .= ")";
executemsql ($database, $command);
}
}
function displayremoveupdate ($database, $table, $sortcolumn,
$fieldsnumber, $fieldnames, $fieldlengths)
{
$result = "";
if ($sortcolumn != "")
{ $sortcolumn = " order by " . $sortcolumn; }
$msqlresult = msql ($database, "select * from " . $table . $sortcolumn);
$tuplesnumber = msql_numrows ($msqlresult);
$tupleindex = 0;
while ($tupleindex < $tuplesnumber)
{
$fieldindex = 0;
while ($fieldindex < $fieldsnumber)
{
$values [$fieldindex] = msql_result ($msqlresult, $tupleindex,
$fieldnames [$fieldindex]);
$fieldindex++;
}
$result .= displaytuple ($fieldsnumber, $fieldnames,
$fieldlengths, $values, "modify");
$tupleindex++;
}
return $result;
}
function displayadd ($fieldsnumber, $fieldnames, $fieldlengths)
{
$result = "";
$fieldindex = 0;
while ($fieldindex < $fieldsnumber)
{
$values [$fieldindex] = "";
$fieldindex++;
}
$result .= displaytuple ($fieldsnumber, $fieldnames,
$fieldlengths, $values, "add");
msql_close ();
return $result;
}
function administrationtable ($database, $table, $sortcolumn)
{
$result = "";
msql_connect ( "localhost");
$msqlresult = msql ($database, "select * from " . $table);
$fieldsnumber = msql_numfields ($msqlresult);
$msqlresult = msql_listfields ($database, $table);
$fieldindex = 0;
while ($fieldindex < $fieldsnumber)
{
$fieldnames [$fieldindex] = msql_fieldname ($msqlresult, $fieldindex);
$fieldlengths [$fieldindex] = msql_fieldlen ($msqlresult, $fieldindex);
$fieldtypes [$fieldindex] = msql_fieldtype ($msqlresult, $fieldindex);
$fieldindex++;
}
handleremove ($database, $table, $fieldsnumber, $fieldnames, $fieldlengths, $fieldtypes);
handleupdate ($database, $table, $fieldsnumber, $fieldnames, $fieldlengths, $fieldtypes);
handleadd ($database, $table, $fieldsnumber, $fieldnames, $fieldlengths, $fieldtypes);
$result .= displayremoveupdate ($database, $table, $sortcolumn, $fieldsnumber, $fieldnames,
$fieldlengths);
$result .= displayadd ($fieldsnumber, $fieldnames, $fieldlengths);
return $result;
}
function addtable ($database, $table)
{
$result = "";
msql_connect ( "localhost");
$msqlresult = msql ($database, "select * from " . $table);
$fieldsnumber = msql_numfields ($msqlresult);
$msqlresult = msql_listfields ($database, $table);
$fieldindex = 0;
while ($fieldindex < $fieldsnumber)
{
$fieldnames [$fieldindex] = msql_fieldname ($msqlresult, $fieldindex);
$fieldlengths [$fieldindex] = msql_fieldlen ($msqlresult, $fieldindex);
$fieldtypes [$fieldindex] = msql_fieldtype ($msqlresult, $fieldindex);
$fieldindex++;
}
handleadd ($database, $table, $fieldsnumber, $fieldnames, $fieldlengths, $fieldtypes);
$result .= displayadd ($fieldsnumber, $fieldnames, $fieldlengths);
return $result;
}
?>
下一篇: 面粉可以做汤圆吗
推荐阅读
-
一个SQL语句获得某人参与的帖子及在该帖得分总和
-
java当中JDBC当中请给出一个sql server的dataSource的helloworld例子
-
sql将一个表中的数据插入到另一个表中的方法
-
我的第一个python web开发框架(32)——接口代码重构
-
我的第一个python web开发框架(36)——后台菜单管理功能
-
JAVA WEB快速入门之从编写一个基于SpringBoot+Mybatis快速创建的REST API项目了解SpringBoot、SpringMVC REST API、Mybatis等相关知识
-
nodeJS搭一个简单的(代理)web服务器
-
SQL Server 将一个表中字段的值复制到另一个表的字段中
-
探讨:Oracle数据库查看一个进程是如何执行相关的实际SQL语句
-
Asp.net中处理一个站点不同Web应用共享Session的问题