Help with the request.

$sql = "SELECT `first_name`, `uidvk`, `rot` FROM `rotation` WHERE `first_name` = 'Леонид'" $Query = mysqli_query($CONNECT, $sql); $array = array(); while($Row = mysqli_fetch_assoc($Query) ){ $array[] = $Row; } 

Here we have an array of Leonids.

I need to update everyone Leonid.

 $sql = "UPDATE `rotation` SET `date` = NOW(), `trah` = '$_SESSION[USER_UIDVK]' WHERE ... "; // Тут нужно как-то сделать, чтобы апдейт // сделался по Леонидам из массива. mysqli_query($CONNECT, $sql); 

Something like: WHERE first_name = 'Leonid' - not satisfied. For the problem is that the orientation was for people from the array. I do not know yet how to do it.

  • one
    Maybe you should take the id all selected entries with leonids and then make UPDATE using IN ? - Alexey Shimansky
  • and what is unique in this array? uidvk ? - splash58
  • although go in my case will not, but please show an example. - MrCoder
  • Yes, Juydi is unique. and in fact, the array of all records will be retrieved, then there will be a request to the API, after which only suitable matching signals will remain in the array - MrCoder
  • What is "people orientation"? I look at your requests and do not understand what exactly you want to do. You don’t have any sorts, and it won’t help - NOW () will record the same time to all Leonids, regardless of the order. - Denis Khvorostin

3 answers 3

In your case, as I understand it, instead of id will be uidvk . Since they are unique.

There are at least two ways to sample.

1) Make an array with identifiers, based on the first sample. And on them to make UPDATE $array - an array with a SELECT result

 $ids = []; foreach ($array as $item) { array_push($ids, $item['uidvk']); } mysqli_query($CONNECT, "UPDATE `rotation` SET `date` = NOW(), `trah` = '$_SESSION[USER_UIDVK]' WHERE `uidvk` IN (". implode(',', $ids) .")"); 

implode - Combines the elements of an array into a string using the specified separator. In my case, this is a comma.

2) Make additional. selection of data identifiers using GROUP_CONCAT and on the result make UPDATE

SELECT GROUP_CONCAT(uidvk) AS ids FROM rotation WHERE first_name = 'Леонид'

the result ( $ids ) will be a string, such as 1,2,3,4,5,6,7 Based on this, we make a query.

 mysqli_query($CONNECT, "UPDATE `rotation` SET `date` = NOW(), `trah` = '$_SESSION[USER_UIDVK]' WHERE `uidvk` IN (". $ids .")"); 

3) Any other ways

Although in this case, the records of all Leonids will also be updated)) Perhaps you meant that you want to update the records in the $_SESSION[USER_UIDVK] .

If so, then you can only use implode and $_SESSION[USER_UIDVK] in UPDATE respectively.

    As always, the pollees have heard nothing about SQL injections.

     $sql = "UPDATE rotation SET date = NOW(), `trah` = ? WHERE uidvk = ?"; $stmt = $CONNECT->prepare($sql); $stmt->bind_param("ss", $_SESSION['USER_UIDVK'], $row['uidvk']); foreach ($array as $row) { $stmt->execute(); } 
    • Who said? Just the meaning of the question is different .... By the way, you can also use IN with prepared expressions, so your cycle is a bit superfluous - Alexey Shimansky
    • And what have the injections. I thought they are done when data is transmitted through a post or a het, and then this data goes in the request. Thought protection against sql injections is covered in data processing, I process all data from a post, a het or a cook before entering into a database of specials. function. And then I do not understand how the injection is caused. Explain, please. - MrCoder
     $array = []; while ($row = mysqli_fetch_assoc($query)) { $array[] = $row['uidvk']; } 

    Use PHP function implode () , and in SQL query IN

     mysqli_query($CONNECT, "UPDATE `rotation` SET `date` = NOW(), `trah` = '$_SESSION[USER_UIDVK]' WHERE `uidvk` IN (" . implode(',', $array) . ")"); 
    • Why minus. I did just that, it works. - MrCoder
    • @MrCoder if the answer helped you - mark it as accepted - Alexey Shimansky