1.数据库连接

$conn=mysql_connect("localhost","root","");

mysql_select_db("baoming");

mysql_query("set names gbk");

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

2。smarty,fck配置。

-------------------smarty配置:-------------------------------

<?php

session_start();

include_once("libs/smarty/Smarty.class.php");

$smarty=new Smarty();

$smarty->left_delimiter="^";

$smarty->right_delimiter="^";

$smarty->compile_dir="libs/smarty/templates_c";

$smarty->template_dir = "templates";

$conn=mysql_connect("localhost","root","");

mysql_select_db("baoming");

mysql_query("set names gbk");

?>

------------------fck配置--------------------------------------------

<?php

include_once "libs/fck/fckeditor.php" ;

$FCK= new FCKeditor('content');

$FCK->BasePath="libs/fck/";

$FCK->ToolbarSet = 'Basic';

//$FCK->ToolbarSet = 'Default';

$FCK->Width='500px'; //设置显示宽度

$FCK->Height='400px';

$FCK->Value="";

$content_area=$FCK->CreateHtml();

?>

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


3。查,插,改,删,

--------------查询(一维数组)------------------------------------

<?php

$sql="select * from infos where id='$id'";

$result=mysql_query($sql);

$array=mysql_fetch_array($result);

$smarty->assign("array",$array);

?>

html中使用:直接用点连接相应字段名

<input type="text" name="names" value="^$array.names^" />

--------------查询(二维数组)---------------------------------------------

<?php

$sql="select * from infos where userid='$userid'";

$result=mysql_query($sql);

$row=mysql_num_rows($result);//使用mysql_num_rows判断只适用于select语句

if($row>0){

$i=0; //下面是查询的二维数组赋值给$array[$i]

while($arr=mysql_fetch_array($result)){

$arr[time]=date("Y-m-d H:i:s",$arr[time]);//改变时间的格式

$array[$i]=$arr;

$i++;

}

}

?>

html中使用:用foreach遍历数组,并显示。

^foreach from=$array item=info^

<tr>

<td>姓名:^$info.names^</td>

</tr>

<tr>

<td>性别:^$info.sex^</td>

</tr>

^/foreach^

------------------插入---------------------------------------------------

$sql="insert into infos (userid,names,sex,birth,emails,edus,time,imgs,info,kecheng) values ('$userid','$names','$sex','$birth','$email','$edus','$time','$uploaddir','$content','$kecheng')"; //插入数据库

$result=mysql_query($sql);

if($result){

echo "<script type='text/javascript'> alert('报名成功!');location.href='selectinfo.php';</script>";

}else{

echo "<script type='text/javascript'> alert('报名失败!');location.href='selectinfo.php';</script>";

}

--------------------修改-------------------------------------------------------

$sql="update infos set names='$names',sex='$sex',birth='$birth',emails='$emails',edus='$edus',time='$time',imgs='$imgs',info='$content',kecheng='$kecheng'where id='$id'";

$result=mysql_query($sql);

if($result){

echo "<script type='text/javascript'> alert('修改报名成功!');location.href='selectinfo.php';</script>";

}else{

echo "<script type='text/javascript'> alert('修改报名失败!');location.href='updateinfo.php';</script>";

}

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


4,分页,

------------------------------------分页-------------------------------------------------------

$sql="SELECT * FROM tb_message";

$result=mysql_query($sql);$datanum=mysql_num_rows($result);//总记录数

$pagesize=5;//每页显示5条

$page=$_GET['page'];

if($page=="" or $page<1){$page=1;----------$page=($page<1)?1:$page;

$pagemax=ceil($datanum/$pagesize);//求可以分得几页

if($page>$pagemax){$page=$pagemax;}--------$page=($page>$pagemax)?$pagemax:$page;

$offset=($page-1)*$pagesize;//每页从第几条开始

$sql="select * from tb_message limit $offset,$pagesize";每次从$offset开始,向下查出5条信息


<a href="online.php?page=1">首页</a>

<a href="online.php?page=<?php echo $page-1 ?>">上一页</a>

<a href="online.php?page=<?php echo $page+1 ?>">下一页</a>

<a href="online.php?page=<?php echo $pagemax; ?>">尾页</a>---------


5,文件上传,

-------------------文件上传-----------------------------------------------------

<form name="from1" method="post" id="from1" enctype="multipart/form-data">

个人图片:<input type="file" name="imgs" />

</form>


<?php

$filename=$_FILES['imgs']['name'];//上传文件

$uploaddir="upload/".$filename;

move_uploaded_file($_FILES['imgs']['tmp_name'],$uploaddir);

?>

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

6,缩略图

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

$file_name为图片的路径

$img=null;

$ext=strtolower(end(explode(".",$file_name)));

if($ext == 'jpg' || $ext == 'jpeg'){//判断图片的类型,创建相应的图片

$img = @p_w_picpathcreatefromjpeg($updir_file);

}elseif($ext == 'png'){

$img = @p_w_picpathcreatefrompng($updir_file);

}elseif($ext=='gif'){

$img=p_w_picpathcreatefromgif($updir_file);

}

list($RealWidth, $RealHeight) = getp_w_picpathsize($img); //获取图片的宽高。

$shuoluetu=p_w_picpathcreatetruecolor(100,100);//创建真彩图

p_w_picpathcopyresized($shuoluetu,$img,0,0,0,0,100,100,$RealWidth,$RealHeight);//创建100*100的缩略图

-----------------------------------------------创建缩略图------------------------------------------------------------------------------------------------------------

<?php

//$imgfile = 'smp.jpg'; //一张图片。

//$percent = 0.2;

header('Content-type: p_w_picpath/jpeg');//定义头信息。

list($width, $height) = getp_w_picpathsize($imgfile); //获取图片的宽高。

$newwidth = $width * $percent; //缩略图片的宽和高,

$newheight = $height * $percent;

$thumb = ImageCreateTrueColor($newwidth,$newheight);//新建一张原始真彩×××像,

$source = p_w_picpathcreatefromjpeg($imgfile); //以图片为基础创建一张图像。

p_w_picpathcopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); //创建缩略图,第一个参数为新建图像,第二个为原图片图像,第三四五六参数为,两头像的起始坐标,后四个为两图像的宽高。

