簡體   English   中英

按標題創建的數據庫中的雙鏈接

[英]Double links in database created by title

我在mydatabase中有雙鏈接問題。 我有php文件形式的新職位。 有$ _POST ['title']當人們提交標題時,php從title創建一個新的URL到數據庫。 問題是當用戶提交數據庫中已經存在的標題時,因為php創建了雙鏈接。 我想編寫一個檢查url的函數,如果存在,則為新帖子創建另一個鏈接,例如:如果存在/ posts / new_link函數,則創建另一個鏈接,例如:/ posts / 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);

}

對不起,我的英語不好。

如果您知道所有標題相同的帖子都具有相似的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注入的影響。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM