How to update session empty value

荒凉一梦 提交于 2019-12-24 18:38:53

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!