我是php的新手,所以请慢点(请)。我已经设置了我的代码,让用户输入YouTube视频的网址,并将该视频放在网站的其中一个网页上。唯一的问题是,我无法弄清楚如何阻止重复的视频上传。很多mysql代码都是由dreamwevaer编写的。我做了几次试图让它停止重复,但我想我错过了一些东西。那么有人可以给我指示如何添加一些东西来阻止上传同一件事的多个副本。感谢。
这是我的代码:
<?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);
?>
答案 0 :(得分:0)
如果不阅读整个代码块,您要查找的语句为INSERT ... ON DUPLICATE KEY UPDATE。数据库列必须是索引或主键。如果您尝试将其插入两次,它将执行更新而不是插入。
编辑:在PHP方面,您可能希望查看parse_url()并删除所有不必要的标记。也许您应该只存储视频的实际唯一标识符,而不是存储整个URL。例如,不是存储http://www.youtube.com/watch?v=Xqghpm4gXf4&feature=feedrec_grec_index
,而是存储Xqghpm4gXf4
,然后从该字符串重新编译URL。