当单击用户个人资料中的链接时,我正在使用此脚本将用户插入到其他用户的收藏夹中。
目前,用户可以将用户添加到收藏夹,但我希望他们能够再次单击该按钮以将其从收藏夹中删除。
因此,如果条目以某种方式存在,我需要实现删除功能,这就是我卡住的地方。有人可以告诉我哪里出错了。
这是我最初将用户添加到收藏夹的代码:
<?php
require_once('includes/session.php');
require_once('includes/functions.php');
require('includes/_config/connection.php');
session_start();
confirm_logged_in();
if (isset ($_GET['to'])) {
$user_to_id = $_GET['to'];
}
if (!isset($_GET['to']))
exit('No user specified.');
$user_id = $_GET['to'];
$result = mysql_query("INSERT INTO ptb_favorites (user_id, favorite_id) VALUES (".$_SESSION['user_id'].", ".$user_to_id.")")
or die(mysql_error());
header($_SERVER['HTTP_REFERER']);
?>
如果条目已经存在,这就是我尝试添加的内容以使其删除:(这给了我语法错误并且不起作用)
<?php
require_once('includes/session.php');
require_once('includes/functions.php');
require('includes/_config/connection.php');
session_start();
confirm_logged_in();
if (isset ($_GET['to'])) {
$user_to_id = $_GET['to'];
}
if (!isset($_GET['to']))
exit('No user specified.');
$user_id = $_GET['to'];
$result = mysql_query("INSERT INTO ptb_favorites (user_id, favorite_id) VALUES (".$_SESSION['user_id'].", ".$user_to_id.")")
or die(mysql_error());
if ($insert() === true) {
$delete = (function () use ($connection, $user_to_id) {
$sql = "DELETE FROM ptb_favorites WHERE user_id = ? AND favorite_id = ?";
$statement = $connection->prepare($sql);
$result = $statement->execute(array(
$_SESSION['user_id'],
$user_to_id
));
return $result;
});
$delete();
}
header($_SERVER['HTTP_REFERER']);
?>