你这个需求为了更好的用户体验,用ajax无刷新好点吧。代码也很简单,去网上搜索一下。

解决方案 »

  1.   

    代码很长的。给你一个思路吧
    $_POST获取用户名。在执行SQL查询。
      

  2.   

    下面是我以前写的注册页面。你自己参考一下。  <?php
    /*注册文件
    * $Author: 陈义 
     * $Id: regist.php 2013-12-17 21:07 陈义 
    */
    //检查session是否存在。
    include('check_session.php');
    include('head.php');
    if(isset($_SESSION['id']) &&isset($_SESSION['pass'])){
    // if(isset($_SESSION['id'])==true &&isset($_SESSION['pass'])==true){
    exit("当前已有一个登陆在线的用户<span class=online_user>".$_SESSION['id']."</span>需要再<a href=regist.php>注册</a>请先<a href=index.php?act=out>退出。</a>否则请<a href=javascript: onclick=history.go(-1);>返回</a>");
    }define('cy','cychat');
    include('config.php');$name=isset($_POST['name']) ? $_POST['name']:null;
    $password=isset($_POST['password']) ? $_POST['password']:null;
    $addess=isset($_POST['addess']) ? $_POST['addess']:null;
    $age=isset($_POST['age']) ? $_POST['age']:null;
    $name_md5=md5($name);//给用户名add 一个 md5值////////////////接收用户上传的头像文件///////////////////////////
    error_reporting(0);
    $upfilepath="image/userimage/";
    $file_houzhui='';
    if($_FILES["userimage"]["type"]  == "image/gif"){
    $file_houzhui=".gif";
    }if($_FILES["userimage"]["type"]  == "image/jpeg"){
    $file_houzhui=".jpg";
    }if($_FILES["userimage"]["type"]  == "image/png"){
    $file_houzhui=".png";
    }switch ($_FILES["userimage"]["error"])
    {
    case 1:
      echo "上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值";
      break;
    case 2:
      echo "上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值";
      break;
    case 3:
      echo "文件只有部分被上传.";
      break;
    case 4:
      echo "你没有上传图像。";
      break;
    case 6:
      echo "找不到临时文件夹.";
      break;
    case 7:
      echo "文件写入失败.";
      break;
    } move_uploaded_file($_FILES["userimage"]["tmp_name"],$upfilepath.$_FILES["userimage"]["name"]);//从server 移动用户上传文件到新的路径
     rename($upfilepath.$_FILES["userimage"]["name"] ,$upfilepath.'tmp'.$name.$file_houzhui);//将客户上传的文件更名为一个临时的文件$source_filename=$upfilepath.'tmp'.$name.$file_houzhui;///////////////////////调整用户上传的图片大小///////////////$filetype=mime_content_type($source_filename);//获取图片的类型。mime_content_type 函数要在php.ini打开 fileinfo选项
    //echo 'the file type is'.$filetype;
    list($width, $height) = getimagesize($source_filename);//获取原图片的大小//如果文件的宽度和高度都小于120就把临时文件改名为正式的文件,
    if($width <= 120 && $height <= 120){
     rename($upfilepath.'tmp'.$name.$file_houzhui,$upfilepath.$name.$file_houzhui);
    }
    //判断图片的width and height 大于120 就缩小为 120*120 的新图片
    if($width > 120 || $heitht > 120){
    $new_width = 120;
    $new_height = 120;
    $image_p = imagecreatetruecolor($new_width, $new_height);// 新建一个真色彩的文件
    //// 重新取样,新建一个和用户上传同类型的文件
    //判断用户上传的是那种类型图片
    if($filetype  == "image/gif"){
    $image = imagecreatefromgif($source_filename);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);//copy 源图像到新建的真色彩文件。
    imagegif($image_p,$upfilepath.$name.$file_houzhui);//保存图片到新地址,并且重新更名
    }
    //判断用户上传的是那种类型图片
    if($filetype  == "image/jpeg"){
    $image = imagecreatefromjpeg($source_filename);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);//copy 源图像到新建的真色彩文件。
    imagejpeg($image_p,$upfilepath.$name.$file_houzhui, 80);//保存图片到新地址,并且重新更名
    }
    //判断用户上传的是那种类型图片
    if($filetype  == "image/png"){
    $image = imagecreatefrompng($source_filename);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);//copy 源图像到新建的真色彩文件。
    imagepng($image_p,$upfilepath.$name.$file_houzhui);//保存图片到新地址,并且重新更名
    } imagedestroy ($image);//销毁图片占用的内存
    imagedestroy ($image_p);//销毁图片占用的内存
    unlink($source_filename);//del 用户上传的原图片,这行很重要!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    }
    //*//////////////////////////////////////$\
    $useruploadimageaddess="";//用于插入数据库的变量//如果上传的不是图像文件或者没有上传文件
    if($filetype  != "image/png" && $filetype   != "image/gif" && $filetype   != "image/jpeg"){
    $useruploadimageaddess='noaddess';
    }else{
    $useruploadimageaddess=$upfilepath.$name.$file_houzhui;//用于插入数据库的变量
    }
    ///////////////////////////////////////////////////////
    $sql = "INSERT INTO user(  `user`,user_md5, `password`, `addess`, `age`,`userImage`,`regist_date`) VALUES ('$name','$name_md5','$password','$addess',$age,'$useruploadimageaddess',now())";
    /////////////////////////////
    /////////////////////////////
     //检查是否有用户已经注册
    $inquiry_username = mysql_query("SELECT user FROM user WHERE user='$name'");
    if(mysql_num_rows($inquiry_username)>=1){                             
    exit(' 对不起!!!<strong style=color:red>'.$name.'</strong>用户已经有人注册。');
    }
    //
    if($name!=null && $password!=null && $addess!=null && $age!=null){
    if(mysql_query($sql,$linkdatabase)){
    exit("<p style=color:red><b>您已成功注册。请记住您的用户名和密码!用户名是$name   密码是$password 请<a href=\"login.php\">登录</a></p>");//如果注册成功就不输出下面的表单。
    }else{
    echo '注册失败,请重新注册';
    }
    }?><div class="regist">
    <form method="post" enctype="multipart/form-data" action="regist.php">
      <label for="name">用户名:</label>
      <input class="regist_username" type="text" name="name" /> *<br/>
     <label for="password">密&nbsp;&nbsp;码:</label>
     <input class="regist_password" type="password" name="password"/> *<br/>
      <label for="addess">地&nbsp;&nbsp;址:</label>
     <input  class="regist_addess"type="text" name="addess"/> *<br/>
     <label for="age">年&nbsp;&nbsp;龄:</label>
     <input class="regist_age" type="text" name="age"/> *<br/>
      <label for="userimage">头&nbsp;&nbsp;像:</label>
     <input type="file" name="userimage" value="userimage"><br/>
     <input class="regist_regist" type="submit" value="注册"><br/>
     <label for="info">带'*'号的必须要填写信息 </label>
    </form>
    </div><?php
    include "footpage.php";//页脚
    ?>
      

  3.   


    $sql= 'select  * from tbname where username ="'.$username.'"';
    $result = mysql_query($sql);
    $data = mysql_num_row($result);
    if ($data){
       echo "该用户名已存在";
    }
      

  4.   

    $sql= 'select  * from tbname where username ="'.$username.'"';
    $result = mysql_query($sql);
    $data = mysql_num_rows($result);
    if ($data){
       echo "该用户名已存在";
    }