Page 1 of 1

[SOLVED]Inserting a row with one changed field

Posted: Tue Jul 27, 2004 6:29 pm
by evilmonkey
Hello. I want to make a mass Private Messaging system, but I'm not sure how to do it. My table is set up like this: message_id, sender, recipient, message, date. Mesasge_id is auto-incremented in the database, the sender stays constant (the admin that is sending this message), but the recipient has to change. I have a table of my users that contains the id's of every user (the user_id is what needs to go into the 'recipient'). I need a hint on the fastest way to do this, because we expect to have a lot of users. Any ideas?

Thanks!

Posted: Tue Jul 27, 2004 6:39 pm
by kettle_drum
Yo do it just as you are doing it. As for the fastest way, you would have to test a few methods and see which was fastest.

Posted: Tue Jul 27, 2004 6:45 pm
by kettle_drum
Opps cut the methods off.

Try to just loop though posting the query. Could also just have one entry in the database if it applies to all users or a user group. Then you can just keep a table of if that user has deleted the post if you allow them to.

Posted: Tue Jul 27, 2004 6:46 pm
by evilmonkey
I had something like this in mind:

Code: Select all

<?php
//class declaration, other functions above...
function massmail ($message) {
$result = mysql_query("SELECT id FROM `users`");
while ($row=mysql_fetch_array($result)) {
$date = date("F d, Y");
mysql_query ("INSERT INTO `messages` (`sender`, `recipient`, `message`, `date`) VALUES ('$this->id', '".$row['id']."', '$message', '$date')", $db_link);
}
return true;
}
?>
Is there a better way?

Posted: Tue Jul 27, 2004 6:46 pm
by feyd
might be a good thing to have in a stored procedure.

Posted: Tue Jul 27, 2004 6:59 pm
by evilmonkey
The code I posted above, any way to make it better?

Posted: Tue Jul 27, 2004 7:14 pm
by kettle_drum
Yeah, move $date = date("F d, Y"); out of the while loop - no point in calculaing it over and over again.

Posted: Tue Jul 27, 2004 7:33 pm
by evilmonkey
Oh, woops, yeah, it's actually not suppossed to be there. Thanks. :)

Posted: Tue Jul 27, 2004 9:25 pm
by feyd
maybe the date should be in unix timestamp form? depends on if users can set their own time views..

Posted: Wed Jul 28, 2004 11:04 am
by evilmonkey
Hmmm...I'll consider that. Thanks feyd.