[英]Check if username exists in a database
pretty stuck on trying to prevent the user from registering if the username exists. 如果用户名存在,试图阻止用户注册。 Here is my code: 这是我的代码:
include('../connection.php');
//get all the names and values that have been posted.
$username = $_POST['username'];
//check if username exists
$sql = "SELECT * FROM tbl_Freelancers WHERE User_Username = '".$username."'";
$result = mysqli_query($con,$query);
if(mysql_num_rows($result)>=1)
{
echo"name already exists";
}
else
{ // excecute insert query
I have tried loads of other stuff to get it working but for some reason my code doesn't like me...... 我已经尝试了很多其他的东西让它工作,但由于某种原因我的代码不喜欢我......
change mysql_num_rows
to mysqli_num_rows
将mysql_num_rows
更改为mysqli_num_rows
if(mysqli_num_rows($result)>=1)//You are mixing the mysql and mysqli, change this line of code
{
echo"name already exists";
}
else
{ //
use bind param or proplerly escape your value, before using it in query. 在查询中使用它之前,请使用bind param或proplerly转义您的值。
change.. 更改..
$sql = "SELECT * FROM tbl_Freelancers WHERE User_Username = '".$username."'";
$result = mysqli_query($con,$query);
to 至
$sql = "SELECT * FROM tbl_Freelancers WHERE User_Username = '".$username."'";
$result = mysqli_query($con,$sql);
you specified query in $sql and executing in $query... also get input as.. 你在$ sql中指定了查询并在$ query中执行...也得到输入为...
$username = mysqli_real_escape_string($con, $_POST['username']);
for security reasons... 出于安全原因......
alter your query to: 将您的查询更改为:
$sql= "SELECT * FROM tbl_Freelancers WHERE User_Username = '$username'";
$result=mysql_query($sql);
if(mysql_num_rows($result)!=0)
{
echo"name already exists";
}
else
{
// excecute insert query
}
mysql_num_rows
is deprecated as of PHP 5.5.0
, and will be removed in the future mysql_num_rows
从PHP 5.5.0
不推荐使用,将来会被删除
So use alternative mysqli_num_rows
所以使用替代mysqli_num_rows
try this
include('../connection.php');
$username = $_POST['username'];
$sql = "SELECT * FROM tbl_Freelancers WHERE User_Username = '".$username."'";
$result = mysql_query($sql);
if(mysql_num_rows($result)!=0)
{
echo"name already exists";
}
else
{
echo ".........";
}
your $query
variable has to be a second argument in mysqli_query , instead you used $sql
. 你的$query
变量必须是mysqli_query中的第二个参数,而不是你使用$sql
。 so replace $sql
with $query in mysqli_query()
function and use mysqli
instead of mysql_num_rows()
所以用mysqli_query()
函数中的$ query替换$sql
并使用mysqli
而不是mysql_num_rows()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.