Solved

sql statement - php

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

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 54

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 54

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 54

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MySQL Error Code 2 19
Wordpress Taxonomy 2 27
ebay api to see some data about my customers 2 28
Checkout Page Input Field not aligned 1 21
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
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 dynamically set the form action using jQuery.

776 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