我在我的数据库中有双链接的问题。我有新帖子的php文件形式。当用户提交title时,会出现$_POST' title‘,php会从title创建一个新的url到数据库。问题是当用户提交已经存在于数据库中的标题时,因为php创建了双重链接。我想写一个检查url的函数,如果它存在,就为新的post创建另一个链接,例如:如果存在/post/ new _ link函数,则创建另一个链接,如:/post/new_link_1。我有这样的代码:
$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);
}对不起,我的英语不好。
发布于 2011-11-14 01:02:39
如果你知道所有具有相同标题的帖子都有类似的URL,那么查询一下:
$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注入的影响。
https://stackoverflow.com/questions/8113122
复制相似问题