p_w_picpathjpeg($thumb); //输出缩略图

?>



7,表单JS验证

-------------------JS验证(此处定义一个函数放在JS文件中)---------------------------------------------------------

function check(){

if(document.form1.userid.value==""){

alert("请输入用户名!");

document.form1.userid.focus();

return false;

}

if(document.form1.userid.value.length<5||document.form1.userid.value.length>10){

alert("用户名必须在5到10位之间!");

document.form1.userid.focus();

return false;

}

var m1=/[\u4e00-\u9fa5]/g;

if(!m1.test(document.form1.name.value)){

alert(姓名必须是中文!);

form1.name.value.focus();

return false;

}

}

在HTML中调用

<script type="text/javascript" src="libs/js/my_check.js"></script>

<form id="form1" name="form1" method="post" οnsubmit="return check();">

--------------------------------------JS验证,一个对话框创建一个函数,-----------------------------------------------------------------------------

my_check.js---------------

function register_userid_check(){

var text1="document.form1.userid.value";

if(document.form1.userid.value==""){

document.getElementById('userid_c').innerHTML="用户名不能为空";

return false;

}

/*}else{

document.getElementById('userid_c').innerHTML="<img width=20 height=15 src='p_w_picpaths/11.jpg'>";

}*/

else if(document.form1.userid.value.length>10 || document.form1.userid.value.length<6){

document.getElementById('userid_c').innerHTML="<font color='red'>用户名长度必须在6-10位之间</font>";

return false;

}

for(var i=0;i<document.form1.userid.value.length;i++)

{

var text=document.form1.userid.value.charAt(i);

if(!(text<=9&&text>=0)&&!(text>='a'&&text<='z')&&!(text>='A'&&text<='Z')&&text!="_")

{

document.getElementById('userid_c').innerHTML="<font color='red'>用户名只能是数字、字母、下划线组成!</font>";

break;

}

}

if(i>=document.form1.userid.value.length)

{

document.getElementById('userid_c').innerHTML="<font color='red'>√</font>";

return true;

}


}

function register_password_check(){

if(document.form1.password.value==""){

document.getElementById('password_c').innerHTML="密码不能为空";

return false;

}else if(document.form1.password.value.length<6){

document.getElementById('password_c').innerHTML="密码不能少于六位";

return false;

}else{

document.getElementById('password_c').innerHTML="<img width=20 height=15 src='p_w_picpaths/11.jpg'>";

return true;

}

}

function checkall() //检查所有

{

if(register_userid_check()&&register_password_check())

{

return true;

}

return false;

}

register.html---------

<html>

<head>

<title>注册界面难证</title>

<script type="text/javascript" src="libs/js/my_check.js"></script>

</head>

<body>

<form name="form1" method="post" action="">

<div>

<input type="text" name="userid" οnblur="register_userid_check()" /><span id="userid_c"></span>

</div>

<div>

<input type="password" name="password" οnblur="register_password_check()" /><span id="password_c"></span>

</div>

<input type="submit" name="Submit" value="注册" OnClick="return checkall();" />

</form>

</body>

</html>


8,ajax无刷新验证

-----------------------------------ajax无刷新验证-----------------------------------------------------------------

由页面调用JS中的函数funphp100(),再由该函数传参给for.php(xmlHttp.open("GET","for.php?id="+f,true);)然后由byphp()函数

接收for.php传回来的值,显示到页面的相应位置。

------------ajax.js文件---------------

var xmlHttp;

function S_xmlhttprequest(){

if(window.ActiveXObject){

xmlHttp=new ActiveXObject('Microsoft.XMLHTTP');

}else if(window.XMLHttprequest){

xmlHttp=new XMLHttpRequest();

}

}


function funphp100(xx){

var f=document.myform.xx.value;

S_xmlhttprequest();

xmlHttp.open("GET","for.php?id="+f,true);

xmlHttp.onreadystatechange=byphp;

xmlHttp.send(null);

}


