I have a table with over a thousand records that I need to update a field I have called "id". I need each record's ID to be unique. I am using the uniqid() function which works just fine. When I use the code below (and several other variations of it), all records have the same value as the last record. The screen output shows each ID to be different. I even substituted the For Loop counter and all the records had the same value. How can I update each record so each has a different value? This program will probably be run only once so efficiency in speed is not an issue.

Screen Display
    Row     ID
    0       51ff1474280a0
    1       51ff14742c475
    2       51ff14742ffcb
    3       51ff147433c21
    1910    51ff1493082b1
    1911    51ff14930c142



$resultset = mysql_query("SELECT * FROM mailing");

for ($i=0; $i<=1912; $i++)  {
    mysql_data_seek($resultset,$i);
    $id = uniqid();
    $query="UPDATE mailing SET id_key='$id'";                               
    mysql_query($query) or die ("Error in query: $query <br>" . error_message);
    echo $i . "  " . $id . "<br>";
  }     

Your update query runs on ALL records, so you'll have to add a WHERE so it runs on only one at a time.

Thanks pritaeas. I was not quite sure of the Where clause structure so I created a new field called "rec" that is of the auto increment type. This automatically put in this field the values of 1 to 1913. With this serving as a row number, I used it to tell my code which record to update. It worked!

Here is my working code in case it can help someone else solve their problem.

$resultset = mysql_query("SELECT * FROM mailing");

for ($i=0; $i<=1912; $i++)  {
    $r = $i + 1;
    $id = uniqid();
    $query="UPDATE mailing SET id_key='$id' WHERE rec = '$r'";                              
    mysql_query($query) or die ("Error in query: $query <br>" . error_message);
    echo $i . "  " . $id . "<br>";
  }     

Your suggestion did get me back on the right track. A BIG THANKS again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.