Select from MYSQL changing data before going to the insert function

Discussion in 'PHP' started by Winnstorm, Apr 3, 2011.

  1. #1
    Hi to all

    Im new to this community so i hope i could learn well about PHP programming im like a beginner so im needing some help

    Well going to the point, im needing to obtain with a select from MYSQL in a php file the data from a database that cotains user signup and logon data with the following camps:


    INSERT users (email, username, display, password)
    SELECT email, username, username, passw
    FROM signup

    Now the problem is that im needing to encrypt these passwords before they go to the INSERT function of mysql so they could have more security adding MD5 and salt encryption to these clear passwords, how i can do to encrypt the passwords from the select before sending them to the insert function? what kind of php function i've to use to do that so i could insert the complete encrypted password on the new database??


    Thank you to all!

    Regards!
     
    Winnstorm, Apr 3, 2011 IP
  2. higibigi

    higibigi Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    When inserting data into a table like you're doing there, everything is usually in a variable. So before the MySQL syntax is executed, just update the variable with the encryption.

    Example: $password is your variable and it contains the customers password.

    $password = MD5($password);

    This should do it! :)
     
    higibigi, Apr 3, 2011 IP
  3. Winnstorm

    Winnstorm Member

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    But, i want that for all the entries on the user database, please could you explain me in detail where i've to put it??

    Also thanks for the reply ;)
     
    Winnstorm, Apr 3, 2011 IP
  4. higibigi

    higibigi Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Ahhh, it's hard to tell you exactly where to put the variable being updated to MD5 aside from telling you to put it before any MySQL syntax you're using to insert this stuff into the database. Every website is different so I'm basically blind in telling you where to put it. Unless you're using a popular application I'm familiar with I could maybe tell you :p If you wanted to get back to me if you're using an application and which application it is, I might be able to help more.

    Regarding the "I want that for all the entries on the user database". What I'm explaining should work. Unless you're referring to the current entries that are already in there. hmmm if that's the case then you could probably just run a query that will MD5 all the current logins, but again that is hard to being blind and all... and that will only work for the current entries.
     
    higibigi, Apr 3, 2011 IP
  5. leunamer

    leunamer Peon

    Messages:
    65
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    From what i understand, you wanted to migrate information from a 'signup' table to other database table right? if that so, you could probably excute a "select" query and loop the result. for example:

    
    $result = mysql_query("SELECT email, username, display, passw FROM signup");
    while($row = mysql_fetch_array($result)){
    $encrypted_password = MD5($row['passw']);
    $insert = mysql_query("INSERT users (email, username, display, password)VALUES('$row[email]','$row[username]','$row[display]','$encrypted_password')")
    }
    
    
    Code (markup):

    I just want to elaborate what @higibigi said.

    Thanks,,
     
    leunamer, Apr 3, 2011 IP
  6. Winnstorm

    Winnstorm Member

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #6
    oh thats it!! thank you very much,

    And apart from that for the future will be any way to read the password with the select? I mean if i need to migrate again the database and i use MD5 encryption is any way to do the SELECT from the database and it converts the md5 password to a clean password so i could encrypt it to another way?

    In resume i want the same procedure but it will be in the other way to decrypt the md5 security to reinsert it again with another encryption (like using double md5 and salt for vbulletin compatibility)
     
    Last edited: Apr 4, 2011
    Winnstorm, Apr 4, 2011 IP
  7. leunamer

    leunamer Peon

    Messages:
    65
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    "I mean if i need to migrate again the database and i use MD5 encryption is any way to do the SELECT from the database and it converts the md5 password to a clean password so i could encrypt it to another way?"

    There's no other way to decrypt an MD5-encrypted value. If you wanted to do such things as encryption and dycryption... you might probably use custom classes for encryption and decryption or vise versa.
     
    leunamer, Apr 4, 2011 IP