2011-08-31 117 views
0

我試圖讓用戶上傳一些東西到我的網站的數據庫,但我想檢查它是否已經存在,然後讓他們上傳它。現在,我的所有代碼都是在Dreamweaver中的第一個代碼塊中編寫的,但isO函數是我嘗試使其工作的一部分。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']); 
    } 

    function isO($sample) { 
    mysql_select_db($database_Main, $Main); 
    $dbunames = mysql_query("SELECT * FROM youtube WHERE video_id='".$sample."'", $Main); 
    echo ($dbunames); 
    if(mysql_num_rows($dbunames) == $sample) { //check if there is already an entry for that username 
    echo "this video has alreday been submited"; 
    return false ; 
    } else { 
    return true; 
    } 
    } 

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

     $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); 
    ?> 

回答

1

改變這一點:

if(mysql_num_rows($dbunames) == $sample) 

這個

if(mysql_num_rows($dbunames) >= 1) 

mysql_num_rows計算行數返回。如果即使返回1行,您的表中已經存在一個ID爲$sample的記錄

+0

當我嘗試我剛剛得到此錯誤 – user918236

+0

對不起,我打算進入。 – user918236

+0

啊,我又做了。當我嘗試,我得到這個errer「 警告:mysql_select_db():提供的參數不是一個有效的MySQL鏈接資源在/home/content/95/8201295/html/dialog/youtube-up.php在線46 警告:mysql_query():提供的參數不是47行中的/home/content/95/8201295/html/dialog/youtube-up.php上的有效MySQL-Link資源 警告:mysql_num_rows():提供的參數不是第49行的/home/content/95/8201295/html/dialog/youtube-up.php中有效的MySQL結果資源「 – user918236