PHP操作数据库 动态创建字段
程序员文章站
2022-06-06 09:14:24
...
PHP操作数据库时自动创建字段,如下代码:
- html>
- head>
- meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
- title>动态创建字段 - www.cxybl.comtitle>
- style type="text/css">
- style>
- head>
- body>
- form id="form1" name="form1" method="post" action="index_ok.php">
- table width="467" height="289" border="0" cellpadding="0" cellspacing="0">
- tr>
- td width="168" height="89"> td>
- td width="289"> td>
- tr>
- tr>
- td height="30" align="right">span class="STYLE1">选择表 span>td>
- td class="STYLE1">input name="table" type="text" id="table" size="20" />td>
- tr>
- tr>
- td height="30" align="right">span class="STYLE1">字段 span>td>
- td class="STYLE1">input name="field" type="text" id="field" size="20">td>
- tr>
- tr>
- td height="30" align="right">span class="STYLE1">类型 span>td>
- td class="STYLE1">select name="type" id="type">
- option>intoption>
- option>textoption>
- option>dateoption>
- option>doubleoption>
- option>varcharoption>
- option>datetimeoption>
- option>bloboption>
- select> td>
- tr>
- tr>
- td height="30" align="right">span class="STYLE1">长度 span>td>
- td class="STYLE1">input name="length" type="text" id="length" size="15">td>
- tr>
- tr>
- td height="30" align="right">span class="STYLE1">NULL span>td>
- td class="STYLE1">input type="radio" name="null" value="null">
- null
- input type="radio" name="null" value="not null">
- not null td>
- tr>
- tr>
- td height="30" align="right"> td>
- td>input type="submit" name="Submit" value="提交" />td>
- tr>
- tr>
- td height="20" align="right"> td>
- td> td>
- tr>
- table>
- form>
- body>
- html>
conn.php:
- $id=mysql_connect("localhost","root","mysql") or die('连接失败:' . mysql_error());
- if(mysql_select_db("phpjcdb",$id)) //说明:phpjcdb 是数据库名称
- echo "";
- else
- echo ('数据库选择失败:' . mysql_error());
- mysql_query("set names gb2312"); //设置为简体中文
- ?>
index_ok.php:
- session_start();
- include("conn.php");
- if($_POST['Submit']==true){
- $null=$_POST[null];
- $table = $_POST['table'];
- $field = $_POST['field'];
- $type = $_POST['type'];
- $length = $_POST['length'];
- $mysql=mysql_query("alter table $table add $field $type($length) $null");
- echo mysql_error();
- if($mysql==true){
- echo "字段添加成功!";
- }else{echo "添加失败!";}
- }
- ?>
上一篇: PHP编程与应用(1)_PHP