I just got done following through a tutorial that I found on 9lessons.info and I made some progress. The page loads and everything looks OK, but when I go to submit a comment I get the following error:
Notice: Undefined variable: video_id in C:\wamp\www\test\stream.php on line 26
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
Line 26 is the start of the $sql_video query. Here is the code I have so far...
<?php
//////////////////// REDIRECT UNAUTHORIZED GUESTS ////////////////////
if (!isset($_SESSION)) {
session_start();
}
//////////////////// REDIRECT UNAUTHORIZED GUESTS ////////////////////
if (!isset($_SESSION['customer_id'])) {
header("location:login.php");
}
//////////////////// CONNECT AND SELECT DATABASE ////////////////////
require_once('connect.php');
mysql_select_db($database, $connect);
//////////////////// FIND OUT WHO IS LOGGED IN SO THEY CAN COMMENT ////////////////////
$customer_id = $_SESSION['customer_id'];
$sql_customer = mysql_query("SELECT CustomerId, CustomerUsername FROM Customer WHERE CustomerId = '$customer_id'", $connect);
$data_customer = mysql_fetch_assoc($sql_customer);
$comment_author = $data_customer['CustomerUsername'];
//////////////////// GET VIDEO ////////////////////
if (isset($_GET['videoid'])) {
$video_id = $_GET['videoid'];
}
$sql_video = mysql_query("SELECT VideoId, VideoFilename, VideoTitle FROM Video WHERE VideoId = $video_id", $connect) or die(mysql_error());
$data_videos = mysql_fetch_assoc($sql_video);
$rows_videos = mysql_num_rows($sql_video);
$video = $data_videos['VideoFilename'];
$title = $data_videos['VideoTitle'];
$video_id = $data_videos['VideoId'];
//////////////////// GET CURRENT RATING ////////////////////
$sql_rating = mysql_query("SELECT VideoScore, VideoVotes FROM Video WHERE VideoId = $video_id", $connect) or die(mysql_error());
$data_rating = mysql_fetch_assoc($sql_rating);
$rows_rating = mysql_num_rows($sql_rating);
$total_score = $data_rating['VideoScore'];
$number_of_votes = $data_rating['VideoVotes'];
if (isset($_POST['rating'])) {
$total_score = $total_score + $_POST['rating'];
}
$new_rating = bcdiv($total_score, ++$number_of_votes, 2);
if ($new_rating == '0.00') {
$rating_message = "This video has not yet been rated. Be the first to rate it.<br />";
} else {
$rating_message = "Current Rating: $new_rating<br />";
}
//////////////////// SUBMIT NEW RATING ////////////////////
if (isset($_POST['rate'])) {
$sql_update = mysql_query("UPDATE Video SET VideoScore = '$total_score', VideoVotes = '$number_of_votes' WHERE VideoId = '$video_id'", $connect) or die(mysql_error());
}
//////////////////// SUBMIT NEW COMMENTS ////////////////////
if (isset($_POST['submit'])) {
$comment_author = mysql_real_escape_string($_POST['comment_author']);
$comment_text = mysql_real_escape_string($_POST['comment_text']);
$comment_timestamp = mysql_real_escape_string($_POST['comment_timestamp']);
$comment_video_id = $video_id;
$sql_insert_comment = mysql_query("INSERT INTO Comments (CommentAuthor, CommentText, CommentTimestamp, CommentVideoId) VALUES ('$comment_author', '$comment_text', '$comment_timestamp', '$comment_video_id')", $connect) or die(mysql_error());
//////////////////// GET CURRENT COMMENTS ////////////////////
$sql_get_comment = mysql_query("SELECT CommentAuthor, CommentText, CommentTimestamp, CommentVideoId FROM Comments WHERE CommentVideoId = '$video_id' ORDER BY CommentTimestamp DESC", $connect) or die(mysql_error());
$data_sql_get_comment = mysql_fetch_assoc($sql_get_comment);
$rows_sql_get_comment = mysql_num_rows($sql_get_comment);
echo $data_sql_get_comment['CommentText'];
echo $data_sql_get_comment['CommentAuthor'];
echo $data_sql_get_comment['CommentTimestamp'];
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta name="Description" content="Information architecture, Web Design, Web Standards." />
<meta name="Keywords" content="your, keywords" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="layout.css" type="text/css" />
<script src="flowplayer/flowplayer-3.2.2.min.js"></script>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(function() {
$(".comment_button").click(function() {
var text = $("#comment_text").val();
var author = $("#comment_author").val();
var date = $("#comment_date").val();
var dataString = 'comment_text='+ text + '&comment_author' + author + '&comment_date' + date;
if(text=='')
{
alert("Please enter a comment.");
}
else
{
$("#flash").show();
$("#flash").fadeIn(400).html('<img src="ajax-loader.gif" align="absmiddle"> <span class="loading">Loading Comment...</span>');
$.ajax({
type: "POST",
url: "stream.php",
data: dataString,
cache: false,
success: function(html){
$("#display").after(html);
document.getElementById('comment_text').value='';
document.getElementById('comment_author').value='';
document.getElementById('comment_date').value='';
document.getElementById('comment_text').focus();
document.getElementById('comment_author').focus();
document.getElementById('comment_date').focus();
$("#flash").hide();
}
});
} return false;
});
});
</script>
<title>Fitness Video Tutorials</title>
</head>
<body>
<!--WRAP STARTS HERE-->
<div id="wrap">
<?php include('header.php'); ?>
<?php include('menu.php'); ?>
<!--CONTENT WRAP STARTS HERE-->
<div id="content-wrap" class="two-col">
<?php include('sidebar.php'); ?>
<!--MAIN CONTENT STARTS HERE-->
<div id="main">
<h1><?php echo $title; ?></h1>
<p>Enjoy your video!</p>
<!-- FLOWPLAYER VIDEO PLAYER STARTS HERE-->
<div align="center">
<a href=<?php echo "videos/".$video; ?> style="display:block;width:425px;height:300px;" id="player"></a>
<script language="JavaScript">
flowplayer("player", "flowplayer/flowplayer-3.2.2.swf",
{
version: [9, 15]
}
);
</script>
</div>
<!--FLOWPLAYER VIDEO PLAYER ENDS HERE-->
<!--RATING FORM STARTS HERE-->
<div align="center">
<?php echo $rating_message; ?>
<form id="rate_video" method="post" action="">
<table width="35%" cellpadding="2" cellspacing="0">
<tr>
<td align="center" bgcolor="#CCCCCC" class="customer_info"><input type="radio" name="rating" value="1" id="rating_0" />
<br />
1
</td>
<td align="center" bgcolor="#CCCCCC" class="customer_info"><input type="radio" name="rating" value="2" id="rating_1" />
<br />
2
</td>
<td align="center" bgcolor="#CCCCCC" class="customer_info"><input type="radio" name="rating" value="3" id="rating_2" />
<br />
3
</td>
<td align="center" bgcolor="#CCCCCC" class="customer_info"><input type="radio" name="rating" value="4" id="rating_3" />
<br />
4
</td>
<td align="center" bgcolor="#CCCCCC" class="customer_info"><input type="radio" name="rating" value="5" id="rating_4" />
<br />
5
</td>
<td align="center" bgcolor="#CCCCCC"><input type="submit" name="rate" id="rate" value="Rate" />
</td>
</tr>
</table>
</form>
</div>
<!--RATING FORM ENDS HERE-->
<br />
<!--COMMENT FORM AND DISPLAY STARTS HERE-->
<div align="center">
<form name="comment" id="comment" method="post" action="">
<textarea name="comment_text" id="comment_text" cols="50" rows="3">Comment on this Video</textarea><br />
<input type="submit" name="submit" id="submit" value="Comment" class="comment_button" />
<input name="comment_author" type="hidden" id="comment_author" value="<?php echo $comment_author; ?>"/>
<input name="comment_timestamp" type="hidden" id="comment_timestamp" value="<?php echo date ("Y-m-d H:m:s"); ?>" />
</form>
</div>
<div id="flash"></div>
<div id="display"></div>
<!--COMMENT FORM AND DISPLAY ENDS HERE-->
<!--MAIN CONTENT ENDS HERE-->
</div>
<!--CONTENT WRAP ENDS HERE-->
</div>
<?php include('footer.php'); ?>
<!--WRAP ENDS HERE-->
</div>
</body>
</html>