Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a php code that gets unread (unread messages has a column called flag that is always = 0) messages from the database and echoes them out. The problem here is I want that when the user clicks on the close button (which is in form of a form) it updates all the flag columns of that particular messages = 1. But I tried and nothing seems to happen. Below is my code and please I am new to php so easy. Thanks in advance.

This below is code to get the unread messages
require_once ("db.php");
$db = new MyDB();
session_start();

$log_id = $_SESSION['log_id'];
$unread = 0;

$sql =<<<EOF
SELECT *, COUNT(group_hash) AS NumOccurrences FROM messager WHERE from_id != '$log_id' AND flag = '$unread' GROUP BY group_hash, message ORDER BY from_id DESC; 
EOF;

$ret = $db->query($sql);

$by_hash = array();

while ($row = $ret->fetchArray(SQLITE3_ASSOC))
{
$hash = $row['group_hash'];
$message = $row['message'];
$from_id = $row['from_id'];

  if(!isset($by_hash[$hash])) $by_hash[$hash] = '';
  $by_hash[$hash] .= $message;

  $ssql =<<<EOF
SELECT * FROM connect WHERE user_one != '$log_id' OR user_two != '$log_id' AND hash = '$hash';
EOF;
  $sret = $db->query($ssql);

  $by_name = array();
  while ($srow = $sret->fetchArray(SQLITE3_ASSOC))
  {
      $hash = $srow['hash'];
      $_SESSION['hasher'] = $hash;
      $user_one = $srow['user_one'];
      $user_two = $srow['user_two'];

      if ($user_one == $log_id) {
          $select_id = $user_two;
      } else {
          $select_id = $user_one;
      }

      $sesql = <<<EOF
SELECT * FROM User WHERE ID = '$select_id';
EOF;
      $seret = $db->query($sesql);

      while ($serow = $seret->fetchArray(SQLITE3_ASSOC)) {
          $select_uname = $serow['fname'];
          $select_uimg = $serow['image'];

      }
  }

}

foreach ($by_hash as $key => $str)
{
echo "<form action='make_read.php' id='upd_unread' method='post' enctype='multipart/form-data'>
<input type='submit' name='make_read' id='make_read' class='not_bubble_closer' value='⨯'>//clicking here updates all $hash with flag= 0 to 1
</form>
<div class='not_msg_area'>";
        echo "<div class='not_msg_sender'><p>From: ".$select_uname."</p><img src='$select_uimg'></div>";
        echo "<div class='not_msg_display'><div class='not_bubble'><p>".$str."</p></div></div>";
        echo "<div class=\"not_msg_rep\">
            <form action=''' method='post' enctype='multipart/form-data'>
                <input type='text' name='not_msg_reply' id='not_msg_reply' placeholder='reply (press enter to send)'>
            </form>
        </div>";
    echo "</div>";
?>  


What I have tried:

Code to update

require_once ("db.php");
$db = new MyDB();

if (isset($_POST['make_read']) && isset($_GET['group_hash']) && !empty($_GET['group_hash']))
{

$log_id = $_SESSION['log_id'];
$hash = $_SESSION['hash'];
$read = 1;
$unread = 0;

$sql =<<<EOF
UPDATE messager SET flag = '$read' WHERE flag = '$unread' AND group_hash = '$hash';  
EOF;

$ret = $db->exec($sql);

if ($ret)
{
    echo "<script>alert('successful')</script>";
}
else
{
    echo "Nah!..";
}
}
Posted
Updated 1-Mar-17 5:55am

1 solution

You are using post method in the form, but in the script you are referencing $_GET variables.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900