php / mysql成员和标记功能

时间:2014-05-12 15:55:49

标签: php mysql database

我正在为一个志愿者团队创建留言板。在这个板上,人们可以写信息。这些消息可以标记为已读。在每条消息下面会显示一张包含成员照片的表格。此照片的网址存储在数据库中。如果会员点击他的照片,这张照片会变成另一张图片,所以他知道他的消息是红色的。这个功能工作得很好。

但是所有这些照片都会手动添加到脚本中。这意味着每当一名新志愿者加入我们的俱乐部时,我都必须更改代码(添加他的名字)以便让他也进行标记。

我需要的是志愿者自动连接到桌子上。因此,如果我在数据库中添加志愿者名称(不编辑代码),他的照片也会显示出来。每当我删除一名志愿者时,他的照片就不再显示了。

我可以通过创建一个包含所有志愿者姓名的数据库并在消息下方创建一个mysql_fetch_array函数来实现此目的。但是当我这样做并且志愿者A点击他的照片时,所有志愿者A的照片都会改变,而不仅仅是他刚读过的消息,所有其他消息也被标记了。我需要每条消息都有一行可以标记的唯一照片,这样用户就知道他已经标记了单个消息,而不是所有消息。

如何实现这一目标?

我现在拥有的:

数据库db_users

id (AI), primary
name (varchar), 255
address (varchar), 255
email (varchar), 255
stillActive (varchar), 5 //if `value` is set to yes, his photo is displayed beneath a message. If `value` is set to no, his photo won't be visibile anymore.

数据库db_messages

 id (AI), primary
 name (varchar), 255 //the name wo entered the message
 message (varchar), 999 //the message itself
 urlUserA // the url of the photo of volunteer A
 urlUserB // the url of the photo of volunteer B
 urlUserC // the url of the photo of volunteer C

mark_read.php

$sql2 = "SELECT * FROM $tbl_name WHERE id = ".$_GET['id']; //to get the specific message
$result2 = mysql_query($sql2);
$url = $_GET['url']; // the url of the MARKED image             
$recover = $_GET['recover']; // the url of the UNMARKED image       
$tabel = $_GET['tabel'];
$id = $_GET['id'];
$date = $_GET['date'];
$row = mysql_fetch_assoc($result2);         
$tabel_content = $row[$tabel];

if ($tabel_content == $url){
    $sql = "UPDATE " . mysql_real_escape_string($tbl_name) .
       " SET ".$_GET['tabel']." = '".$_GET['recover'].
       "' WHERE id = ".$_GET['id'];
  $result = mysql_query($sql);
} 
elseif ($tabel_content == $recover) {
    $sql = "UPDATE " . mysql_real_escape_string($tbl_name) .
       " SET ".$_GET['tabel']." = '".$_GET['url'].
       "' WHERE id = ".$_GET['id'];
  $result = mysql_query($sql);
}

上面的if代码用于查看用户是否已标记该消息。如果已标记并且他再次点击他的照片,则照片将再次取消标记。

如您所见,如果我需要添加新的志愿者,我必须向数据库添加一个表,更改add_message.php的代码。很多工作。我需要一些东西,每当我将用户添加到db_user并保存它时,他也能够标记消息,而无需在他可以使用它之前创建新表urlUserD

非常感谢您对此的任何帮助。

--- --- UPDATE

好的,使用此页面添加消息:

addMSG.php

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$subject = $_POST['subject'];
$date = $_POST['date'];
$message = $_POST['message'];
$name = $_POST['name']; // the name from the person who posted the msg

$sql="INSERT INTO $tbl_name(
date,
archive,
year, 
message, 
name,
subject
)
VALUES(
'$date',
'no',
'2014', 
'$message', 
'$name',
'$subject'
)";

$result = mysql_query($sql);

我如何(以及在​​何处)添加您的代码以便将所有用户绑定到新创建的消息?

1 个答案:

答案 0 :(得分:0)

您遇到了一些数据库架构问题,在您修复它们之前,这些问题将继续让您的生活变得困难。具体来说,您应该有一个表格,用户将用户与他们的消息联系起来。所以你的架构可能看起来更像这样:

<强> db_users

id (AI), primary
name (varchar), 255
address (varchar), 255
email (varchar), 255

<强> db_messages

id (AI), primary
created_by, // user id who create the message. Don't use name here!
message (varchar), 999 //the message itself

<强> db_message_queue

message_id // foreign key to db_messages.id  first column in compound primary key
user_id // foreign key to db_users.id  second column in compound primary key
message_read // tinyint field with 0/1 value to indicate whether message has been read

现在,在创建消息时,您将在消息表中添加一行,并在消息队列中为所有预期的收件人添加一行。当预期收件人读取队列中的邮件时,您将其标记为已读。