AngstyG Posted June 16, 2010 Share Posted June 16, 2010 I'm trying to figure out the best way to email multiple users a new password. My MySQL table looks something like: ID | Name | Password | Email | Reset I'd like to: "UPDATE MyUsersTbl SET password = '" . md5($MD5_PREFIX . $newpass) . "' WHERE Reset = 1"; Or... something like that. And then I'd like it to email the user the new password. I just have no idea where to even begin. I'm familiar with the MD5, which my site uses, but am a native ASP dev, and only just beginning to delve into PHP. Can anyone point me in the right direction? It'd be greatly appreciated. Quote Link to comment Share on other sites More sharing options...
kratsg Posted June 16, 2010 Share Posted June 16, 2010 Two things: 1) Mail Function - Best method is to use the mail() function: http://us2.php.net/manual/en/function.mail.php. The page should be somewhat self-explanatory. 2) Loop - You'll need some loop that goes through all users to do this Here's what your code structure would look like //some query to select all users to update $query = mysql_query("SELECT `Email`,`ID` FROM MyUsersTbl WHERE Reset = 1"); while($result = mysql_fetch_array($query)){ //update query for new password //might also want to set Reset = 0 as well //email user new password } Quote Link to comment Share on other sites More sharing options...
AngstyG Posted June 16, 2010 Author Share Posted June 16, 2010 Ahh, thanks for the reply! I've been playing with this for a few hours now, and I'm almost certain I've got the actual stuff (email and update table) within the loop figured out, but I can't seem to get my loop to work. :-\ // Run the shiz if the button is pushed if (isset($_POST['action']) && $_POST['action'] == 'ok') { // some query to select all users to update $query = mysql_query("SELECT * " . $DBPrefix . "users WHERE Reset = 1"); // Default amount of records being looped is zero, so later we can see if this works $testme = 0; while ($result = mysql_fetch_array($query)) { // Add 1 for each record being correctly looped $testme = $testme + 1; } } But even though I have 10 users in the database with "Reset" set to "1", the test "$testme" output always displays 0 (logically, it would display 10). Can any fresh set(s) of eyes spot what's making this loopless? Ahhhh, I'm just now learning loops for php, so forgive my ignorance. On the upside... I have my conditionals down pat. P.S. Thanks for the tip on the setting Reset to 0. Awesome suggestion is awesome. Quote Link to comment Share on other sites More sharing options...
kratsg Posted June 16, 2010 Share Posted June 16, 2010 Can you var_dump("SELECT * " . $DBPrefix . "users WHERE Reset = 1"); And see what that output is? Also, try: mysql_query("stuff...") or die('There was an error in the query!'.mysql_error()); and see if there was an error with the original query. Ahh, thanks for the reply! I've been playing with this for a few hours now, and I'm almost certain I've got the actual stuff (email and update table) within the loop figured out, but I can't seem to get my loop to work. :-\ // Run the shiz if the button is pushed if (isset($_POST['action']) && $_POST['action'] == 'ok') { // some query to select all users to update $query = mysql_query("SELECT * " . $DBPrefix . "users WHERE Reset = 1"); // Default amount of records being looped is zero, so later we can see if this works $testme = 0; while ($result = mysql_fetch_array($query)) { // Add 1 for each record being correctly looped $testme = $testme + 1; } } But even though I have 10 users in the database with "Reset" set to "1", the test "$testme" output always displays 0 (logically, it would display 10). Can any fresh set(s) of eyes spot what's making this loopless? Ahhhh, I'm just now learning loops for php, so forgive my ignorance. On the upside... I have my conditionals down pat. P.S. Thanks for the tip on the setting Reset to 0. Awesome suggestion is awesome. Quote Link to comment Share on other sites More sharing options...
AngstyG Posted June 16, 2010 Author Share Posted June 16, 2010 Can you var_dump("SELECT * " . $DBPrefix . "users WHERE Reset = 1"); And see what that output is? Also, try: mysql_query("stuff...") or die('There was an error in the query!'.mysql_error()); and see if there was an error with the original query. Ah, yes, it IS an sql error. There was an error in the query! You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mysite_users WHERE Reset = 1' at line 1 Huh. I'm not really certain what the issue could be. I even matched the case of the table name, with "R" being upper and "eset" being lower. I'm sure it's something obvious and easy. Isn't is usually? Thanks again for all the help. You're kinda making my whole night. Which doesn't make me lame. At all. Quote Link to comment Share on other sites More sharing options...
AngstyG Posted June 16, 2010 Author Share Posted June 16, 2010 Well, this is embarrassing... ("SELECT * " . $DBPrefix . "users WHERE Reset = 1"); ("SELECT * FROM " . $DBPrefix . "users WHERE Reset = 1"); I knew it'd be something ridic obvious. Quote Link to comment Share on other sites More sharing options...
AngstyG Posted June 16, 2010 Author Share Posted June 16, 2010 Ok, when I put the email script in, it works great. Loops the full 10 times, emails each person with the randomized MD5 password, etc and so on. JOY! But for some reason, my UPDATE query breaks the loop. It only updates the first row's password and emails only to that user. // Run the shiz if the button is pushed if (isset($_POST['action']) && $_POST['action'] == 'ok') { // Some query to select all users to update $query = mysql_query("SELECT * FROM " . $DBPrefix . "users WHERE Reset=1") or die('There was an error in the query! '.mysql_error()); // Default amount of records being looped is zero, so later we can see if this works $testme = 0; while ($res = mysql_fetch_array($query)) { // Add 1 for each record being correctly looped $testme = $testme + 1; // Gather some information about the user $email = $res['email']; $id = $res['id']; $name = $res['name']; // Generate a new random password for the user $newpass = substr(uniqid(md5(time())), 0, 6); // Update database $query = mysql_query("UPDATE " . $DBPrefix . "users SET password = '" . md5($MD5_PREFIX . $newpass) . "' WHERE id = '$id'") or die('There was an error in the query! '.mysql_error()); // Get super awesome mail function from included file $emailer = new email_class(); // Set yummy template variables omnomnom $emailer->assign_vars(array( 'USERNAME' => $name, 'NEWPASS' => $newpass )); // lol forgot wtf this was even for, but I'm sure it's important... somehow... $emailer->email_uid = $id; // Sendsies! $emailer->email_sender($email, 'newbatchpasswd.inc.php', $MSG['024']); } } It works flawlessly until this code is added: // Update database $query = mysql_query("UPDATE " . $DBPrefix . "users SET password = '" . md5($MD5_PREFIX . $newpass) . "' WHERE id = '$id'") or die('There was an error in the query! '.mysql_error()); I get no mysql errors or anything. It works perfect for the first row. Hmmm. Do I need to nest another loop, or is this just something broken by my method? Thanksya! Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted June 16, 2010 Share Posted June 16, 2010 You're reusing the variable "$query" within a while loop that's using it. Use a different variable. Ken Quote Link to comment Share on other sites More sharing options...
AngstyG Posted June 16, 2010 Author Share Posted June 16, 2010 Ahh, thanks Ken! That definitely did the trick. All 10 emails are now being sent, and all records updated accordingly. I have logged in with each of them and find them to be accurate passwords. However, for any interested parties who comes across this in a search, please note that my use of: $newpass = substr(uniqid(md5(time())), 0, 6); is bad bad bad, as the password is being radomized against the time. And, as you know, all executions take place at the same moment, so the generated key will be the same for each. This is not good. Instead, I used: $newpass = substr(uniqid(md5($name), true), 0, 6); Which uses the username for reference, since it will be unique per every user/loop. HOORAY! Works like a charm! Thanks for the help, guise! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.