Jump to content

PHP insert in MySQL database table in random order?


Metasearching

Recommended Posts

Hi,

I'm an researcher (and complete coding noob), and am planning a longitudinal study that requires e-mail follow-up with subjects taking an initial survey. For purposes of ethics/anonymity due to sensitive survey data, I'd like the acquired e-mails to be saved uncoupled from the survey responses; this is simple to deal with, and I use a basic PHP e-mail form, which injects the email address in a table in MySQL in a different server than the one used for the survey.

 

The issue is that the e-mails are saved in the MySQL database in order of injection, thus it is still theoretically possible for me to link the e-mails back to the survey responses (which have a time stamp that I cannot remove).  Ideally I would like not to be able (at all) to link the e-mails to the survey responses, and one way to do that (since I don't save the e-mail injection timestamps in MySQL) might be to have the e-mails saved in MySQL in a random order. Not sure if this is possible, and not even sure if this would be via PHP or MySQL side of things. The server is on godaddy and uses Starfield interface for MySQL but I cannot find an option for random insert/saving of table items (emails). They are saved in order of injection.

Any solution for this?

Thanks,

 

Link to comment
Share on other sites

  • 2 weeks later...

Not sure if you got your answer elsewhere yet (I'm new too and didn't realize this was the wrong place for my post, lol):

 

Probably the easiest way to do this would be to not have a primary or unique key in your MySQL table. If you do need some kind of unique handle to sort them by or make it easier to delete them, generate a long random string (20 bytes ought to do the trick) and use that as the index field.

 

MYSQL

CREATE TABLE randomized_responses (randomID VARCHAR(20), response TEXT, PRIMARY KEY(randomID));

 

PHP

$letters = array("A","a","B","b"...);
$randomstring = "";
for ($i=0;$i<20;$i++) {
  $randomstring .= $letters[rand(0,51)];
}
$q = mysql_query("INSERT INTO randomized_responses (randomID, response) VALUES('$randomstring','$response');", $sqlConn); //remember to use mysql_real_escape_string to clean the $response of malicious SQL injection before putting it into the database...
if (mysql_affected_rows($sqlConn)<1) {
    //That random key was already used. Get another key and try again. Obviously putting this whole thing in a function and returning affected rows makes sense if you're worried about collisions.
}

 

Hope this helps...

Link to comment
Share on other sites

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.