我正在尝试为视频创建一个喜欢/不喜欢的系统,用户只能投票一次。在网上进行了一些研究之后,我最近在代码中实现了Ajax(我对Ajax的了解有限)。当我单击“喜欢”或“不喜欢”时,页面会刷新,但不会更改mySQL中“喜欢/不喜欢”列中的任何内容。下面是上传视频的代码。
<?php
session_start();
include "config.php";
if( !empty( $_GET['$v_id'] ) ){
$vid = $_SESSION['v_id'] = $_GET['$v_id'];
$sql='SELECT video_name FROM video WHERE v_id=?';
$stmt=$link->prepare( $sql );
$stmt->bind_param('i', $vid );
$res=$stmt->execute();
if( $res ){
$stmt->store_result();
$stmt->bind_result( $videoname );
$stmt->fetch();
printf('
<video width="70%%" height="70%%" style="background-color:#585858; border: 4px solid darkorange; border-radius:20px;" controls>
<source src="uploads/%s" type="video/mp4" id="vid">
</video>
', $videoname );
}
} else {
exit('missing ID');
}
下面是我遇到的代码。
<br>
<script>
function postAjax(url, data, success) {
var params = typeof data == 'string' ? data : Object.keys(data).map(
function(k){ return encodeURIComponent(k) + '=' + encodeURIComponent(data[k]) }
).join('&');
var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
xhr.open('POST', url);
xhr.onreadystatechange = function() {
if (xhr.readyState>3 && xhr.status==200) { success(xhr.responseText); }
};
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send(params);
return xhr;
}
</script>
<a href="" onclick="postAjax('localhost/VarcFiles/watchScreen.php?$v_id=\'$vid\'', 'vote=1', function(data){ console.log(data); });"><img src="imageStoring/like.png" style="height:30px;"/></a>
<a href="" onclick="postAjax('localhost/VarcFiles/watchScreen.php?$v_id=\'$vid\'', 'vote=-1', function(data){ console.log(data); });"><img src="imageStoring/dislike.png" style="height:30px;"/></a>
下面是mySQL
CREATE TABLE video(
v_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
video_name VARCHAR(225) NOT NULL,
id INT NOT NULL,
FOREIGN KEY user_id(id)
REFERENCES users(id)
ON DELETE CASCADE,
n_views INT,
likes INT,
dislikes INT,
image_name VARCHAR(225) NOT NILL
);
答案 0 :(得分:1)
在我看来,您没有用于实际UPDATE操作的任何代码。
SQL应该看起来像“ UPDATE video SET likes = likes + 1 WHERE v_id =?”。