2011-11-13 22 views
0

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

} 

對不起,我的英語不好。

+0

好的SQL注入漏洞。 –

+0

現在已經修補 –

回答

1

如果你知道所有具有相同職務的職位也有類似的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注入。

+0

感謝您的快速響應,我試試這個 –

+0

這是非常好的工作,非常感謝。 –