PHP-MySQLi处理两次查询或错误
作者:互联网
我有一个非常奇怪的错误,我根本不理解.
我有以下以两种方式运行的PHP脚本.
该脚本外壳将新用户添加到mysql数据库.我要检查的是数据库中是否已存在用户名.如果是这样,请设置一个用于决定如何进行的变量.
如果我执行此代码,则此行
if(strcmp($row["username"], $addUser_name) == 0)
被执行(或最好跳入),但我100%确信在执行此行之前,该名称在数据库中不存在(或不应该存在).现在,即使将$errorName设置为1并打印测试,我想要添加到数据库中的数据集也会出现在我的数据库中,但是最后一部分开始于
if(!isset($errorName))
不执行.我一遍又一遍地用简单的echo语句检查了它.
为了查看我的数据集,我删除了这一部分的注释
while ($row = mysqli_fetch_assoc($allUserNames))
{
echo $row["username"]."\n";
}
这部分是正确执行的
if(!isset($errorName))
但是我在数据库中两次找到了数据集.
我根本不了解,为什么Scipt的行为方式如此.我已经尝试了许多不同的方法,但是我无法弄清楚我在做什么错.
<?php
include "auth/auth1.php";
include "functions/connectToDB.php";
include "functions/test_input_XSS.php";
if(isset($_GET["startCheck"])) //TODO Mache auch GET noch POST
{
//Sollte niemals true sein! Passiert nur, wenn man Unsinn macht
if(strcmp($_GET["addUser_pw"], $_GET["addUser_pwRepeat"]) !== 0) { die; }
$servername = "localhost";
$databasename = "X";
$mysqluser = "X";
$mysqlpass = "X";
$addUser_name = $_GET["addUser_name"];
$connection = connectToDB($servername, $mysqluser, $mysqlpass, $databasename);
if(mysqli_connect_errno())
{
printf("Connect failed!");
die();
}
$query_getAllUserNames = "SELECT username FROM user;";
$allUserNames = mysqli_query($connection, $query_getAllUserNames);
/*while ($row = mysqli_fetch_assoc($allUserNames))
{
echo $row["username"]."\n";
}*/
while ($row = mysqli_fetch_assoc($allUserNames))
{
if(strcmp($row["username"], $addUser_name) == 0)
{
$errorName = 1;
echo "test";
}
}
if(!isset($errorName))
{
$username = test_input_for_XSS($_GET["addUser_name"]);
$password = hash("sha256", $_GET["addUser_pw"]);
$permission = test_input_for_XSS($_GET["addUser_permission"]);
$query_addUser = "INSERT INTO user (username, passwordhash, permissionlevel) VALUES ('".$username."', '".$password."', '".$permission."');";
$addUserSuccess = mysqli_query($connection, $query_addUser);
if($addUserSuccess !== 1)
{
$hostname = $_SERVER['HTTP_HOST'];
$path = dirname($_SERVER['PHP_SELF']);
echo"Success";
//header("Location: http://".$hostname.($path == "/" ? "" : $path)."/userManagment.php?added=".$username."");
}
else
{
echo "ANNOYING_ERROR";
}
}
//Tidy up
mysqli_free_result($allUserNames);
mysqli_close($connection);
}
?>
这是相应的HTML代码,此后紧跟在同一文件中:
<?php
include "home.php";
?>
<section>
<h3>Einen neuen Benutzer hinzufügen</h3>
<?php
if(isset($errorName))
{
echo '<p class="warningMessage">Der Nutzername <b>'.$_GET["addUser_name"].'</b> ist bereits vergeben.<br />Bitte wählen Sie einen anderen aus!</p>';
}
?>
<form method="GET" action="addUser.php">
<table>
<tr>
<td>Nutzername:</td>
<td><input type="text" name="addUser_name" required pattern="\w+" /></td>
<td></td>
<td class="annotation">z.B.: Vorname</td>
</tr>
<tr>
<td>Passwort:</td>
<td><input type="password" name="addUser_pw" required pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}" title="Passwort muss mindestens 6 Zeichen, Groß- und Kleinbuchstaben, sowie Zahlen enthalten"
onChange="this.setCustomValidity(this.validity.patternMismatch ? '' : ''); if(this.checkValidity()){ form.addUser_pwRepeat.pattern = this.value; }" /></td>
<td></td>
<td class="annotation">Muss Groß- und Kleinbuchstaben, Zahlen und mindestens 6 Zeichen enthalten</td>
</tr>
<tr>
<td>Passwort wiederholen:</td>
<td><input type="password" name="addUser_pwRepeat" required pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}" /></td>
<td></td>
<td class="annotation">Muss identisch sein zum ersten eingegebenen Passwort</td>
</tr>
<tr>
<td>Berechtigungslevel:</td>
<td>
<input type="radio" name="addUser_permission" value="1" checked />1<br />
<input type="radio" name="addUser_permission" value="2" />2
</td>
</tr>
</table>
<input type="hidden" name="startCheck" value="1" />
<input type="submit" value="Nutzer hinzufügen" class="button" />
</form>
</section>
</body>
</html>
如果对我的问题的描述不够清楚,我很乐意提供所需的任何其他信息.
解决方法:
我建议您不要获取所有用户名,并通过PHP检查是否有相同的用户名.您可以像这样简单地进行查询
$result=$connection->query("SELECT username FROM user WHERE username='".$connection->real_escape_string($addUser_name)."';");
然后检查查询是否返回给您任何行
if($result -> num_rows > 0)
{
//the username is already in use
}
else
{
//the username is unique
}
标签:mysqli,html-form,html,mysql,php 来源: https://codeday.me/bug/20191028/1950170.html