php向mysqli中插入数据字母和数子可以成功写入,汉字插入最后会变成数子

    技术2022-07-16  70

    求助:php向mysqli中插入数据字母和数子可以成功写入,汉字插入最后会变成数子

    数据库php向数据库插入的代码向表单元素文本框中输入汉字提交,最后到数据库的表中都会变成如下图:输汉字插入数据库后的写入内容就会变成数子init.php文件(连接数据库)articleUpdate.php文件articlePublishDo.php文件articlePublish.php文件articleList.php文件articleEdit.php文件articleDelete.php文件希望大神能帮忙解决一下

    数据库

    php向数据库插入的代码

    <?php header('Content-type:text/html;charset=utf-8' );//设置php代码输出文件流编码格式 require_once 'init.php'; //获取连接数据库文件 $title=trim(isset($_POST['title'])); //获取表单元素名为title的表单元素 $author=trim(isset($_POST['author'])); //获取表单元素名为author的表单元素 $content=trim(isset($_POST['content'])); //获取表单元素名为content的表单元素 //向数据库插入数据,并把结果集返还给变量aql $sql="insert into article(title,author,content,create_time) values('$title','$author','$content',NOW())"; $re=mysqli_query($conn,$sql); //执行上一条sql语句 //判断是否插入成功。成功输出发布成功,反之输出发布失败 if($re){ echo '<div>发布成功</div>'; echo '<a href="articleList.php">返回文章列表</a>'; }else{ echo '<div>发布失败</div>'; echo '<a href="articleList.php">返回文章列表</a>'; } // mysql_close();//关闭数据库 ?>

    向表单元素文本框中输入汉字提交,最后到数据库的表中都会变成如下图:

    输汉字

    插入数据库后的写入内容就会变成数子

    init.php文件(连接数据库)

    <?php $conn=mysqli_connect('localhost', 'root', '123456', 'test');//链接数据库 //var_dump($conn); //mysqli_set_charset($conn,'utf8'); ?>

    articleUpdate.php文件

    <!doctype html> <html> <head> <meta charset="utf-8"> <title></title> <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" /> <style type="text/css"> .bg { background-image:url("img/bg.png"); style=" background-repeat:no-repeat ; background-size:100% 100%; background-attachment: fixed;" } a{ text-decoration:none } a:hover{ text-shadow: 0 8px 9px #c4b59d, 10px 10px 5px #fff; } .b1{ margin:auto; width: 50%; padding-top:10% ; font-size:80px; color:#f1ebe5; } </style> </head> <body class="bg"> <div class="b1"> <?php header('Content-type:text/html;charset=utf-8'); require_once 'init.php'; $arr=$_POST; //打印关联数组 $id=(int)$arr['id']; $sql="update article set title = '$arr[title]',content = '$arr[content]' where id = '$id'"; //echo $sql; $re=mysqli_query($conn,$sql);//执行sql语句 if($re){ echo "修改成功"; echo "<a href='articleList.php'>返回文章列表</a>"; }else{ echo "修改失败"; echo "<a href='articleList.php'>返回文章列表</a>"; } ?> </div> </body> </html>

    articlePublishDo.php文件

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> <!--设置页面背景图片--> .bg { background-image:url("img/bg.png"); style=" background-repeat:no-repeat ; background-size:100% 100%; background-attachment: fixed;" } <!--设置类名为b1的标签,使其水平居中对齐, 顶部内边距为父元素的百分之10, 字体大小为80px, 颜色为白色--> .b1{ margin:auto; width: 50%; padding-top:10% ; font-size:80px; color:#f1ebe5; } <!--设置超链接样式,取消下画线,鼠标移动到超连接时超链接出现阴影--> a{ text-decoration:none } a:hover{ text-shadow: 0 8px 9px #c4b59d, 10px 10px 5px #fff; } </style> </head> <body class="bg"> <div class="b1"> <?php header('Content-type:text/html;charset=utf-8' );//设置php代码输出文件流编码格式 require_once 'init.php'; //获取连接数据库文件 $title=trim(isset($_POST['title'])); //获取表单元素名为title的表单元素 $author=trim(isset($_POST['author'])); //获取表单元素名为author的表单元素 $content=trim(isset($_POST['content'])); //获取表单元素名为content的表单元素 //向数据库插入数据,并把结果集返还给变量aql $sql="insert into article(title,author,content,create_time) values('$title','$author','$content',NOW())"; $re=mysqli_query($conn,$sql); //执行上一条sql语句 //判断是否插入成功。成功输出发布成功,反之输出发布失败 if($re){ echo '<div>发布成功</div>'; echo '<a href="articleList.php">返回文章列表</a>'; }else{ echo '<div>发布失败</div>'; echo '<a href="articleList.php">返回文章列表</a>'; } // mysql_close();//关闭数据库 ?> </div> </body> </html>

    articlePublish.php文件

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd> <HTML lang="en"> <Head> <title>留言</title> <style type="text/css"> <!--设置表格边顶部外边距 --> .message{ margin-top:0px; } <!--设置页面背景图片--> .bg { background-image:url("img/bg.png"); style=" background-repeat:no-repeat ; background-size:100% 100%; background-attachment: fixed;" } <!--设置留言板标题字体样式,字体风格,字体大小,字体颜色,阴影效果 --> .text{ font-family:"经典圆体繁", "microsoft yahei", "Arial Rounded MT Bold", "Helvetica Rounded", Arial, sans-serif;; font-size: 80px; color: #f1ebe5; text-shadow: 0 8px 9px #c4b59d, 0px -2px 1px #fff; /*设置阴影*/ } <!--设置td标签下的字体颜色 --> td{ color:#f1ebe5; } </style> </Head> <Body class="bg"> <h1 align="center" class="text">留言板</h1> <div class="message"> <form name="article" method="post" action="articlePublishDo.php"> <table type="text" align="center" border="1px,solid"> <tr> <td>标题</td> <td><input type="text" name="title" /></td> </tr> <tr> <td>作者</td> <td><input type="text" name="author"/> </td> </tr> <tr> <td>内容</td> <td><textarea name="content" cols="60" role="15"></textarea></td> </tr> <tr> <td><input type="submit" name="sumbit"/></td> <td><input type="reset" name="reset"/></td> </tr> </table> </form> </div> </Body> </HTML>

    articleList.php文件

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf8"> <title>Insert title here</title> <style type="text/css"> .bg { background-image:url("img/bg.png"); style=" background-repeat:no-repeat ; background-size:100% 100%; background-attachment: fixed;" } .c{ width:80%; } .b1{ font-size:40px; margin:0 auto; width: 40%; font-weight:900; padding :20px; } .b2{ font-size:40px; margin:auto; width: 50%; font-weight:900; } a{ text-decoration:none } .b2 a{ padding-left:80px; } .b3{ font-size:40px; margin:auto; width: 20%; font-weight:900; } a:hover{ text-shadow: 0 8px 9px #c4b59d, 10px 10px 5px #fff; } </style> </head> <body class="bg"> <!-- 搜索框 --> <div class="b"> <form method="post" action="articleList.php" > <input class="c" type="text" name="search"/> <input class="c2" type="submit" value="搜索"/> </form> </div> <br/> <table cellspacing="0" cellpadding="0" align="center" bgcolor="#ccccccc" width=500 > <tr> <th>编号</th> <th>文章标题</th> <th>作者</th> <th>文章内容</th> <th>编辑文章</th> </tr> <?php //header('Content-type:text/html;charset=utf-8' ); require_once 'init.php'; /** * 搜索 */ $keyword=@$_POST['search']; //如何获取当前search的值,需要错误抑制 /*分页*/ $sql1="SELECT * FROM article WHERE title LIKE '{$keyword}'"; $res=mysqli_query($conn,$sql1); $count=(int)mysqli_num_rows($res); //返回结果集的数量 $pageSize=3;//最大页码 $page=floor($count/$pageSize)+1;//总页数$page if(@isset($_GET['page'])) //判断当前数值是否非空 { //$currentPage = $_GET['page']; if(@$_GET['page'] <=1){ $currentPage = 1; }elseif (@$_GET['page'] >= $page){ $currentPage = $page-1; }else{ $currentPage = @$_GET['page']; } }else { $currentPage=1; } $start = ($currentPage-1)*$pageSize; $sql="SELECT * FROM article WHERE title LIKE '{$keyword}'"; //echo $sql; $re=mysqli_query($conn,$sql);//确认结果集 while($arr=mysqli_fetch_assoc($re)){ //遍历表格 ?> <tr> <td align="center" style="border:1px solid #000"><?php echo $arr['id'];?></td> <input type="hidden" name="id" value="<?php echo $arr['id'];?>"/> <td align="center" style="border:1px solid #000"><?php echo $arr['title'];?></td> <td align="center" style="border:1px solid #000"><?php echo $arr['author'];?></td> <td align="center" style="border:1px solid #000"><?php echo $arr['content'];?></td> <td align="center" style="border:1px solid #000"> <a href="articleEdit.php?id=<?php echo $arr['id']?>"><font color="red">修改</font></a> <a href="articleDelete.php?id=<?php echo $arr['id']?>"><font color="red">删除</font></a> </td> </tr> <?php } ?> </table> <div class="b1"><?php echo $page?>&nbsp &nbsp&nbsp查到<?php echo $count;?>条记录&nbsp&nbsp 当前&nbsp&nbsp第<?php echo @$_GET['page']?></div> <div class="b2"> <a href="articleList.php?page=1&search=<?php echo $keyword?>">首页</a> <a href="articleList.php?page=<?php echo ($currentPage-1)?>&search=<?php echo $keyword?>">上一页</a> <a href="articleList.php?page=<?php echo ($currentPage+1)?>&search=<?php echo $keyword?>">下一页</a> <a href="articleList.php?page=<?php echo $page?>&search=<?php echo $keyword?>">末页</a> </div> <div class="b3"> <a href="articlePublish.php" >返回发布文章</a> </div> </body> </html>

    articleEdit.php文件

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf8"> <title>留言修改</title> <style type="text/css"> <!--设置页面背景图片--> .bg { background-image:url("img/bg.png"); style=" background-repeat:no-repeat ; background-size:100% 100%; background-attachment: fixed;" } <!--设置主标题样式字体风格,字体大小,字体颜色,阴影效果--> .text{ font-family:"经典圆体繁", "microsoft yahei", "Arial Rounded MT Bold", "Helvetica Rounded", Arial, sans-serif;; font-size: 50px; color: #f1ebe5; text-shadow: 0 8px 9px #c4b59d, 0px -2px 1px #fff; /*设置阴影*/ } <!--设置类名为吧b1元素的字体大小,字体粗细,控件宽度,内边距--> .b1{ font-size:40px; margin:0 auto; width: 40%; font-weight:900; padding :20px; } <!--设置类名为吧b2元素的字体大小,字体粗细,水平剧中效果的实现--> .b2{ font-size:40px; margin:auto; width: 50%; font-weight:900; } <!--设置超连接样式,去除下划线--> a{ text-decoration:none } <!--设置类名为吧b3元素的字体大小,字体粗细,水平剧中效果的实现--> .b3{ font-size:40px; margin:auto; width: 20%; font-weight:900; } <!--设置超连接样式,设置鼠标悬停时候的阴影效果--> a:hover{ text-shadow: 0 8px 9px #c4b59d, 10px 10px 5px #fff; } <!--设置类名为吧c1元素的字体大小,水平剧中效果的实现--> .c1{ padding:20px; margin: auto; height: 10%; } <!--设置类名为吧c3元素的内边距,水平剧中效果的实现--> .c3{ padding:20px; margin: auto; width: 20%; } </style> </head> <body class="bg"> <?php $id=(int)$_GET['id']; //获取id require_once 'init.php'; //导入数据库连接文件 $sql="select id,title,content from article where id = '$id'"; //echo $sql; $re=mysqli_query($conn,$sql); //执行sql语句 $arr=mysqli_fetch_assoc($re); //从结果集中返回关联数组 ?> <!--页面html结构--> <form name="article" method="post" action="articleUpdate.php" style="margin:5px 500px;"class="b1" > <h1 class="text">留言修改</h1> <input type="hidden" name="id" value="<?php echo $arr['id']?>"/><br/> 标题:<input type="text" name="title" class="c1" value="<?php echo $arr['title']?>"/><br/> 内容:<textarea cols=30 rows=5 name="content" class="c2"><?php echo $arr['content']?></textarea><br/><br/> <input type="submit" value="修改文章" class="c3" /> <a href="articleList.php">返回文章列表</a> <a href="articlePublish.html">返回发布文章</a> </form> </body> </html>

    articleDelete.php文件

    ```php <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd> <html> <head> <meta charset="utf-8"> <title>删除留言</title> <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" /> <style type="text/css"> <!--设置页面背景图片--> .bg { background-image:url("img/bg.png"); style=" background-repeat:no-repeat ; background-size:100% 100%; background-attachment: fixed;" } <!--设置类名为吧b1元素的字体大小,字体粗细,控件宽度,内边距--> .b1{ font-size:40px; margin:0 auto; width: 40%; font-weight:900; padding-top :15%; } <!--设置超连接样式,,删除下划线,设置鼠标悬停时候的阴影效果--> a{ text-decoration:none } a:hover{ text-shadow: 0 8px 9px #c4b59d, 10px 10px 5px #fff; } </style> </head> <body class="bg"> <div class="b1"> <?php header('Content-type:text/html;charset=utf-8');//设置编码格式 require_once 'init.php'; $id=(int)$_GET['id']; //获取当前的id $sql="delete from article where id = '$id'"; //判断操作是否成功 $re=mysqli_query($conn,$sql); if($re){ echo "删除成功"; echo "<a href='articleList.php'>返回文章列表</a>"; }else{ echo "删除失败"; echo "<a href='articleList.php'>返回文章列表</a>"; } ?> </div> </body> </html>

    希望大神能帮忙解决一下

    Processed: 0.010, SQL: 9