用户名尚不存在它说用户名已经存在

时间:2017-04-02 18:21:33

标签: php mysql

我使用以下代码来阻止在数据库中输入重复的用户名,但它总是说"用户名已存在"通过特定的用户名不存在于数据库中。我无法理解我做错了什么。提前致谢。

    $ImageName = $ImageDir.$image_tempname;
$query = mysqli_query($conn, "SELECT * FROM usernames WHERE username='".$username."'");
$rows = mysqli_num_rows($query);
if (move_uploaded_file($_FILES['image']['tmp_name'], 
                      $ImageName)) {

  //get info about the image being uploaded
  list($width, $height, $type, $attr) = getimagesize($ImageName);


  //**insert these new lines
  if ($type > 3) {
    echo "Sorry, but the file you uploaded was not a GIF, JPG, or " .
         "PNG file.<br>";
    echo "Please hit your browser's 'back' button and try again.";

  } 



elseif ($rows > 0){

    echo "email already exists";
}


else {



    //image is acceptable; ok to proceed

  //**end of inserted lines

  //insert info into image table

$insert = "INSERT INTO xyz (.......)
VALUES (........)";








$insertresults = mysqli_query($insert);  //order executes   



 if($insertresults)
{   


header ("Location:https://www.getalifepartner.com/free-matrimonial-site/upload_success.php");





 } 

1 个答案:

答案 0 :(得分:0)

这是一种方式

$query = mysqli_query($con, "SELECT * FROM usernames WHERE username='$username'");
$res = mysqli_fetch_array($query);

if ($res['username'] == $username)
{
    echo 'Username already in use, please choose another one.';
}
else
{
    // username not in use
}

这是另一种方式

$query = mysqli_query($con, "SELECT * FROM usernames WHERE username='$username'");
$rows = mysqli_num_rows($query);

if ($rows > 0)
{
    echo 'Username already in use, please choose another one.';
}
else
{
    // username not in use
}

更新原始问题

$ImageName = $ImageDir.$image_tempname;

$query = mysqli_query($conn, "SELECT * FROM usernames WHERE username='$username'");
$rows = mysqli_num_rows($query);

if (move_uploaded_file($_FILES['image']['tmp_name'], $ImageName))
{
    //get info about the image being uploaded
    list($width, $height, $type, $attr) = getimagesize($ImageName);

    //**insert these new lines
    if ($type > 3)
    {
        echo "Sorry, but the file you uploaded was not a GIF, JPG, or PNG file.<br>";
        echo "Please hit your browser's 'back' button and try again.";
    } 
}
elseif ($rows > 0)
{
    echo "Email already exists.";
}
else
{
    //image is acceptable; ok to proceed
    //**end of inserted lines
    //insert info into image table
}