<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>模式</title>
<link rel="stylesheet" href="css/common.css" type="text/css" media="screen" />
<script type="text/javascript" src="jquery/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="xheditor-zh-cn.js"></script>
</head>
<body>
<form action="add.php" method="post">
新闻标题:<input name="title" size="40"><br>
内容:<textarea id="elm1" name="content" class="xheditor" rows="10" cols="80"></textarea>
<br>
<input type="submit" value="添加">
<input type="reset" value="重置"> </form>
</body>
</html>add.php<?php
/*
* Created on 2010-3-3
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
session_start();
header("Content-Type:text/html;charset=utf-8");
if(empty($_SESSION["username"])){
header("location:index.html");
}
$mysqli=mysqli_connect("localhost","root","x-duia","news");
$id = $_POST["id"];
$title=$_POST["title"];
$content=$_POST["content"];
$time = date("Ymd");//echo date("Y-m-d");
//$time=date('h-i-s, j-m-y, it is w Day z ');
//判断id是否为空
if($id !=null){
//修改操作
$sql = "update news set title='".$title."',content='".$content."', time=".$time." where id=".$id;
$res = mysqli_query($mysqli,$sql) or die ("修改数据失败: ".mysql_error());
mysqli_close($mysqli);
header("location:select.php");}
if ($title && $content){
//添加操作
$sql = "insert into news (title, content,time)values('$title','$content',".$time.")";
$res = mysqli_query($mysqli,$sql) or die ("插入数据失败: ".mysql_error());
mysqli_close($mysqli);
header("location:select.php");
}
?>select.php
<?php
session_start();
header("Content-Type:text/html;charset=utf-8");
if(empty($_SESSION["username"])){
header("location:index.html");
}
include_once 'conn.php';
$sql = "SELECT * FROM news";
$res = mysql_query($sql);
$count = mysql_num_rows($res);$i = 0;
//echo $count;
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>添加新闻</title></head><body>';
echo "<table border='1'>";
echo "<tr><td>新闻标题</td><td>新闻内容</td><td>发布时间</td><td colspan='3'>操作</td></tr>";
for($i = 0; $i<$count;$i++){
$arr = mysql_fetch_row($res);
if($arr[4] == 0)
echo '<tr><td>'.$arr[1].'</td><td>'
.substr($arr[2],0,6).'</td><td>'
.$arr[3].'</td><td><a href="look.php?id='
.$arr[0].'">查看</a></td><td><a href="update.php?id='
.$arr[0].'">修改</td><td><a href="delete.php?id='
.$arr[0].'&delete=0" onclick=javascript:return(confirm("确实要放进回收站吗?"))>删除</a></td></tr>';
}
echo '</table></body></html>';
?>
look.php
<?php
session_start();
header("Content-Type:text/html;charset=utf8");
if(empty($_SESSION["username"])){
header("location:index.html");
}
include_once 'conn.php';
$id = $_GET['id'];
if($id == null){
echo "id is null";
}
else{
$sql = "select * from news where id=".$id;
$res = mysql_query($sql);
$arr = mysql_fetch_row($res);
$count = mysql_num_rows($res);
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>添加新闻</title></head><body>';
echo $arr[1].'<br /><hr><br />发布时间:'.$arr[3].'<br />'.$arr[2];
echo '</body></html>';
}
?>现在的乱码情况是:
插入数据库的是乱码
select.php页面显示正常
look.php页面乱码
乱码问题,怎么解决啊??
整了N久了。
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>模式</title>
<link rel="stylesheet" href="css/common.css" type="text/css" media="screen" />
<script type="text/javascript" src="jquery/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="xheditor-zh-cn.js"></script>
</head>
<body>
<form action="add.php" method="post">
新闻标题:<input name="title" size="40"><br>
内容:<textarea id="elm1" name="content" class="xheditor" rows="10" cols="80"></textarea>
<br>
<input type="submit" value="添加">
<input type="reset" value="重置"> </form>
</body>
</html>add.php<?php
/*
* Created on 2010-3-3
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
session_start();
header("Content-Type:text/html;charset=utf-8");
if(empty($_SESSION["username"])){
header("location:index.html");
}
$mysqli=mysqli_connect("localhost","root","x-duia","news");
$id = $_POST["id"];
$title=$_POST["title"];
$content=$_POST["content"];
$time = date("Ymd");//echo date("Y-m-d");
//$time=date('h-i-s, j-m-y, it is w Day z ');
//判断id是否为空
if($id !=null){
//修改操作
$sql = "update news set title='".$title."',content='".$content."', time=".$time." where id=".$id;
$res = mysqli_query($mysqli,$sql) or die ("修改数据失败: ".mysql_error());
mysqli_close($mysqli);
header("location:select.php");}
if ($title && $content){
//添加操作
$sql = "insert into news (title, content,time)values('$title','$content',".$time.")";
$res = mysqli_query($mysqli,$sql) or die ("插入数据失败: ".mysql_error());
mysqli_close($mysqli);
header("location:select.php");
}
?>select.php
<?php
session_start();
header("Content-Type:text/html;charset=utf-8");
if(empty($_SESSION["username"])){
header("location:index.html");
}
include_once 'conn.php';
$sql = "SELECT * FROM news";
$res = mysql_query($sql);
$count = mysql_num_rows($res);$i = 0;
//echo $count;
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>添加新闻</title></head><body>';
echo "<table border='1'>";
echo "<tr><td>新闻标题</td><td>新闻内容</td><td>发布时间</td><td colspan='3'>操作</td></tr>";
for($i = 0; $i<$count;$i++){
$arr = mysql_fetch_row($res);
if($arr[4] == 0)
echo '<tr><td>'.$arr[1].'</td><td>'
.substr($arr[2],0,6).'</td><td>'
.$arr[3].'</td><td><a href="look.php?id='
.$arr[0].'">查看</a></td><td><a href="update.php?id='
.$arr[0].'">修改</td><td><a href="delete.php?id='
.$arr[0].'&delete=0" onclick=javascript:return(confirm("确实要放进回收站吗?"))>删除</a></td></tr>';
}
echo '</table></body></html>';
?>
look.php
<?php
session_start();
header("Content-Type:text/html;charset=utf8");
if(empty($_SESSION["username"])){
header("location:index.html");
}
include_once 'conn.php';
$id = $_GET['id'];
if($id == null){
echo "id is null";
}
else{
$sql = "select * from news where id=".$id;
$res = mysql_query($sql);
$arr = mysql_fetch_row($res);
$count = mysql_num_rows($res);
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>添加新闻</title></head><body>';
echo $arr[1].'<br /><hr><br />发布时间:'.$arr[3].'<br />'.$arr[2];
echo '</body></html>';
}
?>现在的乱码情况是:
插入数据库的是乱码
select.php页面显示正常
look.php页面乱码
乱码问题,怎么解决啊??
整了N久了。
$conn = mysql_connect('localhost','root','x-d') or die('不能连接到数据库');
mysql_select_db('news',$conn);
mysql_query('SET names UTF-8');?>
表字段编码是:utf8_unicode_ci 应该不会错啊数据库的编码是乱的 读出来可以正确显示 但源文件也是乱码
mysql_query('SET names UTF-8');这句话错误了....mysql没有说utf-8的编码...你改成:mysql_query('set names utf8');
我改成了 set names uft8 后读出来的数据都是乱的了
header("Content-Type:text/html;charset=utf8"); 这里应该是UTF-8
而
mysql_query('SET names UTF-8'); 这里应该是utf8
存入数据库的还是乱码。。
另外为什么有些用mysql,有些用mysqli ?