PHP avoids adding duplicate records to MySQL instance code

Source: Internet
Author: User

Recently wrote the English search for thieves, in order to make the spider better enjoy the fun of the link, so the use of warehousing processing, the most recent search to the MySQL, and at the same time call these query data on the homepage, you can achieve the effect of the site map, but the drawbacks also appear, that is the problem of duplicate records.

Find an example on the Internet, after the modification found feasible, the code is published as follows:

$link =mysql_connect (' localhost ', ' root ', ' 1234 '); Get MySQL database connection

$username =$_get["name"]; Get the data from the client table conveys.

$q = "SELECT * from usertable where user_name= ' $username '";

mysql_query ("SET NAMES gb2312"); Avoid Chinese garbled

$rs = mysql_query ($q, $link); Querying the database

$num _rows = mysql_num_rows ($RS); Total number of rows obtained from query results

if ($num _rows==0)

Fire Web liehuo.net Welcome replication, Reject malicious collection liehuo.net

{

$exec = "INSERT into student (user_name) VALUES ($username)";

mysql_query ("SET NAMES gb2312");

mysql_query ($exec, $link); Insert data into the database without this user (registered user)

echo "User registration success!";

}

Else

{

echo "This username already exists, please select User name again!";

}

?>

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.