问题
I am creating message conversation delete script in PHP MYSQLI. I am trying to update session empty value. My problem I can update from_delete
column. But I can't update incoming message column to_delete
. I want to update from_delete
or to_delete
columns empty session value.
Here is my pm table
id from_id to_id msg sent_date from_delete to_delete
1 2 3 hi how are you? 2019-12-05 04:14:20 0 3
2 3 2 fine 2019-12-05 05:15:58 0 2
3 2 3 hi 2019-12-05 03:20:34 0 3
4 5 2 hi 2019-12-05 08:30:40 0 2
Here is my source code
<?php
require_once "config.php";
if (isset($_GET['deleteid'])) {
$id = $_GET['id'];
$session_id = $_SESSION['userid'];
}
$stmt = $con->prepare("UPDATE pm SET from_delete = ? or to_delete =? WHERE id=? ");
$stmt->bind_param("sss", $session_id, $session_id, $id);
if ($stmt->execute()) {
echo"deleted successfully";
} else {
echo "Failed to delete<br/>";
}
回答1:
I think it would be better to read first and check if message belongs to the current user as sender or receiver, then delete for him. But you can try to do both just setting the right filters.
<?php
require_once "config.php";
if (isset($_GET['deleteid'])) {
$id = $_GET['id'];
$session_id = $_SESSION['userid'];
}
// Mark from_delete only if user is the sender
$stmt = $con->prepare("UPDATE pm SET from_delete = ? WHERE id = ? AND from_id = ?");
$stmt->bind_param("sss", $session_id, $id, $session_id);
if ($stmt->execute()) {
echo"deleted successfully";
} else {
echo "Failed to delete<br/>";
}
// Mark to_delete only if user is receiver
$stmt = $con->prepare("UPDATE pm SET to_delete = ? WHERE id = ? AND to_id = ?");
$stmt->bind_param("sss", $session_id, $id, $session_id);
if ($stmt->execute()) {
echo"deleted successfully";
} else {
echo "Failed to delete<br/>";
}
来源:https://stackoverflow.com/questions/59429426/how-to-update-session-empty-value