通过HTML表单创建数据库
HTML代码:
代码
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2 <html>
3 <head>
4 <style type="text/css">
5 td{
6 margin-top:100px;
7 }
8 input{
9 border:1px solid #cccccc;
10 }
11 label{
12 font-size:14px;
13 font-family:"微软雅黑","微软细黑","楷体","宋体";
14 }
15 </style>
16 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
17 <title>Insert title here</title>
18 </head>
19 <body>
20 <form action="1.php" method="post">
21 <table align="center">
22 <tr>
23 <td><label for="host">地址:</label><input type="text" name="host" /></td>
24 </tr>
25 <tr>
26 <td><label for="user">账户:</label><input type="text" name="user" /></td>
27 </tr>
28 <tr>
29 <td><label for="pass">密码:</label><input type="text" name="pass" /></td>
30 </tr>
31 <tr>
32 <td><label for="data">库名:</label><input type="text" name="data" /></td>
33 </tr>
34 <tr>
35 <td><label for="tabl">表名:</label><input type="text" name="tabl" /></td>
36 </tr>
37 <tr>
38 <td><label for="name">名字:</label><input type="text" name="name" /></td>
39 </tr>
40 <tr>
41 <td><label for="age">年龄:</label><input type="text" name="age" /></td>
42 </tr>
43 <tr>
44 <td><label for="sex">性别:</label><input type="text" name="sex" /></td>
45 </tr>
46 <tr>
47 <td><input type="submit" name="submit" value="提交" /></td>
48 <td><input type="submit" name="quxiao" value="取消" /></td>
49 </tr>
50 </table>
51 </form>
52 </body>
53 </html>
代码
1 <?php
2 $host = $_POST["host"];
3 $user = $_POST["user"];
4 $pass = $_POST["pass"];
5 $data = $_POST["data"];
6 $tabl = $_POST["tabl"];
7 $name = $_POST["name"];
8 $age = $_POST["age"];
9 $sex = $_POST["sex"];
10 if(($coon = mysql_connect($host,$user,$pass))){ //连接数据库
11 if(mysql_query("create database $data",$coon)){ //创建数据库
12 if(mysql_select_db($data,$coon)){//选择哪个数据库
13 if(mysql_query("create table $tabl(name varchar(20),age int,sex varchar(2))",$coon)){//创建表格
14 $sql = "insert into $tabl(name,age,sex)values('$name',$age,'$sex')";//插入表格数据
15 if(mysql_query($sql,$coon)){
16 $result = mysql_query("select *from $tabl",$coon);
17 while($row = mysql_fetch_array($result)){//查询表中的数据
18 echo $row["name"]." ".$row["age"]." ".$row["sex"];
19 echo "<br />";
20 }
21 }
22 }
23 }
24 }
25 }
26 mysql_close($coon);
27 ?>

浙公网安备 33010602011771号