Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

sql statement - php

Posted on 2016-09-20
5
57 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 8

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 55

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 55

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 55

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

856 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