首页 >后端开发 >php教程 >PHP如何从页面向数据库插入信息的示例代码详解

PHP如何从页面向数据库插入信息的示例代码详解

黄舟
黄舟原创
2017-03-13 16:34:502218浏览

PHP页面

 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
//初始化act
$_REQUEST[&#39;act&#39;]=$_REQUEST[&#39;act&#39;]?$_REQUEST[&#39;act&#39;]:&#39;&#39;;
if($_REQUEST[&#39;act&#39;]==&#39;add&#39;){
 include &#39;templates/zhiliang_add.php&#39;;
 }elseif($_REQUEST[&#39;act&#39;]==&#39;insert&#39;){       //文件上传  
     //var_dump($new_file_name);
     $title=$_POST[&#39;title&#39;];
     $content=$_POST[&#39;content&#39;];
     $time=$_POST[&#39;time&#39;];
     include &#39;db.class.php&#39;;
     $db=new db(&#39;localhost&#39;,&#39;root&#39;,&#39;123&#39;,&#39;fanyi&#39;);
     $sql="insert into zhiliang values(null,&#39;$title&#39;,&#39;$content&#39;,&#39;$time&#39;)";
     if(mysql_query($sql)){
       echo "添加成功"; 
      }     
     }

--------------------------------------------------------------------------------------------------------

HTML页面

<form method="post" action="zhiliang.php" name="theForm" enctype="multipart/form-data" onsubmit="return validate()">
<table cellspacing="1" cellpadding="3" width="100%">
  <tr>
    <td class="label">标题</td>
    <td><input type="text" name="title" maxlength="60" value="" /><span class="require-field">*</span></td>
  </tr>
  <tr>
    <td class="label">时间</td>
    <td><input type="text" name="time" maxlength="60" size="40" value="" /></td>
  </tr>
   <tr>
    <td class="label">文章内容</td>
    <td><textarea  name="content" cols="60" rows="4"  ></textarea></td>
  </tr>
 
  <tr>
    <td colspan="2" align="center"><br />
      <input type="submit" class="button" value=" 确定 " />
      <input type="reset" class="button" value=" 重置 " />
    </td>
  </tr>
</table>
</form>

 

以上是PHP如何从页面向数据库插入信息的示例代码详解的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn