The content of this article is about the basic tutorial of PHP and MySQL (2). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Data validation
Clean spaces
trim function will clear the spaces at the beginning and end of the data. Its syntax is:
trim ($first_name);
Processing of required fields
In your database, some fields must be filled in . In this way, the corresponding fields corresponding to the HTML form are not allowed to be left blank. Of course, this verification process can be handled by the client's javaScript script, but since we are talking about php, let's use PHP to handle it. The following code checks whether the user's last name is entered:
if (ereg(".", $first_name) == 1) { PRint (" 姓 : "); print ("$first_name"); $verify = "OK"; } else { print ("< b> 错误: < /b> 您的尊姓没有被填写 "); $verify = "bad"; }
ereg The pattern recognition function is used to determine whether the specified string contains a certain substring. Its first parameter is the substring to determine whether it is included, and the second parameter specifies the string to be searched, usually a variable. The Ereg function returns "0" (false) if the match fails, or "1" (true) if the match succeeds. Here the comma "." is a pattern wildcard, representing any character. In this way, the expression ereg(".", $first_name) == 1 means that the variable $first_name contains at least one character.
Check e-mail address
Use the following character constants as the first parameter of the ereg function to easily check the e-mail address:
"@": Must contain @
"^ @": Cannot start with @
"@.*..": There must be a character between @ and ..
"....*": There must be at least two characters after .
" ": No spaces are allowed
With these parameter examples, you can also design some other input validations.
Check whether the username is unique
This action seems to be necessary:
MySQL_connect (localhost, username, passWord); mysql_select_db (dbname); $result = mysql_query ("SELECT * FROM tablename WHERE USER_ID = '$USER_ID' "); if ($row = mysql_fetch_array($result)) { print ("< b> 错误: < /b> 用户名 < b>"); print ("$USER_ID "); print ("< /b> 已经被占用,请选者其它的再试试。 "); print ("< p>"); $verify = "bad"; } else { print (" 用户 ID: "); print ("$USER_ID "); }
The idea of the code is very simple. After reading this, I believe it will not be difficult for you.
Check whether the user name is unique
This action seems to be necessary:
MySQL_connect (localhost, username, passWord); mysql_select_db (dbname); $result = mysql_query ("SELECT * FROM tablename WHERE USER_ID = '$USER_ID' "); if ($row = mysql_fetch_array($result)) { print ("< b> 错误: < /b> 用户名 < b>"); print ("$USER_ID "); print ("< /b> 已经被占用,请选者其它的再试试。 "); print ("< p>"); $verify = "bad"; } else { print (" 用户 ID: "); print ("$USER_ID "); }
The idea of the code is very simple. After reading this, I believe it will not be difficult for you.
The above is the content of the PHP and MySQL basic tutorial (2). For more related content, please pay attention to the PHP Chinese website (www.php.cn)! M Related recommendations: y Summary of the basic knowledge of MySQL
PHP and MySQL basic tutorials (1)
PHP and MySQL basic tutorials (3)
PHP and MySQL basic tutorial (4)
mysql manual tutorial: mysql video tutorial: