php将表单中数据传入到数据库

 1 <html>
 2 <head>
 3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 4 <title>无标题文档</title>
 5 </head>
 6 
 7 <body>
 8 <form action="" method="post">//创建表单
 9 姓名
10 <input type="text" name="str">
11 <input type="submit" name="submit">
12 </form>
13 <?php
14 header("Content-type:text/html;charset=utf-8");
15 
16 if(isset($_POST['submit']))//确认是否单击提交按钮
17 {
18 $name=$_POST['str'];//通过post方式获取表单数据并存入到$name变量中
19 $link=mysql_connect("localhost","root","207207");//链接数据库
20     if($link)
21     {
22         echo"连接数据库成功!";
23         mysql_query('SET NAMES UTF8');//设置数据库编码解决无法输入中文的问题
24         $sel=mysql_select_db("db_student");//选择数据库
25             if($sel)
26             {
27                 echo"选择数据库成功";
28                 $str="insert into tb_student values(".""".$name.""".");";//mysql数据库语句
29                 mysql_query($str);//执行mysql语句
30             }
31             else
32             {
33                 echo"选择数据库失败";
34             }
35     }
36     else
37     {
38         echo"连接数据库失败!";
39         exit;
40     }
41     
42 }
43 ?>
44 
45 </body>
46 </html>

从数据库中输出也是同样道理

原文地址:https://www.cnblogs.com/itsuibi/p/5420084.html