Rumah >php教程 >php手册 >PHP操作数据库 动态创建字段

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

WBOY
WBOYasal
2016-06-21 08:52:381396semak imbas

PHP操作数据库时自动创建字段,如下代码:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span class="tag-name">html</span><span class="tag">></span><span> </span></span></span></li>
<li><span><span class="tag"><span class="tag-name">head</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span><span class="tag"><span class="tag-name">meta</span><span> </span><span class="attribute">http-equiv</span><span>=</span><span class="attribute-value">"Content-Type"</span><span> </span><span class="attribute">content</span><span>=</span><span class="attribute-value">"text/html; charset=gb2312"</span><span> </span><span class="tag">/></span><span> </span></span></span></li>
<li><span><span class="tag"><span class="tag-name">title</span><span class="tag">></span><span>动态创建字段 - www.cxybl.com</span><span class="tag"></span><span class="tag-name">title</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span><span class="tag"><span class="tag-name">style</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text/css"</span><span class="tag">></span><span> </span></span></span></li>
<li><span><span class="comments"><!--</span> </span></li><li class="alt"><span><span class="comments">.STYLE1 {font-size: 12px}</span> </span></li><li><span><span class="comments">--></span><span> </span></span></li>
<li class="alt"><span><span class="tag"></span><span class="tag-name">style</span><span class="tag">></span><span> </span></span></li>
<li><span><span class="tag"></span><span class="tag-name">head</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span> </span></li>
<li><span><span class="tag"><span class="tag-name">body</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span><span class="tag"><span class="tag-name">form</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">"form1"</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"form1"</span><span> </span><span class="attribute">method</span><span>=</span><span class="attribute-value">"post"</span><span> </span><span class="attribute">action</span><span>=</span><span class="attribute-value">"index_ok.php"</span><span class="tag">></span><span> </span></span></span></li>
<li><span>  <span class="tag"><span class="tag-name">table</span><span> </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"467"</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"289"</span><span> </span><span class="attribute">border</span><span>=</span><span class="attribute-value">"0"</span><span> </span><span class="attribute">cellpadding</span><span>=</span><span class="attribute-value">"0"</span><span> </span><span class="attribute">cellspacing</span><span>=</span><span class="attribute-value">"0"</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span>    <span class="tag"><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></span></li>
<li><span>      <span class="tag"><span class="tag-name">td</span><span> </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"168"</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"89"</span><span class="tag">></span><span> </span><span class="tag"></span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span>      <span class="tag"><span class="tag-name">td</span><span> </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"289"</span><span class="tag">></span><span> </span><span class="tag"></span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></span></li>
<li><span>    <span class="tag"></span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span>    <span class="tag"><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></span></li>
<li><span>      <span class="tag"><span class="tag-name">td</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"30"</span><span> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"right"</span><span class="tag">></span><span class="tag"><span class="tag-name">span</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"STYLE1"</span><span class="tag">></span><span>选择表 </span><span class="tag"></span><span class="tag-name">span</span><span class="tag">></span><span class="tag"></span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></span></span></li>
<li class="alt"><span>      <span class="tag"><span class="tag-name">td</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"STYLE1"</span><span class="tag">></span><span class="tag"><span class="tag-name">input</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"table"</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text"</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">"table"</span><span> </span><span class="attribute">size</span><span>=</span><span class="attribute-value">"20"</span><span> </span><span class="tag">/></span><span class="tag"></span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></span></span></li>
<li><span>    <span class="tag"></span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span>    <span class="tag"><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></span></li>
<li><span>      <span class="tag"><span class="tag-name">td</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"30"</span><span> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"right"</span><span class="tag">></span><span class="tag"><span class="tag-name">span</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"STYLE1"</span><span class="tag">></span><span>字段 </span><span class="tag"></span><span class="tag-name">span</span><span class="tag">></span><span class="tag"></span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></span></span></li>
<li class="alt"><span>      <span class="tag"><span class="tag-name">td</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"STYLE1"</span><span class="tag">></span><span class="tag"><span class="tag-name">input</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"field"</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text"</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">"field"</span><span> </span><span class="attribute">size</span><span>=</span><span class="attribute-value">"20"</span><span class="tag">></span><span class="tag"></span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></span></span></li>
<li><span>    <span class="tag"></span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span>    <span class="tag"><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></span></li>
<li><span>      <span class="tag"><span class="tag-name">td</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"30"</span><span> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"right"</span><span class="tag">></span><span class="tag"><span class="tag-name">span</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"STYLE1"</span><span class="tag">></span><span>类型 </span><span class="tag"></span><span class="tag-name">span</span><span class="tag">></span><span class="tag"></span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></span></span></li>
<li class="alt"><span>      <span class="tag"><span class="tag-name">td</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"STYLE1"</span><span class="tag">></span><span class="tag"><span class="tag-name">select</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"type"</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">"type"</span><span class="tag">></span><span> </span></span></span></span></li>
<li><span>        <span class="tag"><span class="tag-name">option</span><span class="tag">></span><span>int</span><span class="tag"></span><span class="tag-name">option</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span>        <span class="tag"><span class="tag-name">option</span><span class="tag">></span><span>text</span><span class="tag"></span><span class="tag-name">option</span><span class="tag">></span><span> </span></span></span></li>
<li><span>        <span class="tag"><span class="tag-name">option</span><span class="tag">></span><span>date</span><span class="tag"></span><span class="tag-name">option</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span>        <span class="tag"><span class="tag-name">option</span><span class="tag">></span><span>double</span><span class="tag"></span><span class="tag-name">option</span><span class="tag">></span><span> </span></span></span></li>
<li><span>        <span class="tag"><span class="tag-name">option</span><span class="tag">></span><span>varchar</span><span class="tag"></span><span class="tag-name">option</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span>        <span class="tag"><span class="tag-name">option</span><span class="tag">></span><span>datetime</span><span class="tag"></span><span class="tag-name">option</span><span class="tag">></span><span> </span></span></span></li>
<li><span>        <span class="tag"><span class="tag-name">option</span><span class="tag">></span><span>blob</span><span class="tag"></span><span class="tag-name">option</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span>      <span class="tag"></span><span class="tag-name">select</span><span class="tag">></span><span>      </span><span class="tag"></span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li>
<li><span>    <span class="tag"></span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span>    <span class="tag"><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></span></li>
<li><span>      <span class="tag"><span class="tag-name">td</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"30"</span><span> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"right"</span><span class="tag">></span><span class="tag"><span class="tag-name">span</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"STYLE1"</span><span class="tag">></span><span>长度 </span><span class="tag"></span><span class="tag-name">span</span><span class="tag">></span><span class="tag"></span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></span></span></li>
<li class="alt"><span>      <span class="tag"><span class="tag-name">td</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"STYLE1"</span><span class="tag">></span><span class="tag"><span class="tag-name">input</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"length"</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text"</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">"length"</span><span> </span><span class="attribute">size</span><span>=</span><span class="attribute-value">"15"</span><span class="tag">></span><span class="tag"></span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></span></span></li>
<li><span>    <span class="tag"></span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span>    <span class="tag"><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></span></li>
<li><span>      <span class="tag"><span class="tag-name">td</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"30"</span><span> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"right"</span><span class="tag">></span><span class="tag"><span class="tag-name">span</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"STYLE1"</span><span class="tag">></span><span>NULL </span><span class="tag"></span><span class="tag-name">span</span><span class="tag">></span><span class="tag"></span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></span></span></li>
<li class="alt"><span>      <span class="tag"><span class="tag-name">td</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"STYLE1"</span><span class="tag">></span><span class="tag"><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"radio"</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"null"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"null"</span><span class="tag">></span><span> </span></span></span></span></li>
<li><span>        null </span></li>
<li class="alt"><span>        <span class="tag"><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"radio"</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"null"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"not null"</span><span class="tag">></span><span> </span></span></span></li>
<li><span>        not null <span class="tag"></span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span>    <span class="tag"></span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li>
<li><span>    <span class="tag"><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span>      <span class="tag"><span class="tag-name">td</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"30"</span><span> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"right"</span><span class="tag">></span><span> </span><span class="tag"></span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></span></li>
<li><span>      <span class="tag"><span class="tag-name">td</span><span class="tag">></span><span class="tag"><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"submit"</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"Submit"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"提交"</span><span> </span><span class="tag">/></span><span class="tag"></span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></span></span></li>
<li class="alt"><span>    <span class="tag"></span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li>
<li><span>    <span class="tag"><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span>      <span class="tag"><span class="tag-name">td</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"20"</span><span> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"right"</span><span class="tag">></span><span> </span><span class="tag"></span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></span></li>
<li><span>      <span class="tag"><span class="tag-name">td</span><span class="tag">></span><span> </span><span class="tag"></span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span>    <span class="tag"></span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li>
<li><span>  <span class="tag"></span><span class="tag-name">table</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span><span class="tag"></span><span class="tag-name">form</span><span class="tag">></span><span> </span></span></li>
<li><span><span class="tag"></span><span class="tag-name">body</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span><span class="tag"></span><span class="tag-name">html</span><span class="tag">></span><span> </span></span></li>
</ol>

