duanpo1498 2016-03-16 21:48
浏览 28

确保用户名不会通过PHP退出

Im trying to making sure than when an user introduce a username in a form, that username is not already in my Database.

When I introduce a username that already exist, I get the error message: "* Username already exist". However, the username it is introduce in the database and therefore I have duplication in my database.

Also, anytime I update(F5) the browser a NULL record gets introduce in my database.

THANK YOU SO MUCH!!! :)

Below is the code I'm using:

<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>

<?php
// define variables and set to empty values
$usernameErr = "";
$username = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {

   if (empty($_POST["username"])) {
 $usernameErr = "Username is required";
   } else {
 $username = test_input($_POST["username"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z0-9 ]*$/",$username)) {
  $usernameErr = "Only letters, numbers and white space allowed";
    }
  }
}

function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}
?>



<?php

   if ($usernameErr == '') 
    {

    $db = pg_connect('host=localhost dbname=test user=myuser password=mypass');
    $username = pg_escape_string($_POST['username']);
    $query = "SELECT username FROM host";
    $result = pg_query($query);
    if (!$result) {
        echo "Problem with query " . $query . "<br/>";
        echo pg_last_error();
        exit();
    }


while($myrow = pg_fetch_assoc($result)) {
     if ($username == $myrow[username]) {$usernameErr = "Username already exist";}
     else { $query = "INSERT INTO host(username) VALUES('" . $username . "')";}

     printf ("$myrow[username]>");

    }

   $result = pg_query($db, $query);
         if (!$result) {
         $errormessage = pg_last_error();
             echo "Error with query: " . $errormessage;
         exit();
         }

    $username = "";

    pg_close();

 }
?>


<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
   <input type="text" name="username" placeholder="User Name" value="<?php echo $username;?>">
   <span class="error">* <?php echo $usernameErr;?></span>
   <br><br>
   <input type="submit" name="submit" value="SAVE">
</form>


</body>
</html>
  • 写回答

2条回答 默认 最新

  • dqb78642 2016-03-16 21:57
    关注

    The problem is in the while statement; when it finds a coincidence in the database, it prints the 'User already exists' message, but instead of stopping the loop, it keeps comparing the username against the other records, which generate the inserted record. Change your while statement code to this:

    while($myrow = pg_fetch_assoc($result)) {
        if ($username == $myrow[username]) {
            $usernameErr = "Username already exist";
            break;
        }
        else {
            $query = "INSERT INTO host(username) VALUES('" . $username . "')";
    
            $result = pg_query($db, $query);
            if (!$result) {
                $errormessage = pg_last_error();
                echo "Error with query: " . $errormessage;
                exit();
            }
    
            $username = "";
        }
    
        printf ("$myrow[username]>");
    }
    

    This should work for you. You can also define a unique constraint for the username column in your database.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题