html5中文学习网

您的位置: 首页 > 网络编程 > PHP编程 » 正文

PHP操作数据库 动态创建字段_PHP教程_编程技术

[ ] 已经帮助:人解决问题

PHP操作数据库时自动创建字段,如下代码:AKQHTML5中文学习网 - HTML5先行者学习网

  1. <html> 
  2. <head> 
  3. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
  4. <title>动态创建字段 - www.cxybl.com</title> 
  5. <style type="text/css"> 
  6. <!-- 
  7. .STYLE1 {font-size: 12px} 
  8. --> 
  9. </style> 
  10. </head> 
  11.  
  12. <body> 
  13. <form id="form1" name="form1" method="post" action="index_ok.php"> 
  14.   <table width="467" height="289" border="0" cellpadding="0" cellspacing="0"> 
  15.     <tr> 
  16.       <td width="168" height="89"> </td> 
  17.       <td width="289"> </td> 
  18.     </tr> 
  19.     <tr> 
  20.       <td height="30" align="right"><span class="STYLE1">选择表 </span></td> 
  21.       <td class="STYLE1"><input name="table" type="text" id="table" size="20" /></td> 
  22.     </tr> 
  23.     <tr> 
  24.       <td height="30" align="right"><span class="STYLE1">字段 </span></td> 
  25.       <td class="STYLE1"><input name="field" type="text" id="field" size="20"></td> 
  26.     </tr> 
  27.     <tr> 
  28.       <td height="30" align="right"><span class="STYLE1">类型 </span></td> 
  29.       <td class="STYLE1"><select name="type" id="type"> 
  30.         <option>int</option> 
  31.         <option>text</option> 
  32.         <option>date</option> 
  33.         <option>double</option> 
  34.         <option>varchar</option> 
  35.         <option>datetime</option> 
  36.         <option>blob</option> 
  37.       </select>      </td> 
  38.     </tr> 
  39.     <tr> 
  40.       <td height="30" align="right"><span class="STYLE1">长度 </span></td> 
  41.       <td class="STYLE1"><input name="length" type="text" id="length" size="15"></td> 
  42.     </tr> 
  43.     <tr> 
  44.       <td height="30" align="right"><span class="STYLE1">NULL </span></td> 
  45.       <td class="STYLE1"><input type="radio" name="null" value="null"> 
  46.         null 
  47.         <input type="radio" name="null" value="not null"> 
  48.         not null </td> 
  49.     </tr> 
  50.     <tr> 
  51.       <td height="30" align="right"> </td> 
  52.       <td><input type="submit" name="Submit" value="提交" /></td> 
  53.     </tr> 
  54.     <tr> 
  55.       <td height="20" align="right"> </td> 
  56.       <td> </td> 
  57.     </tr> 
  58.   </table> 
  59. </form> 
  60. </body> 
  61. </html> 

conn.php:AKQHTML5中文学习网 - HTML5先行者学习网

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

index_ok.php:AKQHTML5中文学习网 - HTML5先行者学习网

  1. <?php  
  2. session_start();  
  3. include("conn.php"); 
  4. if($_POST['Submit']==true){ 
  5.   $null=$_POST[null]; 
  6.   $table = $_POST['table']; 
  7.   $field = $_POST['field']; 
  8.   $type = $_POST['type']; 
  9.   $length = $_POST['length']; 
  10.   $mysql=mysql_query("alter table $table add $field $type($length) $null"); 
  11.     echo mysql_error(); 
  12.   if($mysql==true){ 
  13.     echo "字段添加成功!"
  14.   }else{echo "添加失败!";} 
  15. ?> 
AKQHTML5中文学习网 - HTML5先行者学习网
AKQHTML5中文学习网 - HTML5先行者学习网
(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助