conn.php:

<ol class="dp-c">
<li class="alt"><span><span><?php   </span></span></span></li>
<li><span><span class="vars">$id</span><span>=mysql_connect(</span><span class="string">"localhost"</span><span>,</span><span class="string">"root"</span><span>,</span><span class="string">"mysql"</span><span>) </span><span class="keyword">or</span><span> </span><span class="keyword">die</span><span>(</span><span class="string">'连接失败:'</span><span> . mysql_error()); </span></span></li>
<li class="alt"><span><span class="keyword">if</span><span>(mysql_select_db(</span><span class="string">"phpjcdb"</span><span>,</span><span class="vars">$id</span><span>))  </span><span class="comment">//说明:phpjcdb 是数据库名称</span><span> </span></span></li>
<li><span>  <span class="func">echo</span><span> </span><span class="string">""</span><span>; </span></span></li>
<li class="alt"><span>  <span class="keyword">else</span><span> </span></span></li>
<li><span>  <span class="func">echo</span><span> (</span><span class="string">'数据库选择失败:'</span><span> . mysql_error()); </span></span></li>
<li class="alt"><span>mysql_query(<span class="string">"set names gb2312"</span><span>);  </span><span class="comment">//设置为简体中文</span><span> </span></span></li>
<li><span>?> </span></li>
</ol>

