使用PHP更新输入单击时的数据库行 [英] Update database row on input click using PHP
问题描述
我有一个未读取的PHP代码(未读消息有一个名为flag的列,总是= 0)来自数据库的消息并回显它们。这里的问题是我希望当用户点击关闭按钮(以表单的形式)时,它会更新该特定消息的所有标志列= 1.但是我尝试过,似乎没有任何事情发生。下面是我的代码,请我是php新手这么容易。在此先感谢。
以下是获取未读消息的代码
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!..";
}
}
推荐答案
db = new MyDB();
session_start();
db = new MyDB(); session_start();
log_id =
_SESSION ['log_id'];
_SESSION['log_id'];
这篇关于使用PHP更新输入单击时的数据库行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!