PHP避免向MySql添加重复记录的实例代码_PHP教程

WBOY
Release: 2016-07-21 14:53:42
Original
716 people have browsed it

最近写了下英文搜索的小偷,为了让蜘蛛更好的享受链接的乐趣,因此使用了入库处理,将最近的搜索写入到mysql中,同时在首页调用这些查询数据,就可以达到网站地图的效果,但是弊端也出现了了,那就是重复记录的问题。

在网上找到一段实例,经过修改发现可行,现将代码公布如下:

Copy to ClipboardLiehuo.Net Codes引用的内容:[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 "该用户名已存在,请重新选择用户名!";
}
?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/364720.htmlTechArticle最近写了下英文搜索的小偷,为了让蜘蛛更好的享受链接的乐趣,因此使用了入库处理,将最近的搜索写入到mysql中,同时在首页调用这些...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!