index_ok.php:

<ol class="dp-c">
<li class="alt"><span><span><?php   </span></span></span></li>
<li><span>session_start();  </span></li>
<li class="alt"><span><span class="keyword">include</span><span>(</span><span class="string">"conn.php"</span><span>); </span></span></li>
<li><span><span class="keyword">if</span><span>(</span><span class="vars">$_POST</span><span>[</span><span class="string">'Submit'</span><span>]==true){ </span></span></li>
<li class="alt"><span>  <span class="vars">$null</span><span>=</span><span class="vars">$_POST</span><span>[null]; </span></span></li>
<li><span>  <span class="vars">$table</span><span> = </span><span class="vars">$_POST</span><span>[</span><span class="string">'table'</span><span>]; </span></span></li>
<li class="alt"><span>  <span class="vars">$field</span><span> = </span><span class="vars">$_POST</span><span>[</span><span class="string">'field'</span><span>]; </span></span></li>
<li><span>  <span class="vars">$type</span><span> = </span><span class="vars">$_POST</span><span>[</span><span class="string">'type'</span><span>]; </span></span></li>
<li class="alt"><span>  <span class="vars">$length</span><span> = </span><span class="vars">$_POST</span><span>[</span><span class="string">'length'</span><span>]; </span></span></li>
<li><span>  <span class="vars">$mysql</span><span>=mysql_query(</span><span class="string">"alter table $table add $field $type($length) $null"</span><span>); </span></span></li>
<li class="alt"><span>    <span class="func">echo</span><span> mysql_error(); </span></span></li>
<li><span>  <span class="keyword">if</span><span>(</span><span class="vars">$mysql</span><span>==true){ </span></span></li>
<li class="alt"><span>    <span class="func">echo</span><span> </span><span class="string">"字段添加成功!"</span><span>; </span></span></li>
<li><span>  }<span class="keyword">else</span><span>{</span><span class="func">echo</span><span> </span><span class="string">"添加失败!"</span><span>;} </span></span></li>
<li class="alt"><span>} </span></li>
<li><span>?> </span></li>
</ol>



Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:PHP array_pop() 函数Artikel seterusnya:PHP限制文件上传的大小