Actualizar un campo después de enviar un correo
Frecuentes
Visto 130 veces
0
I have a reminder mail sent to those who do not log on to my site after 30 days. Earlier I got an answer on this forum to create a seperate field and then update it as mentioned here: Necesito ayuda con la marca de tiempo de Unix.
I have created a new field lastprelogin
, now how do I update the respective field when I send the mail for inactive users.
<?php
include("include/data.php");
$query = "SELECT * FROM myusers WHERE DATE_ADD(FROM_UNIXTIME(lastprelogin), INTERVAL 15 DAY) < CURDATE()";
$result = mysql_query($query);
$num = mysql_numrows($result);
$i = 0;
while ($i < $num)
{
//send mail code
$sendmail = mail($to,$subject,$msg,$headers);
$i++;
if($sendmail) {
$query1 = "UPDATE myusers SET lastprelogin='".time()."'";
$result2 = mysql_query($query1);
}
}
?>
How can I update the respective user's lastprelogin
field after sending the mail?
I am lost here beccause am unable to understand the logic in this part.
3 Respuestas
4
You need to loop through your results by using mysql_fetch_assoc o función similar.
Your update query needs to include the ID of the record you wish to update.
Debes no utilizar el mysql_*
functions anymore as they are becoming deprecated soon. Use mysqli
<?php
include("include/data.php");
$query = "SELECT * FROM myusers WHERE DATE_ADD(FROM_UNIXTIME(lastprelogin), INTERVAL 15 DAY) < CURDATE()";
$result = mysql_query($query);
while ($user = mysql_fetch_assoc($result))
{
//send mail code
$sendmail = mail($user['email_address'],$subject,$msg,$headers);
$i++;
if($sendmail){
$query1 = "update myusers set lastprelogin='".time()."' WHERE id = " . $user['id'];
$result2 = mysql_query($query1);
}
}
?>
contestado el 03 de mayo de 12 a las 15:05
will try this and let you know here - samry
1
The logic of Your script is simple:
- retrieve all the users that didn't log in for last 15 days
- send an email to each user
- and if that succeeds also update the field lastprelogin for that user
You have some important errors within Your script and this should be like this:
include("include/data.php");
$query = "SELECT * FROM myusers WHERE DATE_ADD(FROM_UNIXTIME(lastprelogin), INTERVAL 15 DAY) < CURDATE()";
$result = mysql_query($query);
while($user = mysql_fetch_assoc($result)) {
// assuming that myusers table has these columns: user_id, user_name, user_email, lastprelogin
//send mail code
if(mail($user['user_email'],'Please log in','Please login to my site',$headers)) {
$query1 = "update myusers set lastprelogin='".time()."' where user_id = {$user['usri_id']}";
$result2 = mysql_query($query1);
}
}
// end.
Como $headers
variable You can set a From
header, etc. Look for PHP mail function here: http://php.net/mail
Also the right query for updating should be this one:
"update myusers set lastprelogin='".time()."' where user_id = {$user['user_id']}"
anyway You will update the lastprelogin
of all users everytime...
contestado el 03 de mayo de 12 a las 14:05
1
You will need to get some id
a partir de su myuser
table and run the update
consulta con where id = $id
.
contestado el 03 de mayo de 12 a las 14:05
I do get id something like $id = mysql_result($result,$i,"UID"); but will taht work? - samry
It sounds like it should work, but without knowing the exact code and database structure, I can't be sure. I'd say: try it. Maybe on some backup environment where it's safe to try it on in case it goes wrong... - Bartlaarhoven
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas php mysql or haz tu propia pregunta.
¿Por qué estás actualizando? todos los users on every run through the loop? - Cylindric
I have put that to get answer, am not updating as of now - sammry
Wait, so you've put something in your sample that you're not actually doing? I'm not sure how we can guess that. Put the code that's failing, not what you think is wrong. - Cylindric
You can add new field
active
a tu mesamyusers
and set it to "0". After sending the e-mail set it to "1" with respect touser_id
. - alkhader@alkhader I wouldn't recommend adding confusing semantics to it. Sending them an email saying "come back" does not make them "active". - Cylindric