index.php:
<?php
header('Content-Type:text/html;charset=utf-8;');//設(shè)置編碼
if($_POST){
$cururl='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];//當(dāng)前網(wǎng)址
$data=$_POST;
$fp = fopen("tpl_sql.sql", "r"); //打開sql文件
if(trim($data["db_ip"])!=""){
}
$conn=@mysql_connect($data["db_ip"],$data["db_username"],$data["db_userpwd"]);
if(!$conn||$data["db_ip"]=="") {
echo "<script>alert('連接失敗!');window.location='{$cururl}';</script>";
exit();
}
$select_db=@mysql_select_db($data["db_name"],$conn);
if(!$select_db)
{
echo "<script>alert('數(shù)據(jù)庫不存在!');window.location='{$cururl}';</script>";
exit();
}
if(is_file("lock.txt")){
echo "<script>alert('請(qǐng)先刪除lock.txt鎖定文件!');window.location='{$cururl}';</script>";
exit();
}
//更改配置文件-開始
$config=file_get_contents("tpl_db_config.php");
$config = str_ireplace("{db_ip}",$data["db_ip"],$config);
$config = str_ireplace("{db_name}",$data["db_name"],$config);
$config = str_ireplace("{db_username}",$data["db_username"],$config);
$config = str_ireplace("{db_userpwd}",$data["db_userpwd"],$config);
$db_config_file="../include/db_config.php";
if(is_writable($db_config_file)) {
echo file_put_contents($db_config_file,$config);
} else {
echo "文件 $filename 不可寫";
}
//更改配置文件-結(jié)束
mysql_query("set names utf8");//設(shè)置數(shù)據(jù)庫編碼,注意有些空間不允許設(shè)置,所以要去掉
$sql_one="";
while(! feof($fp))
{
$line=fgets($fp);
if($line=="\r\n"){
if(strpos($sql_one,"CREATE TABLE")===0||strpos($sql_one,"INSERT INTO")===0){
$rs=mysql_query($sql_one);
if(!$rs)
{
echo mysql_error();
die("執(zhí)行失敗!".$sql_one);
//exit();
}
}
$sql_one="";
}else{
$sql_one.=trim($line);
}
}
$myfile = fopen("lock.txt", "w") or die("Unable to open file!");
fclose($fp);
echo "<script>alert('導(dǎo)入成功!');window.location='{$cururl}';</script>";
exit();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no" />
<title>數(shù)據(jù)庫初始化導(dǎo)入</title>
<style>*{ font-family:"微軟雅黑"; font-size:13px;}</style>
</head>
<body>
<div style="position:absolute; left:50%; top:50%; margin-left:-170px; margin-top:-120px;">
<div style="height:240px; width:340px; border-radius:5px;box-shadow:0px 0px 2px 0px #666666 inset">
<form id="form1" name="form1" method="post" action="">
<p style="margin:0px; text-align:center; line-height:40px; font-size:15px; border-bottom:solid #ccc 1px; margin-bottom:10px;">數(shù)據(jù)庫初始化導(dǎo)入</p>
<table style="width:auto; margin-left:auto; margin-right:auto;">
<tr><td align="right">數(shù)據(jù)庫IP</td><td><input type="text" name="db_ip" /></td></tr>
<tr><td align="right">數(shù)據(jù)庫名</td><td><input type="text" name="db_name" /></td></tr>
<tr><td align="right">數(shù)據(jù)庫登錄名</td><td><input type="text" name="db_username" /></td></tr>
<tr><td align="right">數(shù)據(jù)庫登錄密碼</td><td><input type="text" name="db_userpwd" /></td></tr>
<tr><td align="right"> </td><td><input type="submit" name="Submit" value="提交" /></td></tr>
</table>
</form>
</div>
</div>
</body>
</html>
tpl_sql.sql(sql模板):這個(gè)是從你的phpmyadmin里導(dǎo)出來的數(shù)據(jù)庫文件
tpl_db_config.php(配置模板):我的如下
<?php
define("DB_IP",'{db_ip}'); //數(shù)據(jù)庫ip
define("DB_NAME", '{db_name}'); //數(shù)據(jù)庫名
define("DB_ROOT",'{db_username}'); //數(shù)據(jù)庫登錄名
define("DB_PASSWORD",'{db_userpwd}'); //數(shù)據(jù)庫登錄密碼
define("DB_CONNECT_ERROR",'連接數(shù)據(jù)庫出錯(cuò)!');
define("DB_DATABASE_ERROR",'選擇數(shù)據(jù)庫不存在!');
header('Content-Type:text/html;charset=utf-8;');
define("Login_Titile",'一個(gè)賬號(hào),留言方便你們');
date_default_timezone_set('Asia/Shanghai');//設(shè)為上海時(shí)間
date_default_timezone_set('PRC');//設(shè)為北京時(shí)間
error_reporting(E_ALL & ~ E_NOTICE);//屏蔽那些可以忽略的錯(cuò)誤,如提示什么變量未定義
define("DB_QZ",'bd_');//表前綴
?>