谁能帮我在下面的代码中加上判断数据库里面用户名是否存在 如果存在提示用户名已存在
<?php
$iuser=$_POST['user'];
$pd=$_POST['pd'];
$ll=$_POST['ll'];
$i=$_POST['i'];
$ll=chop($ll,"GB");
$ll=$ll*1024*1024*1024;$host="localhost";
$user="root";
$password="1223456";
$db="ov";$con = mysql_connect($host,$user,$password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db($db,$con);$res=mysql_query("INSERT INTO open(iuser,isent,irecv,maxll,pass,i,starttime) VALUES('$iuser',0,0,0,'$pd','0',CURDATE())");
if($res){
echo "<script>alert('注册成功!');location.href='reg.php';;</script>";
}else{
echo "<script>alert('注册失败!');location.href='reg.php';;</script>";
}
break;mysql_close($con);echo"<script>function f(){history.go(-1);} window.setTimeout('f()',3000);</script>"; ?>

解决方案 »

  1.   

    if(select count(*) from 表名 where iuser >=1 )
      

  2.   

    if($_POST['submit']) 

    $username = $_POST["username"]; 
    $sql="select userName from user_info where userName='$username'"; 
    // echo $sql; 
    $query=mysql_query($sql); 
    $rows = mysql_num_rows($query); 
    if($rows > 0){ 
    echo "<script type='text/javascript'>alert('用户名已存在');location='javascript:history.back()';</script>"; 
    }else{ 
    $user_in = "insert into user_info (username,pass,sex,qq,email,img) values ('$_POST[username]',md5('$_POST[pass]'),'$_POST[sex]','$_POST[qq]','$_POST[email]','$_POST[img_select]')";
    //echo $user_in; 
    mysql_query($user_in); 
    echo "<script type='text/javascript'>alert('写入成功!!');location.href='login.php';</script>"; 

    //javascript:history.go(-1) 
    }
      

  3.   


    <?php
    $iuser=$_POST['user'];
    $pd=$_POST['pd'];
    $ll=$_POST['ll'];
    $i=$_POST['i'];
    $ll=chop($ll,"GB");
    $ll=$ll*1024*1024*1024;$host="localhost";
    $user="root";
    $password="1223456";
    $db="ov";$con = mysql_connect($host,$user,$password);
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    mysql_select_db($db,$con);$sqlstr = "select * from open where iuser='".$iuser."'";
    $res = mysql_query($sqlstr) or die(mysql_error());
    if(mysql_num_rows($res)>0){
        echo "<script>alert('用户名已存在!');location.href='reg.php';;</script>";
        exit();
    }$res=mysql_query("INSERT INTO open(iuser,isent,irecv,maxll,pass,i,starttime) VALUES('$iuser',0,0,0,'$pd','0',CURDATE())");
    if($res){
    echo "<script>alert('注册成功!');location.href='reg.php';;</script>";
    }else{
    echo "<script>alert('注册失败!');location.href='reg.php';;</script>";
    }
    break;mysql_close($con);echo"<script>function f(){history.go(-1);} window.setTimeout('f()',3000);</script>"; ?>