在编写程序中,写添加和更新sql语句是一个比较烦琐的过程,特别是在表结构比较复杂的时候
这个程序主要是根据数据表格式自动生成添加/更新的sql语句,然后由程序员直接加入到程序中,方便编程
下面是文件,方法建立一个单独的文件,按照下面的说明建立相关文件,运行即可
index.php 索引文件
config.php 配置文件,转换get/post数据
addxxx.php 根据提交表格数据生成sql语句
addxxx2.php 根据数据库表信息生成sql语句,配置数据库在文件内部
@author bluemaple<bluemaple@xinhuanet.com>
———————————————————-
index.php文件
———————————————————-
自动生成模版
<br><br>
<a href=addxxx.php>自动生成添加表格,并产生相关sql添加语句,生成update语句(简单版)</a>
<br><br>
<a href=addxxx2.php>更数据表生成相关添加页面</a>
———————————————————-
config.php文件
———————————————————-
<?
/* 转换get和post数据 */
if (!defined(pma_grab_globals_included))
{
define(pma_grab_globals_included, 1);
if (!empty($_get))
{
extract($_get);
}
else if (!empty($http_get_vars))
{
extract($http_get_vars);
} // end if
if (!empty($_post))
{
extract($_post);
}
else if (!empty($http_post_vars))
{
extract($http_post_vars);
} // end if
if (!empty($_files))
{
while (list($name, $value) = each($_files))
{
$$name = $value[tmp_name];
}
}
else if (!empty($http_post_files))
{
while (list($name, $value) = each($http_post_files))
{
$$name = $value[tmp_name];
}
} // end if
}
?>
———————————————————-
addxxx.php
———————————————————-
<?
include("config.php");
if($action == "")
{
echo "<form action=addxxx.php method=get onsubmit=\"if(n.value==){alert(请输入数字);return false;}else return true;\">
添加的表数目:<input type=text name=n value=1><br>
<input type=hidden name=action value=c>
<input type=submit name=createsubmit value=生成>
</form>";
exit();
}
if($action == "c")
{
echo "<form method=post action=addxxx.php>";
echo " 添加到数据表:<input type=text name=t><br>
方法(method):<input type=text name=m value=post><br>
提交表到(action):<input type=text name=a value=<? echo \$http_server_vars[php_self] ?>><br><br>";
echo "<table border=1>";
echo "<tr><td>编号</td><td>说明</td><td>表名(tablename)</td><td>值(value)</td></tr>";
for($i = 1; $i<=$n; $i++)
{
echo "<tr>";
echo "<td>$i</td>";
echo "<td><input name=e$i type=text></td>";
echo "<td><input name=n$i type=text></td>";
echo "<td><input name=v$i type=text></td>";
echo "</tr>";
}
echo "<tr><td colspan=3 align=center><input type=submit name=submit value=确 定></td></tr>";
echo "</table>";
echo "<input type=hidden name=n value=$n>";
echo "<input type=hidden name=action value=a>";
echo "</form>";
}
if($action == "a")
{
echo "<h3>生成的插入sql语句</h3>";
if($submit)
{
if($t == "") $t = "table";
$tname = "";
$tvalue= "";
// table …
for($i = 1; $i <= $n; $i++)
{
$temp = "n".$i;
$tname .= $$temp;
if($i != $n)
{
$tname .= ", ";
}
$tempi = "n".$i;
$tvalue .= "\$".$$tempi."";
if($i != $n)
{
$tvalue .= ", ";
}
$usqli .= $$temp."=\$".$$tempi."";
if($i != $n)
{
$usqli .= ", ";
}
}
$sql = "insert into $t ($tname) values ($tvalue);";
echo $sql;
echo "<br><br>";
$usql = "update $t set ".$usqli." where ***";
echo $usql;
echo "<pre>";
echo "<b>生成表格</b> 点右键查看源文件";
echo "<table width=\"700\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\" bgcolor=\"\">
<form action=$a method=$m>
<tr>
<td width=\"\"></td>
<td width=\"\"></td>
</tr>";
for($i = 1; $i <= $n; $i++)
{
$etemp = "e".$i;
$ntemp = "n".$i;
$vtemp = "v".$i;
echo "<tr>
<td>".$$etemp."</td>
<td><input type=text class=\"\" name=\"".$$ntemp."\" value=\"".$$vtemp."\"></td>
</tr>";
}
echo "<tr>
<td></td>
<td><input type=submit value=添加 class=\"\"><input type=reset value=重置 class=\"\"></td>
</tr>
</form>
</table>";
echo "</pre>";
}
}
?>
———————————————————-
addxxx2.php
———————————————————-
<?
include("config.php");
if($action == "") $action = "d";
$mysqlserver = "localhost";
$mysqlport = 3306;
$mysqluser = "root";
$mysqlpwd = "";
if($action == d)
{
$mysql_id = mysql_pconnect($mysqlserver.":".$mysqlport, $mysqluser, $mysqlpwd);
if($db == "")
{
$db_list = mysql_list_dbs();
echo "选择数据库<br>";
while ($row = mysql_fetch_object($db_list))
{
echo "<a href=addxxx2.php?action=d&db=".$row->database.">".$row->database ."</a><br>";
}
}
if($db != "")
{
mysql_select_db($db);
$tresult = mysql_list_tables($db);
if (!$tresult)
{
echo "db error, could not list tables\n";
echo mysql error: . mysql_error();
exit;
}
echo "数据库<b>$db</b>列表<br>";
while ($row = mysql_fetch_row($tresult))
{
print "<a href=addxxx2.php?db=$db&tname=$row[0]>$row[0]</a><br>";
}
if($tname != "")
{
$fields = mysql_list_fields($db, $tname);
$n = mysql_num_fields($fields);
echo "<br><b>$tname</b>表中:";
for ($i = 0; $i < $n; $i++)
{
$name = mysql_field_name($fields, $i);
echo $name." ";
$tname .= $name;
$vname .= "\$".$name."";
$usqli .= $name."=\$".$name."";
if($i != $n-1)
{
$tname .= ", ";
$vname .= ", ";
$usqli .=" and ";
}
//create table…
$table .= "<tr>
<td>".$name."</td>
<td><input type=text class=\"\" name=\"".$name."\" value=\"\"></td>
</tr>\n";
}
echo "生成表:<br><br>";
echo "insert into $tname ($tname) values ($vname);";
echo "<br><br>";
$usql = "update $tname set ".$usqli." where ***";
echo $usql;
echo "<pre>";
echo "<b>生成表格</b> 点右键查看源文件<br>\n";
echo "<table width=\"700\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\" bgcolor=\"\">
<form action=$a method=$m>
<tr>
<td width=\"\"></td>
<td width=\"\"></td>
</tr>";
echo "$table";
echo "<tr>
<td></td>
<td><input type=submit value=添加 class=\"\"><input type=reset value=重置 class=\"\"></td>
</tr>
</form>
</table>";
echo "</pre>";
}
}
}
?>
