2011-08-31 2 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 행이 반환되는 경우 $sample의 ID를 가진 레코드가 이미 테이블에 존재합니다

+0

내가이 오류를 얻으려고하면 – user918236

+0

죄송합니다. – user918236

+0

아, 다시 했어요. 이 오류가 발생했습니다. " 경고 : mysql_select_db() : 제공된 인수가 /home/content/95/8201295/html/dialog/youtube-up.php 온라인의 유효한 MySQL-Link 자원이 아닙니다. 경고 : mysql_query() : 제공된 인자는 라인 47의 /home/content/95/8201295/html/dialog/youtube-up.php에있는 유효한 MySQL-Link 자원이 아닙니다. 경고 : mysql_num_rows() : 제공된 인수가 없습니다. 유효한 MySQL 결과 자원 /home/content/95/8201295/html/dialog/youtube-up.php 온라인 49 " – user918236

관련 문제