PHP操作数据库时自动创建字段,如下代码:
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
- <title>动态创建字段 - www.cxybl.com</title>
- <style type="text/css">
- <!--
- .STYLE1 {font-size: 12px}
- -->
- </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>int</option>
- <option>text</option>
- <option>date</option>
- <option>double</option>
- <option>varchar</option>
- <option>datetime</option>
- <option>blob</option>
- </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:
- <?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:
- <?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
, null
, 动态
字段
数据库动态字段设计、数据库 动态字段、数据库动态字段扩展、数据库动态添加字段、数据库创建字段,以便于您获取更多的相关知识。
时间: 2024-12-02 19:40:31