mysql php,表单,连接数据库,字符编码问题

表单:

   1: <html >
   2: <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
   3: <body>
   4: <form action="phone2pc_do.php" method="post">
   5: 请输入:
   6: <center>
   7: <input type="text" name="phone2pc_txt"  />
   8: </center>
   9: <input type="submit" />
  10: </form>
  11: </body>
  12: </html>

表单连接数据:

   1: <html>
   2: <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
   3: <body>
   4: <br>
   5: <?php
   6: $con = mysql_connect("127.0.0.1","db_name","db_pw");        //连接数据库,db_name,db_pw为连接数据库的用户名和密码
   7: if (!$con)
   8:   {
   9:   die('Could not connect: ' . mysql_error());
  10:   }
  11: mysql_select_db("phone2pc", $con);                                //选择要用的数据库
  12: mysql_query("set names gb2312");                                //设置字符集
  13:  
  14: $sql="INSERT INTO phone2pc (content) VALUES ('$_POST[phone2pc_txt]')";            //查询的mysql语句
  15:  
  16:  
  17: if (!mysql_query($sql,$con))                                        //执行查询
  18:   {
  19:   die('Error: ' . mysql_error());
  20:   }
  21: echo "sucess:&lt;br>";
  22: echo $_POST["phone2pc_txt"];
  23:  
  24: mysql_close($con)                                                    //关闭连接.
  25: ?>
  26:  
  27: <br />
  28: </body>
  29: </html>

问题:

               mysql数据库的Query failed: Incorrect string value:

解决:

几处处理字符编码的问题:

1.mysql安装目录下my.ini的字符集设置:

image

2.字段的创建的时候选择的编码:

image

3.网页的编码:

image

4.操作数据库前设置编码:

image

2013年4月2日 19:24:03

tag 数据库 mysql 表单 字符编码.


原文地址:https://www.cnblogs.com/xilifeng/p/2996301.html