I am trying to update the field id_key which is an unique value using uniqid() function. When I print to the screen the value for uniqid() [$id] is different for each record. Which is correct and is what I want.

However when I go inspect the table all the values in the id_key column are the same for each record. What am I doing wrong? Each record should have a different value - the value shown on the screen with my echo statement.

while ($row = mysql_fetch_array($loop))
{
    $id = uniqid();
    echo $row['rec'] . " " . $row['first_name'] . " " . $row['email'] . " "  . $id . "<br/>";
    $query="UPDATE va SET id_key='$id'";
    mysql_query($query) or die ("Error in query: $query <br>" . error_message);
}   

You are missing the WHERE clause in the query

$query="UPDATE va SET id_key='$id' WHERE id_key='".$row['id_key']."'";

If type of id_key is INTEGER then try set value without apostrophes

$query="UPDATE va SET id_key=$id";

or

$query="UPDATE va SET id_key=".$id;

niranga,
You fixed it for me, all that was needed was the where clause. I did not realize that I would need one here but now I know.
Thanks!

AndrisP,
Your suggestion would give me an error in the query message. The values looks like this: 540f17638b808
The field type is varchar(13).
Thanks for your help. I will keep it in mind when I have a similar situation.

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.