Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

sql statement - php

Posted on 2016-09-20
5
Medium Priority
?
72 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
[X]
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
  • 3
5 Comments
 
LVL 9

Accepted Solution

by:
Mukesh Yadav earned 2000 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 59

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 59

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 59

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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This article discusses how to implement server side field validation and display customized error messages to the client.
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 and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

721 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