drvxnivoqf17568697 2014-05-12 15:55
浏览 37

php / mysql成员和标记功能

I'm creating a messageboard for a team of volunteers. On this board people can write messages. These messages can be marked as read. Underneath each message a table with photos of the members is shown. The url of this photo is stored in a database. If a member clicks on his photo this photo changes to another image, so he know he has red the message. This function is working just fine.

But all these photo's are added manually into the script. This means that whenever a new volunteer joins our club, I have to change the code (add his name) in order to have him marking too.

What I need is that the volunteers are automatically connected to the table. So, if I add a volunteers name into the database (without editing the code), his photo will be displayed too. And whenever I delete a volunteer, his photo won't be displayed anymore.

I can achieve this by creating a database holding all volunteers names and create a mysql_fetch_array function right beneath the message. But when I do this and volunteer A clicks his photo, all photos of volunteer A changes, not just fot the message he just has read, also all the other message are marked too. I need that every message gets a unique row of photos that can be marked, so that the user knows he has marked that single message, not all of them.

How can this be achieved?

What I have right now:

The database 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.

The database 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

The 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);
}

The if code above is used to see whether the user already marked the message or not. If it is marked and he'll click his photo again, the photo will be unmarked again.

As you can see, if I need to add a new volunteer, I have to add a table to the database, change the code of add_message.php. A lot of work. I need something that whenever I add a user into the db_user and save it, he'll be able to mark messages too, without having to create a new table urlUserD before he can use it.

Any help on this will be very much appreciated.

---UPDATE---

Okay, the message is added using this page:

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);

How (and where) do I add your code in order to bind all the users to the newly created message?

  • 写回答

1条回答 默认 最新

  • dpmpa26468 2014-05-12 16:16
    关注

    You have some database schema problems, that until you fix them, will continue to make your life difficult. Specifically you should have a table that relates users to the messages intended for them. So your schema might better look like this:

    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
    

    Now, when a message is created, you would add a row into the messages table, as well a rows in the message queue for all intended recipients. When the message in the queue is read by the intended recipient, you would mark it as read.

    评论

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示