在做好了第一个数据库安装页面后,突发奇想,因为这个原理是通过将主机名、SA用户及SA密码集成到页面中,如果交给别人使用,他最起码得更改SA用户或是SA密码,不是很方便。那么,就想到,是否可以用表单提交的方式,在页面上提交主机名、SA用户名及SA密码。经过两天不停的测试,数据库可以安装、卸载。基本满意。但是在进行表单验证时,遇到了困难,目前无法解决。问题一:用submit提交时,会刷新页面,;二、如果改用button来代替submit,解决了页面刷新问题,却又遇到了程序无法往下执行的问题。现贴出来,请指点。
[code=php]
<?php $a="留言簿数据库安装"; ?>
<title><?=$a;?></title>
<form name="whb" action="install.php" method="post">
<br><br>
<table width=400 align=center border=1 cellpadding=0 cellspacing=0 bordercolor=green >
<tr height=50>
<td colspan=2 align=center ><b><?=$a;?></b></td>
</tr>
<tr height=50 >
<td width=200 align=center >主机名称:</td>
<td >    <input type="text" size=18 name="gethost"></td>
</tr>
<tr height=50 >
<td align=center >   SA用户:</td>
<td>    <input type="text" size=18 name="user"></td>
</tr>
<tr height=50 >
<td align=center >连接密码:</td>
<td>    <input type="password" size=20 name="pin" ></td>
</tr>
<tr height=50 >
<td colspan=2 align=center >
<input type="submit" name="install" value="安装">
    
<input type="reset" name="renset" value="重置" >
    
<input type="submit" name="uninstall" value="卸载" >
</td>
</tr>
</table>
<br><br>
</form>
<div><font color=3399cc size=-2 ><b>
    这是本人花两天时间写出来的,没办法,菜鸟也就这水平了。<br><br>
    这个程序我自己都有些不满意,如,在检查表单时,页面总是刷新,不过,我目前还没有办法能解决。<br><br>
    解决了刷新,程序就不往下走了,先放放吧,等以后再慢慢的完善吧。人无完人,程序难道还能没有BUG吗?<br><br>
    欢迎高手们赐教!注,本人刚学,请不要用太深奥的代码示之!
</b></font></div>
<br><br><br>
<div align=center ><font color=blue size=-4>
Email:<a href=mailto:color1978@126.com>
color1978@126.com</a>    QQ:8828 0164     Blog:<a href=http://hexun.com/whb2000/default.html>大斌的网窝</a>
</font></div>
<?php
/* Install Program */
//define database
$shost=$_POST['gethost'];
$suser=$_POST['user'];
$spwd=$_POST['pin'];
//检测表单是否为空
if(empty($shost))
{
?>
<script>
alert(" 主机名不能为空,请输入主机名称或是IP地址!");
</script>
<?php
exit;
}
else if(empty($suser))
{
?>
<script>
alert(" SA用户名不能为空,请返回输入!");
</script>
<?php
exit;
}
else if(empty($spwd))
{
?>
<script>
alert(" SA密码不能为空!\n\n 请返回输入!");
</script>
<?php
exit;
}
else
{
?>
<script>
alert("现在即将开始 安装(卸载) 数据库,请 确定 开始!");
</script>
<?php
}
//link database
@$link=mysql_connect($shost,$suser,$spwd);
//installing
if($_POST[install]=="安装")
{
//check whb and delete its
@$sql="drop database IF EXISTS whb";
@mysql_query($sql);
//create new whb;
@$sql="create database whb";
@mysql_query($sql);
//chose whb;
@mysql_select_db("whb",$link);
@mysql_query("SET NAMES UTF8");
//create a table,name is message
@$mysql="create table message(ID bigint(20) not null auto_increment primary key,
name varchar(16) not null,
gender enum('F','M') not null default 'F',
face tinyint(2) not null default 0,
IP varchar(12) not null,
email varchar(50) not null,
QQ varchar(12) not null,
title varchar(100) not null,
time datetime not null default '0000-00-00 00:00:00',
content text not null,
retime datetime not null default '0000-00-00 00:00:00',
recontent text
)
ENGINE=MyISAM default charset=UTF8;
";
//ENGINE=MyISAM default charset=UTF8;让数据表以UTF8编码格式创建。
@$result=mysql_query($mysql);
if ($result)
{
?>
<script>
alert('数据库安装成功!');
window.location="index.php";
</script>
<?php
}
else
{
?>
<script>
alert('数据库安装不成功!');
</script>
<?php
}
}
else if($_POST[uninstall]=="卸载")
{
@$mysql="drop database IF EXISTS whb";
@$del=mysql_query($mysql);
if($del)
{
?>
<script>
alert('数据库卸载成功!');
</script>
<?php
}
else
{
?>
<script>
alert('数据库卸载失败,请返回!');
</script>
<?php
}
}
?>
[/code]
感谢风过无语提供实例代码供研究!
