从javascript运行php更新文件

时间:2015-02-07 09:52:52

标签: javascript php mysql

我正在尝试在单击表中的链接时对mysql运行更新。 为此,我制作了3个文件:

movies.php

<html>
<head>
<link href="css/style.css" rel="stylesheet" type="text/css" media="screen" />
<script src="video.js" type="text/javascript"></script>
</head>

<?php

include 'combo_new.php';
include 'config.php';
include 'opendb.php';

$ndate = $_POST['ndate'];

$result = mysql_query("SELECT *
            FROM DayMovie 
            WHERE FileDate LIKE '$ndate%' ORDER BY FileDate DESC") 
or die(mysql_error());  
echo "<table border='0'>";
echo "<tr> <th>Dato</th><th>Visninger</th><th>Handling</th></tr>";
while($row = mysql_fetch_array( $result )) {
	echo "<tr><td>";
	echo date('d.m.Y', strtotime($row['FileDate']));
	echo "</td><td>";
	echo $row['Counter'];
	echo "</td><td>";
	echo "<a href='alldaymovies/{$row['FileName']}' onclick='playVideo(this.href, {$row['FileName']});' onkeypress='playVideo(this.href, {$row['FileName']});'>Se film</a>";
	echo "</td></tr>";        
} 
echo "</table>";

include 'closedb.php';
?>

</html>

的Video.js

function playVideo(filename)
{
	$.post( "update.php" {"filename":filename},
	function( data ) {
	alert( "Data Loaded: " + data );
});
}

update.php

<?php

include 'config.php';
include 'opendb.php';

$filename = $_POST['filename'];

$result = mysql_query("UPDATE DayMovie SET Counter=Counter+1 WHERE FileName='$filename'") 
or die(mysql_error());

include 'closedb.php';
?>

然而,这里的事情不正确......谁能看到我哪里出错了?

1 个答案:

答案 0 :(得分:2)

问题可能是您的用户在调用update.php之前已经重定向到另一页。请记住,如果您将浏览器重定向到另一个请求繁忙请求取消的页面。

要测试这是否真的是问题,请尝试用“#”替换“a”元素的href。 并将playVideo功能更改为:

function playVideo(filename)
{
	$.post( "update.php" {"filename":filename},
	function( data ) {
	alert( "Data Loaded: " + data );
    setTimeout(function(){ document.location.href="alldaymovies/" + filename;}, 300);
});
}