Solved

sql statement - php

Posted on 2016-09-20
5
46 Views
Last Modified: 2016-09-20
Dear Experts,
I'm using Ray Paseur PHP Client Registration, Login, Logout and Easy Access Control pages.

I have created a database for my users and I already set their user name and password for them. However when I want to set uuk value for all the users with below code it set all uuk value the same.
However I want all the uuk codes unique.
$uuk = md5($uid . $pwd . rand());

How can I set every uuk code unique for every user?

my code:

$sql = "SELECT _key,uid,pwd FROM KullaniciBilgileri where uuk='' ";
$result = $conn->query($sql);


    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["_key"]. " - İsim: " . $row["uid"]. " " . $row["pwd"]. "<br>";
            
      $_key= $row["_key"];
      $uid= $row["uid"];
      $pwd= $row["pwd"];
            
      }
      
$uuk = md5($uid . $pwd . rand());

$sql2 = "UPDATE KullaniciBilgileri SET uuk = '$uuk'";
$result2 = $conn->query($sql2);
0
Comment
Question by:Braveheartli
  • 3
5 Comments
 
LVL 6

Accepted Solution

by:
Mukesh Yadav earned 500 total points
ID: 41806238
Try this ;)

To make it unique you need to put it in while loop:
 while($row = $result->fetch_assoc()) {
     echo "id: " . $row["_key"]. " - İsim: " . $row["uid"]. " " . $row["pwd"]. "<br>";
     $uuk = md5($row["uid"] . $row["pwd"] . rand());

    $sql2 = "UPDATE KullaniciBilgileri SET uuk = '$uuk' WHERE id = '" . $row["id"] . "'";
    $conn->query($sql2);
}


/* make sure you select id column to identify and update a single row, I assumed this column name id you can use whatever you are using with primary key */
So your select query would be like this:

$sql = "SELECT id, _key,uid,pwd FROM KullaniciBilgileri where uuk='' ";
0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41806256
Why not just run this query directly - let the database do the work
$sql = <<< QUERY
UPDATE KullaniciBilgileri 
  SET 
     uuk = MD5(CONCAT(uid,pwd, rand())) 
  WHERE 
      uuk=''
QUERY;
$conn->query($sql);

Open in new window

2
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41806257
In fact - if all you are doing is creating an unguesssable key then this is better
$sql = <<< QUERY
UPDATE KullaniciBilgileri 
  SET 
     uuk = UUID() 
  WHERE 
      uuk=''
QUERY;
$conn->query($sql);

Open in new window

1
 
LVL 1

Author Closing Comment

by:Braveheartli
ID: 41806312
thank you very much
0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41806324
Just a note to anyone reading this - there is no benefit to using script to do what can be done with a simple SQL query. Where possible let the database do the work and resort to script only when what needs to be done falls outside of what is possible with a query.
2

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now