PHP避免向MySql添加重复记录的实例代码
最近写了下英文搜索的小偷,为了让蜘蛛更好的享受链接的乐趣,因此使用了入库处理,将最近的搜索写入到mysql中,同时在首页调用这些查询数据,就可以达到网站地图的效果,但是弊端也出现了了,那就是重复记录的问题。
在网上找到一段实例,经过修改发现可行,现将代码公布如下:
Copy to Clipboard引用的内容:[www.bkjia.com] $link=mysql_connect(‘localhost’,’root’,’1234’); //得到MySQL数据库连接$username=$_GET["name"]; //得到从客户端表单传过来的数据
$q="select * from usertable where user_name='$username'";
mysql_query("SET NAMES gb2312"); //避免出现中文乱码
$rs = mysql_query($q, $link); //查询数据库
$num_rows = mysql_num_rows($rs); //得到查询结果的总行数
if($num_rows==0)
// 烈火網 bkjia.com 欢迎复制,拒绝恶意采集 liehuo.net
{
$exec="insert into student (user_name) values ($username)";
mysql_query("SET NAMES gb2312");
mysql_query($exec, $link); //若没有此用户则将数据插入到数据库(注册用户)
echo "用户注册成功!";
}
else
{
echo "该用户名已存在,请重新选择用户名!";
}
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1
Easy-to-use and free code editor
