Home > php教程 > php手册 > body text

Basic Tutorial on PHP and MySQL (2)

黄舟
Release: 2019-01-28 17:29:23
Original
1981 people have browsed it

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);
Copy after login

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"; 
}
Copy after login

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 = &#39;$USER_ID&#39; 
"); 
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 "); 
}
Copy after login

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 = &#39;$USER_ID&#39; 
"); 
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 "); 
}
Copy after login

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 (m.sbmmt.com)! 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:

//m.sbmmt.com/course/37.html

mysql video tutorial:

//m.sbmmt.com/course/list/51.html

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template