简体   繁体   English

在php中的mysql_query上应用条件

[英]Apply condition on mysql_query in php

sorry for my really dumb question.(I'm so new in this!). 抱歉,我的问题很愚蠢。(我真是太新了!)。

I have a query (mysql, the programming language is php): 我有一个查询(mysql,编程语言是php):

mysql_query("INSERT INTO user (id, f_name, l_name, email, user_name, password, b_day, gender, country, p_number, address, active)
VALUES ('NULL', '$_POST[fname]', '$_POST[lname]', '$_POST[email]', '$_POST[username]', '$_POST[password]', '$_POST[bday]', '$gender', '$_POST[country]', '$_POST[phone]', 'address', '0')")


or die("The username already exist");

that i need to apply some condition on it , sth like... 我需要在上面加上一些条件,比如……

if (mysql_query done inserting successfully){
// do 1-
// 2-
// 3-
// 4-
}

else {die("The username already exist");
//and do 
// 1-
// 2-
// 3-
// 4-

}

by the wayI set the username as unique in mysql database so will give an error if someone want to insert sth similar and i catch it as user already exist. 顺便说一句,我在mysql数据库中将用户名设置为唯一,因此如果有人想插入类似的东西并且由于用户已经存在而将其捕获,则会给出错误消息。

how can i do this? 我怎样才能做到这一点?

If you hit a unique key constraint in MySQL, it will throw an error, causing PHP to throw an error, which then shows up in your logs, meaning you forgot to do something in your code. 如果您在MySQL中遇到唯一的键约束,它将引发一个错误,导致PHP引发一个错误,该错误随后出现在您的日志中,这意味着您忘记了在代码中做某事。 You should do a separate SELECT query to determine if the user already exists (validation) before attempting the INSERT statement. 在尝试执行INSERT语句之前,应该执行单独的SELECT查询以确定用户是否已经存在(验证)。

Use of the mysql_* functions is discouraged. 不建议使用mysql_ *函数。 You should learn to use MySqli or PDO. 您应该学习使用MySqli或PDO。 However, I'll just answer your question. 但是,我会回答你的问题。

$qry = mysql_query("...");
if ($qry)
{
    //do 1-2-3-4
}
else
{
    //did not insert
}

And you could do a query checking if the user already exists, before you insert anything. 您可以在插入任何内容之前进行查询,以检查用户是否已经存在。

You should sanitize and clean those $_POSTS. 您应该清理并清洁那些$ _POSTS。

If you want to find if the username exists, you need to do a select statement beforehand 如果要查找用户名是否存在,则需要事先执行选择语句

SELECT * FROM user WHERE username = 'xxx'

Once you run that query, check if username exist by counting the number of rows return using mysql_num_rows (or mysqli_num_rows). 运行该查询后,通过计算使用mysql_num_rows(或mysqli_num_rows)返回的行数来检查用户名是否存在。

$result = mysql_query("SELECT * FROM user WHERE username = 'xxx'", $link);
$num_rows = mysql_num_rows($result);
if ($num_rows == 0) {
    mysql_query("INSERT INTO user (id,f_name,l_name,email,user_name,password,b_day,gender,country,p_number,address,active) VALUES ('NULL','$_POST[fname]','$_POST[lname]','$_POST[email]','$_POST[username]','$_POST[password]','$_POST[bday]','$gender','$_POST[country]','$_POST[phone]','address','0')")
} else {
    die("The username already exist");
}

Suggestion: 建议:

  • use mysqli instead of mysql 使用mysqli代替mysql
  • escape the input given to insert into DB using mysqli_real_escape_string (http://www.php.net/manual/en/mysqli.real-escape-string.php) 使用mysqli_real_escape_string(http://www.php.net/manual/en/mysqli.real-escape-string.php)转义要插入数据库的输入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM