<? if($database_host == '' || $database_username == ''){?>
<form action='index.php?step=initdata' method='post' onsubmit="return confirm('重新安装将会破坏原来的数据，确定安装吗？')">
<br><br>
              <table width="80%" border="0" cellspacing="0" cellpadding="3" align="center" class="inputline" style="border:1px solid #ccc;">
                <tr>
                  <td align="center">数据库类型</td>
                  <td >
                    <select name="database_type">
                      <option value="1" selected>MySQL</option>
                    </select>
                  </td>
                </tr>
                <tr>
                  <td align="center">数据库服务器</td>
                  <td >
                    <input type="text" name="database_host" value="<?=$database_host?$database_host:"localhost"?>">
					<font color="#FF0000"><?=$database_host == ''?'请输入数据库服务器名':''?></font>
                  </td>
                </tr>
                <tr>
                  <td align="center">数据库名称</td>
                  <td >
                    <input type="text" name="database_name" value="<?=$database_name?>">
					<font color="#FF0000"><?=$database_name == ''?'请输入数据库名':''?></font>
                  </td>
                </tr>
                <tr>
                  <td align="center">数据库用户名</td>
                  <td >
                    <input type="text" name="database_username" value="<?=$database_username?>">
					<font color="#FF0000"><?=$database_username == ''?'请输入数据库用户名':''?></font>
                  </td>
                </tr>
                <tr>
                  <td align="center">数据库密码</td>
                  <td >
                    <input type="password" name="database_password" value="<?=$database_password?>">
					<font color="#FF0000"><?=$database_password == ''?'请输入数据库密码':''?></font>
                  </td>
                </tr>
              </table>
	

	<div style="text-align:right;padding:20px 20px 20px 20px;width:90%;">
	<?
		echo '<div style="float:left"><input type="button" value="上一步" class="BUTTON" onClick="location=\'index.php?step=syscheck\'"></div>';
		echo '<input type="submit" value="下一步" class="BUTTON"">';
	?>
	</div>
	 </form>
<? }else{ ?>
<?
	$db=@mysql_connect($database_host,$database_username,$database_password);
	if(!$db) $error="数据库主机、用户名、密码中一个或多个有误：".mysql_error();
	if(!$error){
		if(!@mysql_select_db($database_name,$db)){
			$error="数据库名有错或您没有访问该数据库的权限";
		}
	}
	mysql_query('SET NAMES latin1');
	if(!$error){
		$filename = "sql.sql";
		$fp = fopen($filename, "r");
		$content = fread($fp, filesize($filename));
		
		$content = str_replace("\r", '', $content);
		$contents = explode(";\n", $content);
		$c_count = count($contents);
		for($i = 0; $i < $c_count-1; $i ++)
		{
			$contents[$i] = trim($contents[$i]);
			if(!$contents[$i]) continue;
			mysql_query($contents[$i],$db);
			if (mysql_error()) {
				$error="初始化数据出错:".$contents[$i].mysql_error();
				break;
			}
		}
		fclose($fp);
	}
	$query ="select * from users";
	$result=mysql_query($query,$db);
    while($row=mysql_fetch_array($result))
    {
		$db_new=@mysql_connect($database_host,$database_username,$database_password);
		if(!$db_new) $error="数据库主机、用户名、密码中一个或多个有误：".mysql_error();
		if(!$error){
			if(!@mysql_select_db($database_name,$db_new)){
				$error="数据库名有错或您没有访问该数据库的权限";
			}
		}
		mysql_query('SET NAMES latin1');
		$sql="UPDATE `users` SET `Password` = MD5('".$row['Password']."') WHERE `UserName` = '".$row['UserName']."' LIMIT 1;";
		mysql_query($sql,$db_new);
		if (mysql_error()) {
				$error="初始化数据出错:".$sql.mysql_error();
				break;
			}
    }
	?>
<table width="100%">
<tr>
	<td>&nbsp;</td>
</tr>
</table>
	<div style="text-align:right;padding:20px 20px 20px 20px;width:90%;">
	<?
	if(!$error){
	?>
<table width="100%">
<tr>
	<td style="padding:10px 0px 50px 0px;color:#006600;font-size:14px;font-weight:bold;">数据库安装成功,请进入下一步</td>
</tr>
</table>
	<form action='index.php?step=sysset' method='post'>
	  <input type='hidden' name='database_host' value='<?=$database_host?>'>
	  <input type='hidden' name='database_name' value='<?=$database_name?>'>
	  <input type='hidden' name='database_username' value='<?=$database_username?>'>
	  <input type='hidden' name='database_password' value='<?=$database_password?>'>
	<?
		echo '<div style="float:left"><input type="button" value="上一步" class="BUTTON" onClick="location=\'index.php?step=syscheck\'"></div>';
		echo '<input type="submit" value="下一步" class="BUTTON"">';
	?>
	</form>
	<?
	}else{
		echo '<br><div style="text-align:center;color:Red;width:120%">'.$error.'<br><br>';
		echo '<input type="button" value="返回" class="BUTTON" onClick="history.go(-1)"></div>';
	}
?>
</div>
<? }?>