2011-08-31 39 views
0

我是新來的PHP,所以請慢(請)。我已設置了我的代碼,以允許用戶輸入YouTube視頻的網址,並將該視頻放在網站的其中一個網頁上。唯一的問題是,我不知道如何阻止重複的視頻上傳。很多mysql代碼都是由dreamwevaer編寫的。我已經做了幾次嘗試讓它停止重複,但我想我錯過了一些東西。所以有人可以給我說明如何添加一些能夠停止上傳相同內容的多個副本。謝謝。我如何停止重複的信息被上傳到MySQL

這裏是我的代碼:

<?php require_once('../Connections/Main.php'); ?> 
<?php 
if (!function_exists("GetSQLValueString")) { 
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{ 
    if (PHP_VERSION < 6) { 
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; 
    } 

    $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); 

    switch ($theType) { 
    case "text": 
     $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; 
     break;  
    case "long": 
    case "int": 
     $theValue = ($theValue != "") ? intval($theValue) : "NULL"; 
     break; 
    case "double": 
     $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; 
     break; 
    case "date": 
     $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; 
     break; 
    case "defined": 
     $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; 
     break; 
    } 
    return $theValue; 
} 
} 

mysql_select_db($database_Main, $Main); 
$query_youtube = "SELECT video_id FROM youtube"; 
$youtube = mysql_query($query_youtube, $Main) or die(mysql_error()); 
$row_youtube = mysql_fetch_assoc($youtube); 
$totalRows_youtube = mysql_num_rows($youtube); 

$editFormAction = $_SERVER['PHP_SELF']; 
if (isset($_SERVER['QUERY_STRING'])) { 
    $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); 
} 

    $pieces = explode("=", $_POST['url']); 
    $Ndone = $pieces[1]; 
    $pieces = explode("&", $Ndone); 
    $done = $pieces[0]; 
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "youtube")) { 

    $insertSQL = sprintf("INSERT INTO youtube (video_id) VALUES (%s)", 
         GetSQLValueString($done, "text")); 

    $Result1 = mysql_query($insertSQL, $Main) or die(mysql_error()); 
} 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<title>Untitled Document</title> 
<script src="../SpryAssets/SpryValidationTextField.js" type="text/javascript"></script> 
<link href="../SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" /> 
</head> 
<style type="text/css"> 
.text_box { 
    text 
    font-size: 9px; 
    color: #000; 
    } 
</style> 
<body> 
    <?php 
if (isset($_POST['url'])){ 
    echo "YouTube Video Submited"; 
    } 
?> 
    <form action="<?php echo $editFormAction; ?>" name="youtube" height="100px" method="POST" id="youtube"> 
    <span id="url"> 
    <input type="text" class="text_box" value="type in url of video " name="url" id="url2" /> 
    <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span> 
    </input> 
    <input type="submit"> 
    <input type="hidden" name="MM_insert" value="youtube" /> 
    </p> 
    </input> 
    </form> 
    <?php ?> 
<script type="text/javascript"> 
var sprytextfield1 = new Spry.Widget.ValidationTextField("url", "url", {validateOn:["blur"]}); 
</script> 
</body> 
</html> 
<?php 
mysql_free_result($youtube); 
?> 
+2

與PHP無關,你只需要在你的表模式中定義一個唯一的索引/鍵。在你的情況下,應該是YouTube的URL,http://dev.mysql.com/doc/refman/5.1/en/create-table.html,我認爲MySQL文檔會讓你想哭... – ajreal

+0

意思是你不希望在數據庫中存在兩次相同的URL? – stslavik

回答

0

沒有通過的代碼你的整個塊讀書,你正在尋找的語句是INSERT ... ON DUPLICATE KEY UPDATE。數據庫列必須是索引或主鍵。如果您嘗試插入兩次,它將執行更新而不是插入。

編輯:在PHP的一面,你可能想看看parse_url()並刪除所有不必要的標籤。也許不是存儲整個URL,而應該只存儲視頻的實際唯一標識符。例如,不是存儲http://www.youtube.com/watch?v=Xqghpm4gXf4&feature=feedrec_grec_index,而是存儲Xqghpm4gXf4,然後稍後從該字符串重新編譯該URL。

+1

謝謝,第二部分我已經在做。我只是忘了在我的評論中說。我想知道是否有一種方法來檢查在PHP方面是否有重複,因爲我想告訴用戶視頻已經更新。或者你可以用你的話來做到這一點? – user918236

+0

爲此,只需搜索它是否存在('SELECT COUNT(*)FROM table WHERE video ='$ video'LIMIT 1'),如果mysql_num_rows == 1,則顯示錯誤,否則請插入。 – Mike

+0

由於某些原因,包含mysql所獲取內容的變量每次都包含「某些資源#4」,天氣還是不存在。所以它總是說它已經存在。 – user918236