D
D
dudedude2020-08-11 11:52:43
PHP
dudedude, 2020-08-11 11:52:43

Update query not working after mysqli_fetch_assoc?

table structure
5f3257f25b21c232365359.jpeg

in the update function, the query does not work after mysqli_fetch_assoc, before mysqli_fetch_assoc( ) it works, more precisely, the update query returns true, but no changes occur in the table. 7.4 php, database MariaDB 10.3, server last openserver. the function should just change state, current to 0, next to 1
function
// $link = mysqli_connect($host, $user, $pass, $db_name);
function setNewColor($link) {
  $query = "SELECT * FROM colors";
  $result = mysqli_query($link, $query);
  for ($data = []; $row = mysqli_fetch_assoc($result); $data[] = $row);
  mysqli_free_result($result);
    for ($i=0;$i<count($data);$i++) {
      if ($data[$i]['state'] == 1) {
        //print_r($data[$i]);
        $id = $data[$i]['id'];

        $query = "UPDATE colors SET state = 0 WHERE id=$id";
        $result = mysqli_query($link, $query);
        //Если state = 1 у последнего цвета, меняем state у первого, иначе меняем state у i++ (следующего).
        if ($i == count($data)-1) {

          $id = $data[0]['id'];
          $query = "UPDATE colors SET state = 1 WHERE id = $id";
          $result = mysqli_query($link, $query);
        } else {

          $id = $data[$i++]['id'];
          $query = "UPDATE colors SET state = 1 WHERE id = $id";
          $result = mysqli_query($link, $query);
        }
        break;
      }
    }

}


this option doesn't work either.
function setNewColor($link) {
  $query = "SELECT * FROM colors";
  $result = mysqli_query($link, $query);
  for ($data = []; $row = mysqli_fetch_assoc($result); $data[] = $row);
  mysqli_free_result($result);
    $i;
    $id;
    for ($i=0;$i<count($data);$i++) {
      if ($data[$i]['state'] == 1) {
        //print_r($data[$i]);
        $id = $data[$i]['id'];
        break;
      }
    }
  $query = "UPDATE colors SET state = 0 WHERE id=$id";
        $result = mysqli_query($link, $query);
        //Если state = 1 у последнего цвета, меняем state у первого, иначе меняем state у i++ (следующего).
        if ($i == count($data)-1) {

          $id = $data[0]['id'];
          $query = "UPDATE colors SET state = 1 WHERE id = $id";
          $result = mysqli_query($link, $query);
        } else {

          $id = $data[$i++]['id'];
          $query = "UPDATE colors SET state = 1 WHERE id = $id";
          $result = mysqli_query($link, $query);
        }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Spartak (Web-StyleStudio), 2020-08-11
@dudedude

$id = $data[$i++]['id'];

It will not be at the next one, but after one => 0, 2, 4, 6, etc. ...
If it will also be after one, but like this => 1, 3, 5, 7, etc.. If you need the next one, then then So:
$id = $data[++$i]['id'];
$id = $data[$i+1]['id'];

F
FanatPHP, 2020-08-11
@FanatPHP

Where is the fix?
I wrote a bunch of meaningless sentences and did not write the main thing.
Questions like "Mine doesn't work" should be asked in this format

Here is the result of the select:
the code with the row select and the output of the result
Here is the result of the update of the same line:
the code with the update and the output of the result

And right away, even at the stage of writing the question, everything will become clear where the cant is in your code

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question