提交代码无法正常工作

submit code not working correctly

好吧,我创建了简单的注册表格,我的意思是它不是注册,它应该检查用户名是否存在于数据库中,但它不起作用,我的意思是,它只显示 $rows 并且不检查username 那好,总是得到 This username does not exists, even if does.

名称存储为 Firstname_Lastname,这就是为什么我将 2 个字符串转换为 1_2

if(isset($_POST['register_button']))
{       

    $name = clear($_POST['firstname']);
    $lastname = clear($_POST['lastname']);

    $createdname = sprintf("%s_%s", $name, $lastname);

    $escapename = mysqli_real_escape_string($createdname, $escapename);

    $email = clear($_POST['email']);
    $pass = clear($_POST['password']);
    $cpass = clear($_POST['confpassword']);

    $query = "SELECT Ime FROM Igraci WHERE Ime = '$escapename'";
    $stmt = mysqli_prepare($con, $query) or die(mysqli_error($con));
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);
    $row = mysqli_fetch_array($result);

    if($row)
    {
        echo 'Thsis name already exists print: '.$createdname.'';
        exit();
    }
    else if(!$row)
    {
        echo 'This name does not exists: '.$createdname.'';
        exit();
    }
}

if($_COOKIE['authorization'] == "ok")
{
    header ("Location:welcome.php");
    exit();
}


function clear($message)
{
   if(!get_magic_quotes_gpc())
   $message = addslashes($message);
   $message = strip_tags($message);
   $message = htmlentities($message);
   return trim($message);
}
  1. 你的作业:$escapename = mysqli_real_escape_string($createdname, $escapename);向 mysqli_real_escape_string 传递了错误的参数。第一个参数应该是您的 sql 连接变量。否则你可以像这样将 $createdname 传递给函数: $escapename = mysqli_real_escape_string($createdname);
  2. 由于您只想知道该名称是否存在于数据库中,您应该使用 FareedMN 评论中的建议并查询计数,然后检查计数是否 >0。您可以使用此查询: $query = "SELECT COUNT(*) FROM Igraci WHERE Ime = '$escapename'";