PHP avoids adding duplicate records to MySQL instance code _php tutorial

Source: Internet
Author: User
Recently wrote the English search for thieves, in order to let spiders better enjoy the fun of the link, so the use of storage processing, the recent search to write to MySQL, while the home page call these query data, you can reach the effect of the site map, but the drawbacks have also emerged, that is the problem of repeated records.

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

Copy to ClipboardWhat to refer to: [www.bkjia.com] $link =mysql_connect (' localhost ', ' root ', ' 1234 '); Get a MySQL database connection
$username =$_get["name"]; Get the data from the client table only son.
$q = "SELECT * from usertable where user_name= ' $username '";
mysql_query ("SET NAMES gb2312"); Avoid Chinese garbled characters
$rs = mysql_query ($q, $link); Querying the database
$num _rows = mysql_num_rows ($RS); Total number of rows to get query results
if ($num _rows==0)
Fire Network bkjia.com Welcome copy, refuse to collect liehuo malicious. 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 users)
echo "User Registration successful! ";
}
Else
{
echo "The user name already exists, please re-select the user name!" ";
}
?>

http://www.bkjia.com/PHPjc/364720.html www.bkjia.com true http://www.bkjia.com/PHPjc/364720.html techarticle recently wrote the English search for thieves, in order to let spiders better enjoy the fun of the link, so the use of inbound processing, the most recent search to write to MySQL, while on the home page call these ...

  • Related Article

    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.