function byphp(){

if(xmlHttp.readyState==1){

document.getElementById('php100').innerHTML="<img src=006.jpg>";

}

if(xmlHttp.readyState==4){

var byphp100=xmlHttp.responseText;

document.getElementById('php100').innerHTML=byphp100;

}

}

-----------------------for.php文件---------

<?php

header('Content-Type:text/html; charset=gb2312');

if($id=$_GET['id']){

sleep(1);

$conn=mysql_connect('localhost','root','')or die ('数据库连接错误');

mysql_select_db('test',$conn);

$sql="select * from users where name='$id'";

$result=mysql_query($sql);

if(is_array(mysql_fetch_row($result))){

echo '用户名已存在';

}else{

echo "可以使用";

}

}

?>


---------------------------login.php-----------

<script type="text/javascript" src="ajax.js"></script>

<form action="" name="myform" method="post" enctype="text/plain">

<input type="text" name="user" value="" οnblur="funphp100()"/>

<div id="php100"></div>

</form>

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

9,复选框,单选框,下拉菜单获值。

-------------------------单选框--------------------------------------------

<form>

男:

<input type="radio" checked name="Sex" value="male">

<br>

女:

<input type="radio" name="Sex" value="female">

</form>

----------------------------------复选框---------------------------------------------------

c语言<input type="checkbox" name="kecheng[]" id="" value="c" >//此处name定义为数组。

java语言<input type="checkbox" name="kecheng[]" id="" value="java">

php语言<input type="checkbox" name="kecheng[]" id="" value="php">

c#语言<input type="checkbox" name="kecheng[]" id="" value="c#">


php获得的值是数组,$kecheng=implode("|",$_POST['kecheng']); //用合并函数合并为字符串存入数据库


修改的时候用下面这段代码:

$array_kecheng=explode('|',$array[kecheng]);//将字符串切割为数组

for($i=0;$i<4;$i++){//循环判断数组里的值

switch ($array_kecheng[$i]){

case "c": $c_check="checked"; break; //如果数组中的值等于C就将复选框选上

case "java": $java_check="checked"; break;

case "php": $php_check="checked"; break;

case "c#": $cx_check="checked"; break;

}

}


修改显示页面

c语言<input type="checkbox" name="kecheng[]" id="" value="c" ^$c_check^>//如果$c_check的值为checked则复选框被选择

java语言<input type="checkbox" name="kecheng[]" id="" value="java" ^$java_check^>

php语言<input type="checkbox" name="kecheng[]" id="" value="php" ^$php_check^>

c#语言<input type="checkbox" name="kecheng[]" id="" value="c#" ^$cx_check^>


------------------------------------下拉菜单-------------------------------------------------------------------------------

<form>

<select name="cars">

<option value="volvo">Volvo

<option value="saab">Saab

<option value="fiat" selected>Fiat selected表示被选择

<option value="audi">Audi

</select>

</form>



------------------------------------------------简单图片验证码,他建图像,在图像上写字符串。--------------------------------------------------------------

<?php

session_start(); //开启SESSION

header("content-type:p_w_picpath/jpeg"); //向浏览器发布头信息,为图片。header()第二个参数是图片保存的路径。可将图片保存到文件中,此处是显示在浏览器。

$handel=p_w_picpathcreatefromjpeg("1.jpg");// 以1。JPG为原形创建一张图片。

$textcolor=p_w_picpathcolorallocate($handel,255,255,255);//调色。白色

$valicode=rand(1000,9999);//四位随机数做验证码。

$_SESSION[yzm]=$valicode;//传值给SESSION。

p_w_picpathstring($handel,5,100,100,$valicode,$textcolor);//在图片上写字符串,第一个是图片句柄,第二个参数是字体大小,第三,四个参数是书写起始坐标,第五个是要写的字符串,第六个是字体色。

p_w_picpathjpeg($handel);//输出JPEG格式的图像。

p_w_picpathdestory($handel);//释放图片资源。

?>


----------------------------------------------------页面跳转-----------------------------------------------------------------------------

<span id="tiao">3</span><a href="countDown">< /a>秒后自动返回…

<!--脚本开始-->

<script language="javascript" type="">

function countDown(secs){

tiao.innerText=secs;

if(--secs>0){

setTimeout("countDown("+secs+")",1000);

}else{

location.href="xx.html"//top.location.href=""//跳转到上级页面。

}


}

countDown(3);

</script>

-------------------------页面跳转---------------------------------------------------------------------------------

header("location:xx.html");//头信息跳转

javascript:history.back(1);//后退一步跳转

<META content=2;url=msg.php?id=xx http-equiv=refresh>//meta跳转,content=2指暂停2秒再跳转

<script> top.location='yy.html' </script>//返回上一级页面再跳转

<script> setTimeout(top.location='xx.php',50000);</script>//用于在指定的毫秒数后调用函数或计算表达式。

<script>location.href='register.php';</script>//跳转

<script>history.go(-1);</script>//返回跳转

<body onLoad="top.location.href='xxx.php';">//body中加载跳转,top指先回到上一级页面再跳转。