|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
告诉你了一个方式,但是缺少努力这一环节,那也是白搭。数据|数据库|语句 在编写法式中,写添加和更新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>";
}
}
}
?> 完成一个功能齐全的动态站点 |
|