53 lines
1.9 KiB
PHP
53 lines
1.9 KiB
PHP
|
|
<?php
|
||
|
|
// 处理推荐提交的后端接口
|
||
|
|
require 'db_connect.php';
|
||
|
|
|
||
|
|
// 仅处理POST请求
|
||
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||
|
|
$songName = trim($_POST['songName'] ?? '');
|
||
|
|
$artistName = trim($_POST['artistName'] ?? '');
|
||
|
|
$reason = trim($_POST['reason'] ?? '');
|
||
|
|
|
||
|
|
// 获取用户标识
|
||
|
|
function getUserIdentifier() {
|
||
|
|
if (isset($_COOKIE['user_identifier'])) {
|
||
|
|
return $_COOKIE['user_identifier'];
|
||
|
|
}
|
||
|
|
$identifier = uniqid('user_', true);
|
||
|
|
setcookie('user_identifier', $identifier, time() + 365 * 24 * 3600, '/');
|
||
|
|
return $identifier;
|
||
|
|
}
|
||
|
|
$userIdentifier = getUserIdentifier();
|
||
|
|
|
||
|
|
// 验证必填字段
|
||
|
|
if (!empty($songName) && !empty($artistName)) {
|
||
|
|
try {
|
||
|
|
// 插入数据,包含用户标识
|
||
|
|
$sql = "INSERT INTO recommendations (song_name, artist_name, reason, user_identifier, status)
|
||
|
|
VALUES (:song_name, :artist_name, :reason, :user_id, 0)";
|
||
|
|
$stmt = $pdo->prepare($sql);
|
||
|
|
$stmt->bindParam(':song_name', $songName);
|
||
|
|
$stmt->bindParam(':artist_name', $artistName);
|
||
|
|
$stmt->bindParam(':reason', $reason);
|
||
|
|
$stmt->bindParam(':user_id', $userIdentifier);
|
||
|
|
$stmt->execute();
|
||
|
|
|
||
|
|
// 跳转回主页并显示成功消息
|
||
|
|
header('Location: index.html?status=success');
|
||
|
|
exit;
|
||
|
|
} catch(PDOException $e) {
|
||
|
|
// 数据库错误
|
||
|
|
header('Location: index.html?status=error&msg=' . urlencode($e->getMessage()));
|
||
|
|
exit;
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
// 字段验证失败
|
||
|
|
header('Location: index.html?status=error&msg=歌曲名称和歌手名称为必填项');
|
||
|
|
exit;
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
// 非POST请求直接跳转回主页
|
||
|
|
header('Location: index.html');
|
||
|
|
exit;
|
||
|
|
}
|
||
|
|
?>
|