Я недавно создал систему учетных записей, в которой отображаются сообщения, опубликованные этой учетной записью.
работает, когда создается сообщение, добавляет идентификатор записи в account-posts
в другой таблице и к чему я стремлюсь делать, когда пользователь запрашивает удалить это сообщение
Итак, что я сделал, если account-posts
не содержит запятой (которая разделяется между несколькими сообщениями, когда она пуста и создается сообщение, она просто добавляет id, но если он не пуст, он добавит запятую и пробел, затем id, чтобы он выглядел как 345, 678), он удаляет его, но если он выполняет поиск запрошенного идентификатора поста удаления, то заменяет, например, «678», когда я удаляю во втором посте все мои тесты удаляют всю переменную в пустую
//$_GET['id'] is the post id and $_SESSION['id'] is the account id
$query = "SELECT * FROM `accounts` WHERE id = '".mysqli_real_escape_string($link, $_SESSION['id'])."'";
$result = mysqli_query($link, $query);
$row = mysqli_fetch_array($result);
//above is getting the `account-posts`
if (strpos($row['posts'], ',') !== false) {
//the `account-posts` has only one post eg(345 not 345, 678)
if (strpos($row['posts'], $_GET['id'].', ') !== false) {
//if the requested delete is in the middle of `account-posts`
$newposts = preg_replace($_GET['id'].", ", "", $row['posts']);
}
if (strpos($row['posts'], ', '.$_GET['id']) !== false) {
//if the requested delete is at the end of `account-posts`
$newposts = preg_replace(", ".$_GET['id'], "", $row['posts']);
}
//below it updates the `account-posts` to the new removed id
$query = "UPDATE `accounts` SET `posts` = '".mysqli_real_escape_string($link, $newposts)."' WHERE `id` = ".mysqli_real_escape_string($link, $_SESSION['id'])." LIMIT 1";
mysqli_query($link, $query);
//below it deletes the post
$query = "DELETE FROM `listing` WHERE `ID` = '".mysqli_real_escape_string($link, $_GET['id'])."' LIMIT 1";
if ($link->query($query) === TRUE ) {
header("Location: account.php");
}
} else {
$newposts = null;
$query = "UPDATE `accounts` SET `posts` = '".mysqli_real_escape_string($link, $newposts)."' WHERE `id` = ".mysqli_real_escape_string($link, $_SESSION['id'])." LIMIT 1";
mysqli_query($link, $query);
$query = "DELETE FROM `listing` WHERE `ID` = '".mysqli_real_escape_string($link, $_GET['id'])."' LIMIT 1";
if ($link->query($query) === TRUE ) {
header("Location: index.php");
}
}
Любая помощь приветствуется !!!