Jump to content

Recommended Posts

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

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?

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?

  • 2 weeks later...

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?

  • 4 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.