PHP avoid adding repeated records to MySql instance code _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP avoids adding duplicate record instance code to MySql. I recently wrote an English search thief. in order to make the spider better enjoy the fun of the link, I used the warehouse receiving processing to write the latest search to mysql, at the same time, I called these thieves who recently wrote an English search on the homepage. in order to make the spider better enjoy the fun of the link, I used the warehouse receiving process to write the latest search to mysql, at the same time, calling the query data on the home page can achieve the effect of website maps, but the drawback also arises, that is, the problem of repeated records.

Find an instance on the Internet and modify it. the code is published as follows:

Copy to ClipboardReference: [www.bkjia.com] $ Link = mysql_connect ('localhost', 'root', '000000'); // Obtain the MySQL database connection.
$ Username = $ _ GET ["name"]; // GET the data transmitted 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 a database
$ Num_rows = mysql_num_rows ($ rs); // The total number of rows in the query result.
If ($ num_rows = 0)
// Copy the website bkjia.com to reject malicious collection of liehuo.net
{
$ Exec = "insert into student (user_name) values ($ username )";
Mysql_query ("set names gb2312 ");
Mysql_query ($ exec, $ link); // if this user does not exist, insert the data to the database (registered user)
Echo "user registration successful! ";
}
Else
{
Echo "this user name already exists. please select another user name! ";
}
?>

...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.