我正在创建一个视频存储页面,用户可以在该页面上进行投票或降低投票。为了检查用户是否已经喜欢视频,我创建了一个表格来检查用户是否喜欢多次。但是,限制“点赞”数量的“ videosLikedBy”表并未更新。 “视频”表存储了喜欢的总数,但效果很好。
我认为问题出在 $ _ SESSION ['id'] ,因为我之前在语法错误中显示(id ='')应该等于用户ID的位置。在php中使用会话时,我还是很新。
我尝试在您单击视频的上一页中添加 $ _ SESSION ['id'] = $ _POST ['$ id'] 。然后,在观看视频的页面中,我将有 $ id = $ _POST ['$ id'] 。但这并没有做任何事情,因此我只保留了 $ id = $ _SESSION ['id']; 作为查看页面。
下面是我的代码,
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');
};
$id = $_SESSION['id'];
$upsql = "SELECT liked FROM videosLikedBy WHERE (v_id = '$vid') AND (id = '$id')";
$upResult = mysqli_query($link, $upsql);
$likeCount = mysqli_fetch_assoc($upResult);
if(isset($_POST["submitted"]) && $likeCount['liked'] < 1){
mysqli_query($link, "UPDATE video SET likes = likes + 1 WHERE (v_id = '$vid')");
mysqli_query($link, "UPDATE videosLikedBy SET liked = liked + 1 WHERE (v_id = '$vid') AND (id = '$id')");
};
?>
<br>
<form method="post">
<input type="hidden" name="submitted">
<button type="submit">⇧</button>
</form>
<form method="post">
<input type="hidden" name="disliked">
<button type="submit">⇩</button>
</form>
下面是我的mysql表,
CREATE TABLE users (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(100) NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
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
);
CREATE TABLE videosLikedBy(
liked INT NOT NULL PRIMARY KEY,
id INT NOT NULL,
FOREIGN KEY users_id(id)
REFERENCES users(id)
ON DELETE CASCADE,
v_id INT NOT NULL,
FOREIGN KEY vid_id(v_id)
REFERENCES video(v_id)
ON DELETE CASCADE
);