首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >标题创建的数据库中的双重链接

问标题创建的数据库中的双重链接
EN

Stack Overflow用户
提问于 2011-11-14 00:56:09
回答 1查看 124关注 0票数 0

我在我的数据库中有双链接的问题。我有新帖子的php文件形式。当用户提交title时,会出现$_POST' title‘,php会从title创建一个新的url到数据库。问题是当用户提交已经存在于数据库中的标题时,因为php创建了双重链接。我想写一个检查url的函数,如果它存在,就为新的post创建另一个链接,例如:如果存在/post/ new _ link函数,则创建另一个链接,如:/post/new_link_1。我有这样的代码:

代码语言:javascript
复制
$link = $_GET['url'];

$checkdb = mysql_query("SELECT `url` FROM `posts` WHERE `url`='$link'" ) or die("ERROR");
$howmanypost = mysql_num_rows($checkdb); //this is check links in db

if ($howmanypost = $newlinksfromtitle) { 

// Here I would like the function that creates a new url to db

} else {

$newpost = "INSERT INTO `posts` (`id`, `title`, `img`, `tags`, `author`, `data`, `type`, `url`) VALUES ('', '$title', '$img', '$tags', '$author', '$data', 'img', '$url')";
$makepost = mysql_query($newpost);

}

对不起,我的英语不好。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-14 01:02:39

如果你知道所有具有相同标题的帖子都有类似的URL,那么查询一下:

代码语言:javascript
复制
$title = mysql_real_escape_string( $title);
$result = mysql_query("SELECT `title` FROM `posts` WHERE `title`='$title'" );
$num_same = mysql_num_rows( $result);

if( $num_same > 0) 
{
    // We know there's at least 1 post with the same title, so change the URL
    $url = $url . '_' . $num_same; // Or: ($num_same + 1)
}

$sql = "INSERT INTO `posts` (`id`, `title`, `img`, `tags`, `author`, `data`, `type`, `url`) VALUES ('', '$title', '$img', '$tags', '$author', '$data', 'img', '$url')";
$result = mysql_query( $sql);

确保正确清理输入,这样字符串就不会受到SQL注入的影响。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8113122

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档