Home  >  Article  >  Backend Development  >  PHP avoid adding duplicate records to MySql example code_PHP tutorial

PHP avoid adding duplicate records to MySql example code_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 14:53:42800browse

I recently wrote about the English search thief. In order to let spiders better enjoy the fun of links, I used warehousing processing to write the recent searches into mysql, and at the same time call these query data on the homepage. , you can achieve the effect of a site map, but drawbacks have also appeared, that is, the problem of repeated records.

I found an example on the Internet and found it feasible after modification. The code is now published as follows:

Copy to ClipboardLiehuo.Net CodesQuoted content: [www.bkjia.com] $link=mysql_connect('localhost','root','1234'); //Get the MySQL database connection
$username=$_GET["name"]; //Get from the customer Data passed from the client form
$q="select * from usertable where user_name='$username'";
mysql_query("SET NAMES gb2312"); //Avoid Chinese garbled characters
$rs = mysql_query($q, $link); //Query the database
$num_rows = mysql_num_rows($rs); //Get the total number of rows in the query result
if($num_rows==0) ​​
// Agni Net bkjia.com welcomes copying and rejects malicious collection liehuo. nスt
{
$exec="insert into student (user_name) values ​​($username)";
mysql_query("SET NAMES gb2312");
mysql_query($exec, $link); / /If there is no such user, insert the data into the database (registered user)
echo "User registration successful!";
}
else
{
echo "The user name already exists, please Re-select username! ";
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364720.htmlTechArticleRecently wrote about the thief of English search. In order to let spiders better enjoy the fun of links, they used the entry Library processing, writing recent searches to mysql, and calling these on the home page...
Statement:
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