bettina702 Posted May 22, 2009 Share Posted May 22, 2009 Hi, I am a total newbie when it comes to PHP/MySQL. The project that I am working on requires me to generate a password, insert it and overwrite it in MySQL and then e-mail it out. For the most part it is working, I am e-mailing it and something is being inserted into MySQL, but the field is blank. Its as if the password generator isn't working or I am just missing something in my code. Please take a look. Any help is appreciated at this point! <?php $link = mysql_connect("a","b","c"); if (!$link) { die('I cannot connect to the database beacuse: ' . mysql_error()); } $db_selected = mysql_select_db("my_db" , $link); if (!$db_selected) { die("Can't connect: " . mysql_error()); } function generatePassword ($length = 5) { // start with a blank password $password = ""; // define possible characters $possible = "0123456789bcdfghjkmnpqrstvwxyz"; // set up a counter $i = 0; // add random characters to $password until $length is reached while ($i < $length) { // pick a random character from the possible ones $char = substr($possible, mt_rand(0, strlen($possible)-1), 1); // we don't want this character if it's already in the password if (!strstr($password, $char)) { $password .= $char; $i++; } } // done! return $password; } mysql_query("INSERT INTO Table (field) VALUES('$password')") or die('I cannot insert into the Database because: ' . mysql_error()); $today = date("l, F j, Y"); $message = <<<ENDFORM Here is the new password for {$today}. Password = {$password}. Visit "http://www.domain.com" and log in. ... ENDFORM; $recipient1 = "Me <me@domain.com>"; $recipient2 = "Me <me@domain.com>"; $subject = "The Code for {$today}"; $headers .= "To: $recipent1"."\r\n"; $headers .= "From: $recipient2"."\r\n"; $headers .= "Bcc: $recipient2, $recipient1"."\r\n"; $headers .= "Reply-To: $recipient2"."\r\n"; $headers .= "X-Mailer: PHP-Mailer"; mail( "$recipient1, $recipient2", $subject, $message, $headers); mysql_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/159276-solved-password-generator-not-working-with-mysql/ Share on other sites More sharing options...
jackpf Posted May 22, 2009 Share Posted May 22, 2009 It doesn't look like you're calling the function to create the password. Quote Link to comment https://forums.phpfreaks.com/topic/159276-solved-password-generator-not-working-with-mysql/#findComment-840036 Share on other sites More sharing options...
bettina702 Posted May 22, 2009 Author Share Posted May 22, 2009 Hi Jack, Can you tell me how I do that? Thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/159276-solved-password-generator-not-working-with-mysql/#findComment-840082 Share on other sites More sharing options...
jackpf Posted May 22, 2009 Share Posted May 22, 2009 Like this - $password = generatePassword(); You also spelt recipient wrong in your headers (line 49) No problem Quote Link to comment https://forums.phpfreaks.com/topic/159276-solved-password-generator-not-working-with-mysql/#findComment-840098 Share on other sites More sharing options...
bettina702 Posted May 22, 2009 Author Share Posted May 22, 2009 Awesome, that worked!! Now the next part of the issue: Overwriting the previous password. I know the INSERT INTO thing is not how one does that. I tried using UPDATE this way: mysql_query("UPDATE table SET field WHERE field=1") or die('I cannot insert into the Database because: ' . mysql_error()); But it gives me this error message: I cannot insert into the Database because: 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 'WHERE password=1' at line 1 Can you help me with that? Quote Link to comment https://forums.phpfreaks.com/topic/159276-solved-password-generator-not-working-with-mysql/#findComment-840108 Share on other sites More sharing options...
jackpf Posted May 22, 2009 Share Posted May 22, 2009 Yeah, you need to set `field` to something. Look at your code. How does mysql know what to update `field` to? Try something like this: mysql_query("UPDATE table SET `password`='$password' WHERE field=1"); Quote Link to comment https://forums.phpfreaks.com/topic/159276-solved-password-generator-not-working-with-mysql/#findComment-840110 Share on other sites More sharing options...
bettina702 Posted May 22, 2009 Author Share Posted May 22, 2009 OK. I have it e-mailing now, but the code does not overwrite MySQL: mysql_query("UPDATE table SET column='$password' WHERE column='1'") Quote Link to comment https://forums.phpfreaks.com/topic/159276-solved-password-generator-not-working-with-mysql/#findComment-840119 Share on other sites More sharing options...
jackpf Posted May 22, 2009 Share Posted May 22, 2009 Put or die(mysql_error()); after the query. And are you sure there's a field called column with a value of 1? Quote Link to comment https://forums.phpfreaks.com/topic/159276-solved-password-generator-not-working-with-mysql/#findComment-840131 Share on other sites More sharing options...
bettina702 Posted May 22, 2009 Author Share Posted May 22, 2009 And are you sure there's a field called column with a value of 1? What I mean by column='1' is that I want the new password to overwrite the old one that is located in the one and only field in the database. Clearly thats not working. How do I get the new password to overwrite the old one? Quote Link to comment https://forums.phpfreaks.com/topic/159276-solved-password-generator-not-working-with-mysql/#findComment-840172 Share on other sites More sharing options...
jackpf Posted May 22, 2009 Share Posted May 22, 2009 That's not what the query does. What it does is updates the record that has "1" as the value of `column`. Maybe you should have a look at mysql update syntax. Try this mysql_query("UPDATE table SET column='$password' LIMIT 1"); Quote Link to comment https://forums.phpfreaks.com/topic/159276-solved-password-generator-not-working-with-mysql/#findComment-840246 Share on other sites More sharing options...
bettina702 Posted May 22, 2009 Author Share Posted May 22, 2009 Fantastic. Its working now. Thanks so much Jack!! Quote Link to comment https://forums.phpfreaks.com/topic/159276-solved-password-generator-not-working-with-mysql/#findComment-840284 Share on other sites More sharing options...
jackpf Posted May 22, 2009 Share Posted May 22, 2009 Np Quote Link to comment https://forums.phpfreaks.com/topic/159276-solved-password-generator-not-working-with-mysql/#findComment-840317 Share on other sites More sharing options...
fenway Posted June 1, 2009 Share Posted June 1, 2009 That's not what the query does. What it does is updates the record that has "1" as the value of `column`. Maybe you should have a look at mysql update syntax. Try this mysql_query("UPDATE table SET column='$password' LIMIT 1"); Fantastic. Its working now. Thanks so much Jack!! Um... no where clause? Quote Link to comment https://forums.phpfreaks.com/topic/159276-solved-password-generator-not-working-with-mysql/#findComment-846833 Share on other sites More sharing options...
jackpf Posted June 28, 2009 Share Posted June 28, 2009 the one and only field in the database ....? Quote Link to comment https://forums.phpfreaks.com/topic/159276-solved-password-generator-not-working-with-mysql/#findComment-865161 Share on other sites More sharing options...
fenway Posted June 28, 2009 Share Posted June 28, 2009 the one and only field in the database ....? Oh. Quote Link to comment https://forums.phpfreaks.com/topic/159276-solved-password-generator-not-working-with-mysql/#findComment-865185 Share on other sites More sharing options...
jackpf Posted June 28, 2009 Share Posted June 28, 2009 Lol Quote Link to comment https://forums.phpfreaks.com/topic/159276-solved-password-generator-not-working-with-mysql/#findComment-865231 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.