欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

PHP操作数据库 动态创建字段

程序员文章站 2022-06-06 09:14:24
...
PHP操作数据库时自动创建字段,如下代码:
  1. html>
  2. head>
  3. meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  4. title>动态创建字段 - www.cxybl.comtitle>
  5. style type="text/css">
  6. style>
  7. head>
  8. body>
  9. form id="form1" name="form1" method="post" action="index_ok.php">
  10. table width="467" height="289" border="0" cellpadding="0" cellspacing="0">
  11. tr>
  12. td width="168" height="89"> td>
  13. td width="289"> td>
  14. tr>
  15. tr>
  16. td height="30" align="right">span class="STYLE1">选择表 span>td>
  17. td class="STYLE1">input name="table" type="text" id="table" size="20" />td>
  18. tr>
  19. tr>
  20. td height="30" align="right">span class="STYLE1">字段 span>td>
  21. td class="STYLE1">input name="field" type="text" id="field" size="20">td>
  22. tr>
  23. tr>
  24. td height="30" align="right">span class="STYLE1">类型 span>td>
  25. td class="STYLE1">select name="type" id="type">
  26. option>intoption>
  27. option>textoption>
  28. option>dateoption>
  29. option>doubleoption>
  30. option>varcharoption>
  31. option>datetimeoption>
  32. option>bloboption>
  33. select> td>
  34. tr>
  35. tr>
  36. td height="30" align="right">span class="STYLE1">长度 span>td>
  37. td class="STYLE1">input name="length" type="text" id="length" size="15">td>
  38. tr>
  39. tr>
  40. td height="30" align="right">span class="STYLE1">NULL span>td>
  41. td class="STYLE1">input type="radio" name="null" value="null">
  42. null
  43. input type="radio" name="null" value="not null">
  44. not null td>
  45. tr>
  46. tr>
  47. td height="30" align="right"> td>
  48. td>input type="submit" name="Submit" value="提交" />td>
  49. tr>
  50. tr>
  51. td height="20" align="right"> td>
  52. td> td>
  53. tr>
  54. table>
  55. form>
  56. body>
  57. html>

conn.php:

  1. $id=mysql_connect("localhost","root","mysql") or die('连接失败:' . mysql_error());
  2. if(mysql_select_db("phpjcdb",$id)) //说明:phpjcdb 是数据库名称
  3. echo "";
  4. else
  5. echo ('数据库选择失败:' . mysql_error());
  6. mysql_query("set names gb2312"); //设置为简体中文
  7. ?>

index_ok.php:

  1. session_start();
  2. include("conn.php");
  3. if($_POST['Submit']==true){
  4. $null=$_POST[null];
  5. $table = $_POST['table'];
  6. $field = $_POST['field'];
  7. $type = $_POST['type'];
  8. $length = $_POST['length'];
  9. $mysql=mysql_query("alter table $table add $field $type($length) $null");
  10. echo mysql_error();
  11. if($mysql==true){
  12. echo "字段添加成功!";
  13. }else{echo "添加失败!";}
  14. }
  15. ?>