检查数据库中是否存在用户名(Check if username exists in a database)
如果用户名存在,那就试图阻止用户注册。 这是我的代码:
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
我已经尝试了很多其他的东西让它工作,但由于某种原因我的代码不喜欢我......
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......
原文:https://stackoverflow.com/questions/26357597
更新时间:2019-11-19 03:56
最满意答案
将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
{ //
在查询中使用它之前,请使用bind param或proplerly转义值。
change mysql_num_rows to 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.
2014-10-14
相关问答
编辑:新的答案,旧的仍然在下面。 我会摆脱你的方法"checkFirebaseForUsername"因为它总是会返回0,无论如何。 你需要做的是: DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
ref.child("users").child("username").addListenerForSingleValueEvent(new ValueEventListener() {
@Overrid
...
将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
{ //
在查询中使用它之前,请使用bind param或
...
//Connections
try {
$handler = new PDO('mysql:host=localhost;dbname=s','root', '*');
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e){
exit($e->getMessage());
}
$name = $_POST['name'];
$u
...
您的选择查询是错误的。 你错过了'大约$username 。 试试这个: $query = "SELECT user_user FROM lichie_user WHERE user_user='$username'";
另请参考: 如何在PHP中阻止SQL注入? Your select query is wrong. You are missing ' around $username. Try this one : $query = "SELECT user_user FROM lichie
...
那不会返回整数 string CheckUser = "select count(*) from UserData where Username like @Username";
SqlCommand com2 = new SqlCommand(CheckUser, conn2);
com2.Parameters.AddWithValue("@Username", "'%"+ UsernameTextBox.Text +"%'");
int IsMatch = Convert.To
...
你要找的是在主键存在时更新值的SQL,如果不存在则插入。 在MySQL中,仅当插入不存在时才具有该形式 INSERT INTO Table11(...) VALUES(...) ON DUPLICATE KEY UPDATE col11=value1, col2=value2,...
要指定您希望值来自VALUES()部分,您需要进行以下操作。 INSERT INTO Table11(...) VALUES(...) ON DUPLICATE KEY UPDATE col1=VALUES(col
...
您可以将数据库中的用户名字段设置为主键或唯一键,这保证了数据库中的用户名唯一性。 然后,如果尝试插入已存在的用户名, 则mysql_query()函数将失败,并返回FALSE 。 除此之外,您应该始终使用简单的select语句向数据库查询用户名的存在: SELECT username FROM table WHERE username='$php_username_var'; You can make the username field in the database a primary key
...
你有错误 if(isset($_POST))['username'])){
它应该是 if(isset($_POST['username'])){
$_POST['username']应该包含在isset函数中 you have error here if(isset($_POST))['username'])){
it should be if(isset($_POST['username'])){
$_POST['username'] should be enclosed inside
...
根据您的问题,您正在从数据库中选择所有用户并逐个比较输入的新用户名,因此可能会导致性能问题。 你可以试试这样: SqlConnection con = new SqlConnection("Your ConnectionString");
con.Open();
SqlCommand cmd = new SqlCommand("select * from [login] where UserName=@Name",con);
cmd.Parameters.AddWithValue("@Name"
...