MySQL 重复检查不起作用

发布于 2024-12-02 05:49:39 字数 4200 浏览 0 评论 0原文

我试图让用户将某些内容上传到我网站的数据库,但我想在让他们上传之前检查它是否已经存在。现在,我的所有代码都已在 Dreamweaver 中的第一个代码块中编写,除了 isO 函数是我尝试使其正常工作的一部分。

这是我的代码:

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

I'm trying to let users upload something to my website's database, but I want to check if it's already there before I let them upload it. Right now all of my code has been written in Dreamweaver in the first code block, except the isO function was part of my attempt to get it to work.

Here is my code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

卷耳 2024-12-09 05:49:39

更改此:

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

将此

if(mysql_num_rows($dbunames) >= 1)

mysql_num_rows 计数返回的行数。如果返回 1 行,则表中已存在 id 为 $sample 的记录

change this:

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

to this

if(mysql_num_rows($dbunames) >= 1)

mysql_num_rows count the number of rows returned. If even 1 row is returned, a record with id of $sample already exists in your table

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文