//File: query.php
//描述: 数据库批量导入脚本
//功能: 可进行单行和批量插入.
//作者: SworDs
//QQ: 78623269
//日期: 2005-1-21
//其他:好老了啊,刚学PHP写的,连抄带查!2006.5.19从仓库里翻出来,。。。
error_reporting(7);
if ( function_exists('ini_get') ) {
$onoff = ini_get('register_globals');
} else {
$onoff = get_cfg_var('register_globals');
}
if ($onoff != 1) {
@extract($_POST, EXTR_SKIP);
@extract($_GET, EXTR_SKIP);
}
// 去掉转义字符
function stripslashes_array(&$array) {
while(list($key,$var) = each($array)) {
if ($key != 'argc' && $key != 'argv' && (strtoupper($key) != $key || ''.intval($key) == "$key")) {
if (is_string($var)) {
$array[$key] = stripslashes($var);
}
if (is_array($var)) {
$array[$key] = stripslashes_array($var);
}
}
}
return $array;
}
// 判断 magic_quotes_gpc 状态
if (get_magic_quotes_gpc()) {
$_POST = stripslashes_array($_POST);
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>数据库批量导入脚本 by SworDs</title>
<style type="text/css">
body,td {
font-family: "sans-serif";
font-size: "12px";
line-height: "150%";
}
.INPUT {
FONT-SIZE: "12px";
COLOR: "#000000";
BACKGROUND-COLOR: "#FFFFFF";
height: "18px";
border: "1px solid #666666";
}
.firstalt {BACKGROUND-COLOR: "#EFEFEF"}
.secondalt {BACKGROUND-COLOR: "#F5F5F5"}
</style>
</head>
<body style="table-layout:fixed; word-break:break-all">
<center>
<?
if ($_GET['action'] == "sql") {
$servername = isset($servername) ? $servername : 'localhost';
$dbusername = isset($dbusername) ? $dbusername : 'root';
$dbpassword = isset($dbpassword) ? $dbpassword : '';
$dbname = isset($dbname) ? $dbname : '';
}
if($doquery) {
if(@mysql_connect($servername,$dbusername,$dbpassword) AND @mysql_select_db($dbname))
{
echo "数据库连接执行!\n";
}
else
{
echo "出错: ".mysql_error();
}
$a_query = explode(";",$_POST['sql_query']);
while (list(,$query) = each($a_query)) {
$query = trim($query);
if ($query) {
if (strstr($query,'CREATE TABLE')) {
ereg('CREATE TABLE ([^ ]*)',$query,$regs);
mysql_query("DROP TABLE IF EXISTS $regs[1]");
echo "<p>正在建立表: ".$regs[1]." …… ";
$result=@mysql_query($query);
if ($result)
{
echo "成功!</p>\n";
} else {
echo "失败!</p>\n";
}
} else {
$result=@mysql_query($query);
echo ($result) ? "SQL语句成功执行\n" : "出错: ".mysql_error();
}
}
}
mysql_close();
}
elseif(connect){
if(@mysql_connect($servername,$dbusername,$dbpassword) AND @mysql_select_db($dbname)) {
echo "数据库连接执行!\n";
}else{
echo "出错: ".mysql_error();
}
}
?>
<table width="760" border="0" cellpadding="3" cellspacing="1" bgcolor="#ffffff">
<tr class="firstalt">
<td align="center">数据库批量导入脚本 By SworDs</td>
</tr>
<form action="?action=sql" method="POST">
<tr class="secondalt">
<td align="center">Host:
<input name="servername" type="text" class="INPUT" value="<?=$servername?>">
User:
<input name="dbusername" type="text" class="INPUT" size="15" value="<?=$dbusername?>">
Pass:
<input name="dbpassword" type="text" class="INPUT" size="15" value="<?=$dbpassword?>">
DB:
<input name="dbname" type="text" class="INPUT" size="15" value="<?=$dbname?>">
<input name="connect" type="submit" class="INPUT" value="连接"></td>
</tr>
<tr class="firstalt">
<td align="center"><textarea name="sql_query" cols="85" rows="10"></textarea></td>
</tr>
<tr class="secondalt">
<td align="center"><input type="submit" name="doquery" value="执行" class="input"></td>
</tr>
</form>
</table>
</body>
</html>