ecshop后台实现可修改表前缀的功能
很多客户在安装ecshop的时候,忽略表前缀,安装上了测试数据的表前缀和安装时候的不一样,下面我们教大家在后台实现可修改表前缀的功能
1、把rename.htm放到到admin/templates
2、将sql.php放到admin
3、修改admin/includes/inc_menu.php
查找'sql.php?act=main';在下面添加一行:
$modules['13_backup']['04_rename'] = 'sql.php?act=rename';
4、修改languages/zh_cn/admin/common.php
查找'SQL查询';在下面添加一行:
$_LANG['04_rename'] = '更改数据表前缀';
5、进入后台,在数据库管理就可以看到‘更改数据库前缀’的功能了。
rename.htm 文件源码:
<!-- $Id: sql.htm 14216 2008-03-10 02:27:21Z testyang $ -->
{include file="pageheader.htm"}
<div class="form-div">
{if $result}
{$result}<br />
<a href="sql.php?act=rename">返回</a>
{else}
提醒:一般修改前缀目的是为了数据库的安全和方便数据库转移,不是必要时刻,请不要修改,修改前先备份数据库。
<form name="sqlFrom" method="post" action="sql.php" onsubmit="return validate()">
<table>
<tr><th>数据名:</th><td><input type="text" name="shujuku" value=""></td></tr>
<tr><th>把数据表前缀设置为:</th><td><input type="text" name="qianzhui" value="">请不要带‘_’,修改成功后,再修改一下配置文件data/config.php,$prefix="新的前缀名_";(不要忘了后面的‘_’)</td></tr>
<tr><th></th><td><input value="确定" type="submit" class="button" /></td></tr>
<input type="hidden" name="act" value="rename_sub">
</table>
</form>
{/if}
</div>
{include file="pagefooter.htm"}
sql.php文件源码:
<?php
define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
$_POST['sql'] = !empty($_POST['sql']) ? trim($_POST['sql']) : '';
/*------------------------------------------------------ */
//-- 用户账号列表
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'main')
{
admin_priv('sql_query');
assign_query_info();
$smarty->assign('type', -1);
$smarty->assign('ur_here', $_LANG['04_sql_query']);
$smarty->display('sql.htm');
}
if ($_REQUEST['act'] == 'rename')
{
admin_priv('sql_query');
assign_query_info();
$smarty->assign('type', -1);
$smarty->assign('ur_here', $_LANG['04_sql_query']);
$smarty->display('rename.htm');
}
if ($_REQUEST['act'] == 'rename_sub')
{
admin_priv('sql_query');
$shujuku = $_REQUEST['shujuku'];
$qianzhui = $_REQUEST['qianzhui'];
if($shujuku && $qianzhui)
{
if(strpos($qianzhui ,'_'))
{
$smarty->assign('result', '前缀不允许带‘_’!');
}
else
{
$sql = "SELECT TABLE_NAME from information_schema.columns where TABLE_SCHEMA = '".$shujuku."' group by TABLE_NAME";
$table_name = $db->getAll($sql);
if($table_name)
{
foreach($table_name as $val)
{
$old_val = $val['TABLE_NAME'];
$new_val = $qianzhui . substr($old_val ,strpos($old_val ,'_'));
$sql = 'alter table `'.$shujuku.'`.`'. $old_val .'` rename to `'.$shujuku.'`.`'.$new_val .'`';
$db->query($sql);
}
$smarty->assign('result', '成功!再修改一下配置文件data/config.php,$prefix="新的前缀名_";(不要忘了后面的‘_’)就可以了。');
}
else
{
$smarty->assign('result', '数据库名错误!');
}
}
}
else
{
$smarty->assign('result', '失败!没有输入数据库名或者前缀');
}
assign_query_info();
$smarty->assign('type', -1);
$smarty->assign('ur_here', $_LANG['04_sql_query']);
$smarty->display('rename.htm');
}
if ($_REQUEST['act'] == 'query')
{
admin_priv('sql_query');
assign_sql($_POST['sql']);
assign_query_info();
$smarty->assign('ur_here', $_LANG['04_sql_query']);
$smarty->display('sql.htm');
}
/**
*
*
* @access public
* @param
*
* @return void
*/
function assign_sql($sql)
{
global $db, $smarty, $_LANG;
$sql = stripslashes($sql);
$smarty->assign('sql', $sql);
/* 解析查询项 */
$sql = str_replace("\r", '', $sql);
$query_items = explode(";\n", $sql);
foreach ($query_items as $key=>$value)
{
if (empty($value))
{
unset($query_items[$key]);
}
}
/* 如果是多条语句,拆开来执行 */
if (count($query_items) > 1)
{
foreach ($query_items as $key=>$value)
{
if ($db->query($value, 'SILENT'))
{
$smarty->assign('type', 1);
}
else
{
$smarty->assign('type', 0);
$smarty->assign('error', $db->error());
return;
}
}
return; //退出函数
}
/* 单独一条sql语句处理 */
if (preg_match("/^(?:UPDATE|DELETE|TRUNCATE|ALTER|DROP|FLUSH|INSERT|REPLACE|SET|CREATE)\\s+/i", $sql))
{
if ($db->query($sql, 'SILENT'))
{
$smarty->assign('type', 1);
}
else
{
$smarty->assign('type', 0);
$smarty->assign('error', $db->error());
}
}
else
{
$data = $db->GetAll($sql);
if ($data === false)
{
$smarty->assign('type', 0);
$smarty->assign('error', $db->error());
}
else
{
$result = '';
if (is_array($data) && isset($data[0]) === true)
{
$result = "<table> \n <tr>";
$keys = array_keys($data[0]);
for ($i = 0, $num = count($keys); $i < $num; $i++)
{
$result .= "<th>" . $keys[$i] . "</th>\n";
}
$result .= "</tr> \n";
foreach ($data AS $data1)
{
$result .= "<tr>\n";
foreach ($data1 AS $value)
{
$result .= "<td>" . $value . "</td>";
}
$result .= "</tr>\n";
}
$result .= "</table>\n";
}
else
{
$result ="<center><h3>" . $_LANG['no_data'] . "</h3></center>";
}
$smarty->assign('type', 2);
$smarty->assign('result', $result);
}
}
}
?>